咱們在使用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) 的註解,若是不存在則使用默認的