Article Directory
Spring Boot Starter official website description : Spring Boot Starter official introduction
What is Spring Boot Starter
Starters can be understood as a starter, which contains a series of dependency packages that can be integrated into the application. It can integrate Spring and other technologies in one stop without looking for sample code and dependency packages everywhere. The working principle of Spring Boot Starter is: Spring Boot scans the JAR package that the project depends on at startup, looks for the JAR package containing the spring.factories file, spring.factories
loads the AutoConfigure
class according to the configuration @Conditional
, performs automatic configuration and injects the Bean according to the annotation conditionsSpring Context
Why customize Spring Boot Starter?
In order to simplify our development, the Spring Boot official website has provided a lot of starters for us to use. Even so, it cannot fully meet the development scenarios in our actual work. At this time, we need to customize the custom implementation Starter.
Implementation steps
1. First, create a Maven empty project and add two modules
Launcher
There is no source code in the launcher, just tell us which dependencies need to be introduced in the current scene!
Create a launcher module as a maven project, named as
xiaozhao-hello-spring-boot-starter
, the corresponding dependency file
<groupId>com.zhao</groupId>
<artifactId>xiaozhao-hello-spring-boot-starter</artifactId>
<version>1.0-SNAPSHOT</version>
auto-configuration package
All autoconfiguration features are implemented in the autoconfiguration package!
Create an automatic configuration package module as the SpringBoot initialization project, namedxiaozhao-hello-spring-boot-starter-autoconfigure
The final project module is as follows:
2. After the module is created, you need to import the automatic configuration module into the launcher (if someone else introduces the scene launcher, the automatic configuration package will be automatically imported)
<dependencies>
<dependency>
<groupId>com.zhao</groupId>
<artifactId>xiaozhao-hello-spring-boot-starter-autoconfigure</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
3. Write the automatic configuration module.
- Create a custom Properties file
@ConfigurationProperties("xiaozhao.hello")
public class HelloProperties {
private String prefix;
private String suffix;
public String getPrefix() {
return prefix;
}
public void setPrefix(String prefix) {
this.prefix = prefix;
}
public String getSuffix() {
return suffix;
}
public void setSuffix(String suffix) {
this.suffix = suffix;
}
}
- Create a business class to read the values in the Properties file
public class HelloService {
@Autowired
HelloProperties helloProperties;
public String sayHello(String userName){
return helloProperties.getPrefix() + ":" + userName + ">" + helloProperties.getSuffix();
}
}
- Another automatic configuration class, automatic class loading
@Configuration
@ConditionalOnMissingBean(HelloService.class)
@EnableConfigurationProperties(HelloProperties.class)
public class HelloServiceAutoConfiguration {
@Bean
public HelloService helloService(){
HelloService helloService = new HelloService();
return helloService;
}
}
The final effect is as follows:
4. resources
Create META-INF/spring.factories in the directory and add the following configuration information
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.zhao.hello.auto.HelloServiceAutoConfiguration
5. Install the hello-spring-boot-starter-autoconfigure
module andxiaozhao-hello-spring-boot-starter
6. After the installation is complete, create a new project to introduce the created starter. The new project is called hello-test
the Spring Boot initialization project.
Introduce the Starter starter we defined earlier and the Web starter that comes with Spring Boot
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.zhao</groupId>
<artifactId>xiaozhao-hello-spring-boot-starter</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
7. hello-test
Create a test Controller in the project
@RestController
public class HelloController {
@Autowired
HelloService helloService;
@GetMapping("/hello")
public String sayHello() {
String str = helloService.sayHello("李四");
return str;
}
}
Write a configuration file
xiaozhao.hello.prefix=hello
xiaozhao.hello.suffix=666
8. Start the project and test it
Summarize the implementation logic of custom Starter
- First introduce a custom Starter:
xiaozhao-hello-spring-boot-starter
, introduce a custom automatic configuration scenario in this Starter - When the automatic configuration scene starts, it will look for the spring.factories file to automatically load
HelloServiceAutoConfiguration
the class file - After the automatic configuration class is loaded,
@ConditionalOnMissingBean(HelloService.class)
through this annotation, when there is no component in the container,HelloService
a component is automatically addedHelloService
. HelloService
All properties of the component are bound throughHelloPropertie
the s configuration file,@ConfigurationProperties("xiaozhao.hello")
, throughxiaozhao.hello。xxx
binding.- If you inject a component in the container yourself
HelloService
, it is not automatically configured, but re-injected.