1. Description
I had a tentative interview with a company before. One of the questions was: have you ever written a SpringBoot custom starter
Since I have read the source code of Spring before, is this still a problem? Although I haven't written it, I explained the principle again. Take the time to write a custom starter and an enhanced version.
Let's write a simple one first.
2. Principle
The principle of starter is very simple: during the startup process, SpringBoot will get meta-inf / spring.exe in all jar packages under the classpath Factories file, go to org. Org springframework. boot. autoconfigure. Enableautoconfiguration configuration item to judge whether the configured class meets the conditions of automatic assembly.
But writing a custom starter requires some additional knowledge
-
@ConfigurationProperties annotation function
-
@EnableConfigurationProperties annotation function
The following two articles can understand the principle of automatic configuration and the use of relevant annotations. You can read them before writing.
Analysis of SpringBoot starter startup mechanism
https://blog.csdn.net/zxd1435513775/article/details/99064671
How to read configuration properties in Spring and important features of SpringBoot automatic configuration
https://blog.csdn.net/zxd1435513775/article/details/103661672
3. Steps
- Introduce corresponding dependencies
- Write automatic Configuration class: use @ EnableConfigurationProperties annotation and @ Configuration annotation
- Automatic configuration class and related property class binding: use @ configurationproperties annotation
- Write specific implementation classes
- In resources / meta-inf / spring Configure custom auto assembly classes in factories
4. Code implementation
directory structure
4.1 pom file
Finally, you should type the customized starter project into a jar for other projects to reference
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> </dependencies>
4.2 automatic configuration
For this auto configuration class, configure it in meta-inf / spring In the factories file. Once this autoconfiguration class takes effect, you can do a lot of things. The next Enhanced Edition will be written here.
@Slf4j @Configuration // Binding property configuration class @EnableConfigurationProperties(CustomConfigurationProperties.class) @ConditionalOnClass(CustomService.class) // Only when there is a specified class in the class path can the configuration be changed effectively public class CustomConfigurationClass { @Bean public CustomService customService(){ log.info("customService...."); return new CustomServiceImpl(); } }
4.3 attribute configuration class
@ConfigurationProperties("custom.node") public class CustomConfigurationProperties { // Give default value private String customHost = "127.0.0.1"; private String customPort = "8080"; public String getCustomHost() { return customHost; } public void setCustomHost(String customHost) { this.customHost = customHost; } public String getCustomPort() { return customPort; } public void setCustomPort(String customPort) { this.customPort = customPort; } }
4.4 specific business types
For this specific business class, there are too many things to write and too many things to do. Only test code is written here.
@Slf4j @Service public class CustomServiceImpl implements CustomService { @Autowired CustomConfigurationProperties cp; @Override public void customMethod() { log.info("customMethod invoke ..."); log.info(cp.getCustomHost()+" : " + cp.getCustomPort()); } }
4.5 spring.factories
org.springframework.boot.autoconfigure.EnableAutoConfiguration= com.scorpios.customstarter.config.CustomConfigurationClass
5. Test
Project startup log
In application Custom. Is not configured in properties node. customHost,custom. node. The test results of customport are as follows:
In application Add the following configuration to properties, and the test results are as follows:
custom.node.customHost=192.168.3.1 custom.node.customPort=8888
The custom starter has been written, which is the application of several knowledge points. The specific code address is as follows:
Code address: https://github.com/Hofanking/springboot-custom-starter-example