我们在使用Spring Boot 项目时,引入一个Spring Boot Start依赖,只需要很少的代码,或者不用任何代码就能直接使用默认配置,再也不用那些繁琐的配置了,感觉特别神奇。我们自己也动手写一个start。
1、新建一个 Start 的 Maven 项目
pom 文件如下
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.1.0.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.6</version>
<optional>true</optional>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
说明:
spring-boot-autoconfigure springboot 自动配置的核心依赖
spring-boot-starter-test 测试包
lombok 省去 getter/setter 等简化代码
演示代码
DemoService接口类:
public interface DemoService {
String getMessage();
Integer getCode();
}
DemoServiceImpl实现类:
public class DemoServiceImpl implements DemoService {
@Override
public String getMessage() {
return "Hello!";
}
@Override
public Integer getCode() {
return 123;
}
}
DemoAutoConfiguration自动配置类:
@Configuration
public class DemoAutoConfiguration {
@Bean
@ConditionalOnMissingBean(DemoService.class)
public DemoService demoService() {
return new DemoServiceImpl();
}
}
说明:
@Configuration 标注该类为一个配置类
ConditionalOnMissingBean(DemoService.class) 条件注解
让Spring Boot识别自动自动配置的代码
需要在resources下新建文件META-INF/spring.factories
org.springframework.boot.autoconfigure.EnableAutoConfiguration=cn.springmvc.DemoAutoConfiguration
Spring Boot 中的注解 @EnableAutoConfiguration 在项目启动的时候会通过 SpringFactoriesLoader.loadFactoryNames 方法获取 spring.factories 文件下的配置类
新建启动类:
@SpringBootApplication
public class StartDemoApplication {
public static void main(String[] args) {
SpringApplication.run(StartDemoApplication.class, args);
}
}
新建测试类,代码如下所示:
@RunWith(SpringRunner.class)
@SpringBootTest
public class StartDemoApplicationTests {
@Resource
private DemoService demoService;
@Test
public void test() {
String message = demoService.getMessage();
System.out.println(message);
Assert.assertEquals("Hello!", message);
Integer code = demoService.getCode();
System.out.println(code);
Assert.assertEquals(123, (int) code);
}
}
备注:
如果没有StartDemoApplication这个类,则测试类启动的时候会报 @SpringBootApplication 找不到错误。
2、新建 Spring Boot 项目引入刚写的start项目
新建service类,如下代码所示:
@Service
public class TestService {
@Resource
private DemoService demoService;
public void message() {
System.out.println("code:" + demoService.getCode());
System.out.println("message:" + demoService.getMessage());
}
}
新建测试类,如下代码所示:
@Resource
private TestService testService;
@Test
public void test() {
testService.message();
}
运行结果为:
code:123
message:Hello!
重写DemoService方法,主要增加@Service注解,代码如下所示:
@Service
public class DemoServiceImpl implements DemoService {
@Override
public String getMessage() {
return "Hello!";
}
@Override
public Integer getCode() {
return 123;
}
}
测试结果:
code:123
message:Hello!
增加@Service注解和没有@Service注解运行的结果相同,之所以出现这种情况,是因为在start项目中的DemoService实现类中有一个@ConditionalOnMissingBean(DemoService.class)的注解。
Demo 源码
https://github.com/drinkagain/springboot-start-demo