1.项目创建及结构分析
1.1打开sts集成开发工具,创建spring boot项目
第一步:打开项目新建窗口(快捷键crtl+n),搜索spring,选择spring starter项目
第二步:填写项目基本信息
其中:指定Java版本为8,打包方式jar
第三步:选择spring boot 版本
建议选择官方指定的版本
最后finish即可
2.项目启动过程分析
2.1找到项目的入口类(使用了@SpringBootApplication注解描述),然后运行启动类,检查启动过程,启动时,控制台会出现spring标识spring 是一个资源整合框架,通过spring可将很多资源整合在一起,然后进行科学应用,以便更好的对外提供服务。
3.简单案例
3.1第一步:定义业务Cache接口
package com.cy.pj.common.cache;
public interface Cache {
}
3.2第二步:定义WeakCache实现Cache接口
package com.cy.pj.common.cache;
import org.springframework.stereotype.Component;
@Component
public class WeakCache implements Cache{}
3.3第三步:创建或修改SoftCache,此类也实现Cache接口
package com.cy.pj.common.cache;
@Component
public class SoftCache implements Cache{
}
3.4第四步:单元测试类
package com.cy.pj.common.cache;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
public class CacheTests {
@Autowired
@Qualifier("softCache")
private Cache cache;
@Test
public void testCache() {
System.out.println(cache);
}}
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。