When we use the SpringBoot project, we can introduce a SpringBoot Start dependency, which requires very little code, or no code to use the default configuration, and no more tedious configuration. Let’s write a start ourselves.
Create a New Maven project for Start
- The POM file is as follows
<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>
Copy the code
- Spring-boot-autoconfigure A core dependency for springboot automatic configuration
- Spring – the boot – starter – test test package
- Lombok dispenses with getters/setters and other simplified code
- Demo code
DemoService:
public interface DemoService {
String getMessage(a);
Integer getCode(a);
}
Copy the code
DemoServiceImpl:
public class DemoServiceImpl implements DemoService {
@Override
public String getMessage(a) {
return "Hello!";
}
@Override
public Integer getCode(a) {
return 123; }}Copy the code
DemoAutoConfiguration:
@Configuration
public class DemoAutoConfiguration {
@Bean
@ConditionalOnMissingBean(DemoService.class)
public DemoService demoService(a) {
return newDemoServiceImpl(); }}Copy the code
- @Configuration Indicates that this class is a Configuration class
- ConditionalOnMissingBean(demoservice.class) ConditionalOnMissingBean(demoservice.class
Spingboot’s automatic annotations are mainly implemented with these conditional annotations. Check out the previous article:
Conditional notes for Spring Boot automatic configuration
Spring Boot automatic configuration @enable * and @import annotations
Spring Boot automatic configuration @enableAutoConfiguration
- Have SpringBoot recognize code that is automatically configured
You need to create a new file meta-INF /spring.factories under Resources
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.jiuxian.DemoAutoConfiguration
Copy the code
SpringBoot the annotations in @ EnableAutoConfiguration. At the time of launch will pass SpringFactoriesLoader loadFactoryNames method to get the spring. The factories Configuration class under file
- The test class
- We need to package the main method of Application Run
@SpringBootApplication
public class StartDemoApplication {
public static void main(String[] args) { SpringApplication.run(StartDemoApplication.class, args); }}Copy the code
- The test class
@RunWith(SpringRunner.class)
@SpringBootTest
public class StartDemoApplicationTests {
@Resource
private DemoService demoService;
@Test
public void test(a) {
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); }}Copy the code
If there is no StartDemoApplication class, the test class starts with an @SpringBootApplication failed to find error
2. Create a SpringBoot project
- service
@Service
public class TestService {
@Resource
private DemoService demoService;
public void message(a) {
System.out.println("code:" + demoService.getCode());
System.out.println("message:"+ demoService.getMessage()); }}Copy the code
- test
@Resource
private TestService testService;
@Test
public void test(a) {
testService.message();
}
Copy the code
Results:
code:123
message:Hello!
Copy the code
- Override the DemoService method
@Service
public class DemoServiceImpl implements DemoService {
@Override
public String getMessage(a) {
return "Hello!";
}
@Override
public Integer getCode(a) {
return 123; }}Copy the code
- The test results
code:123
message:Hello!
Copy the code
This result is due to the @conditionAlonmissingBean (demoservice.class) annotation in the DemoService implementation class of the Start project, which is used by default if it does not exist
Third, Demo source code
GitHub source address