我們在使用SpringBoot 項目時,引入一個springboot start依賴叹侄,只需要很少的代碼巩搏,或者不用任何代碼就能直接使用默認配置,再也不用那些繁瑣的配置了趾代,感覺特別神奇贯底。我們自己也動手寫一個start.
一、新建一個 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) 條件注解
spingboot 的自動注解主要還是用這些條件注解來實現(xiàn)的撒强。請查看之前的文章:
- 讓springboot 識別自動自動配置的代碼
需要在resources下新建文件META-INF/spring.factories
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.jiuxian.DemoAutoConfiguration
SpringBoot 中的注解 @EnableAutoConfiguration 在項目啟動的時候會通過 SpringFactoriesLoader.loadFactoryNames 方法獲取 spring.factories 文件下的配置類
- 測試類
- 首先需要再包下建 Application run 的main 方法
@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 找不到錯誤
二禽捆、新建 springboot 項目引入剛寫的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
public class DemoServiceImpl implements DemoService {
@Override
public String getMessage() {
return "Hello!";
}
@Override
public Integer getCode() {
return 123;
}
}
- 測試結果
code:123
message:Hello!
之所以這樣的結果笙什,是因為在start項目中的DemoService 實現(xiàn)類中有一個 @ConditionalOnMissingBean(DemoService.class) 的注解,如果不存在則使用默認的