(1) Overview
SpringBoot is widely used for its auto-assembly capabilities. We must have encountered many spring-boot-starter named dependencies when writing code, such as spring-boot-starter-web. After introducing these starter dependencies in poM files, SpringBoot can then scan these classes using autowiring techniques and load them into Bean containers.
In addition to these official SpringBoot starters, we can also develop our own. To distinguish it from the official starter, you are advised to name a user-defined starter in the format of XXXX-spring-boot-starter, for example, mybatis-spring-boot-starter.
This article shows you how to implement a starter yourself.
(2) Look at an example
Before writing your own Starter, it’s important to look at how others are writing. Take the RedisAutoConfiguration class as an example.
SpringBoot starts with an autowiring scan of the project Mata-INF/Spring.Factories file that defines all the classes that need to be autowled. The Redis autowiring class is RedisAutoConfiguration shown below.
ConditionalOnClass specifies that the Bean will be loaded only when the class behind the annotation exists. It is clear from the image below that RedisOperations do not exist, so Redis will not be auto-assembled.
@ EnableConfigurationProperties for automatic loading configuration class information, configuration class, as we usually write the basic, through ConfigurationProperties reads the properties or in yaml configuration information.
There are two beans defined in the RedisAutoConfiguration class that we should be familiar with. ConditionalOnMissingBean ConditionalOnMissingBean ConditionalOnMissingBean ConditionalOnMissingBean ConditionalOnMissingBean ConditionalOnMissingBean ConditionalOnMissingBean ConditionalOnMissingBean ConditionalOnMissingBean ConditionalOnMissingBean ConditionalOnMissingBean ConditionalOnMissingBean ConditionalOnMissingBean ConditionalOnMissingBean ConditionalOnMissingBean ConditionalOnMissingBean ConditionalOnMissingBean ConditionalOnMissingBean ConditionalOnMissingBean ConditionalOnMissingBean
Now that you know the basic implementation of a Starter, let’s write a simple Starter.
(3) Realize the information broadcasting Starter
The Starter to do is actually very simple, is the output configuration file configuration information.
First, create a New Maven project. Create two modules in the project. I named the two projects report-spring-boot-starter and example spring-boot
First, we introduce report-spring-boot-starter, which is a starter. It mainly outputs some content. We can write according to RedisAutoConfiguration. First, create ReportAutoConfiguration
@Configuration
@ConditionalOnClass(ReportOperation.class)
@EnableConfigurationProperties(ReportProperties.class)
public class ReportAutoConfiguration {
@Bean
public ReportOperation reportOperation(ReportProperties reportProperties){
ReportOperation reportOperation = new ReportOperation(reportProperties.getMsg());
returnreportOperation; }}Copy the code
The configuration is loaded only when ReportOperation exists, and the ReportProperties configuration file is activated
public class ReportOperation {
private String msg;
public ReportOperation(String msg){
this.msg = msg;
}
public String getMsg(a) {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public String report(a){
returnmsg; }}Copy the code
ReportProperties is as follows:
@ConfigurationProperties(prefix = "report")
public class ReportProperties {
private String msg;
public void setMsg(String msg) {
this.msg = msg;
}
public String getMsg(a) {
returnmsg; }}Copy the code
In the resources directory, start a new meta-INF /spring.factories (). Write down configuration information:
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.javayz.starter.ReportAutoConfiguration
Copy the code
This simple Starter is done.
(4) Call the Starter
In the starter module, the springboot dependency and the starter dependency will be added:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.example</groupId>
<artifactId>report-spring-boot-starter</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
Copy the code
Then write a test for the controller to test the effect of automatic injection:
@RestController
public class TestController {
@Autowired
private ReportOperation reportOperation;
@GetMapping("test")
public String test(a){
System.out.println(reportOperation.getMsg());
returnreportOperation.getMsg(); }}Copy the code
Add a configuration to the configuration file:
report.msg = hello
Copy the code
Run the project call interface and you can see that the ReportOperation Bean has been automatically injected.
(5) Summary
This article combined with redis automatic injection example, wrote a Starter of their own, I hope to help you. I’m fish boy. See you next time!