咱们在使用SpringBoot 项目时,引入一个springboot start依赖,只须要不多的代码,或者不用任何代码就能直接使用默认配置,不再用那些繁琐的配置了,感受特别神奇。咱们本身也动手写一个start.java
<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>
复制代码
DemoService:git
public interface DemoService {
String getMessage();
Integer getCode();
}
复制代码
DemoServiceImpl:github
public class DemoServiceImpl implements DemoService {
@Override
public String getMessage() {
return "Hello!";
}
@Override
public Integer getCode() {
return 123;
}
}
复制代码
DemoAutoConfiguration:spring
@Configuration
public class DemoAutoConfiguration {
@Bean
@ConditionalOnMissingBean(DemoService.class)
public DemoService demoService() {
return new DemoServiceImpl();
}
}
复制代码
spingboot 的自动注解主要仍是用这些条件注解来实现的。请查看以前的文章:springboot
Spring Boot 自动配置之@Enable*与@Import注解spring-boot
须要在resources下新建文件META-INF/spring.factories测试
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.jiuxian.DemoAutoConfiguration
复制代码
SpringBoot 中的注解 @EnableAutoConfiguration 在项目启动的时候会经过 SpringFactoriesLoader.loadFactoryNames 方法获取 spring.factories 文件下的配置类spa
@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 找不到错误
@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!
复制代码
@Service
public class DemoServiceImpl implements DemoService {
@Override
public String getMessage() {
return "Hello!";
}
@Override
public Integer getCode() {
return 123;
}
}
复制代码
code:123
message:Hello!
复制代码
之因此这样的结果,是由于在start项目中的DemoService 实现类中有一个 @ConditionalOnMissingBean(DemoService.class) 的注解,若是不存在则使用默认的