“This article has participated in the good article call order activity, click to see: back end, big front end double track submission, 20,000 yuan prize pool for you to challenge!”
Standard development process of the SpringBoot Starter
A complete SpringBoot Starter module contains the following components:
- The autoconfigure module, which contains the autoconfiguration code and configures the autoconfiguration classes that need to be loaded upon startup in the meta-INF/spring-factories file
- Starter module, which provides dependencies to the AutoConfigure module as well as class libraries and other dependencies commonly used
The dependencies are as follows:
graph TD
xxx-xxx-project --> xxx-xxx-starter --> xxx-xxx-autoconfigurer
If you don't want to separate the two modules, you can also combine the autoconfiguration code and dependency management in one module
Benefits of using Starter
- Integrates the dependency libraries required by this module
- Provides configuration items for modules to consumers
- Provides an autoconfiguration class to automate the assembly of beans within a module
Starter development steps
- Create a new Maven project and define the dependencies to be used in the project’s POM file
- Create a configuration class, write the configuration item and the default configuration value, and specify the configuration item prefix
- Create a new autoliring class, using @Configuration and @Bean for autoliring
- Create the spring.factories file and specify the autowire class for the Starter
The specific creation process and code demonstration will not be detailed here, you can refer to this blog: blog.csdn.net/weixin_3865…
Because we have a much simpler and more efficient way
It’s me: Mica-Auto
Functional principle
Mica-auto Automatically generates some SpringBoot configuration files based on the SpringBoot configuration annotations
- Generate spring. Factories
- Generate the spring – devtools. Properties
- Generate the FeignClient in spring.factories to automate the Feign configuration in MICA
The idea is to scan classes that reference the @Component annotation or a composite annotation containing @Component and automatically generate the corresponding configuration
With this feature, we can write our own Starter without the AutoConfigurer module, because mica-Auto will automatically scan for us the Configuration class that references @Configuration, which is a composite annotation that contains @Component. The meta-INF /spring.factories file is automatically created, and the configuration class full pathname is automatically written to the file.
How to use
Simply add a dependency to the Starter project
maven
<dependency>
<groupId>net.dreamlu</groupId>
<artifactId>mica-auto</artifactId>
<version>${version}</version>
<scope>provided</scope>
</dependency>
Copy the code
gradle
annotationProcessor("net.dreamlu:mica-auto:${version}")
Copy the code
Note: If you are using Lombok in your project, place the mica-Auto dependencies behind Lombok.
Results show
You can see from the classes compiled by Idea and the JAR package built by Maven that Mica-Auto has added the auto-configuration classes to the meta-INF /spring.factories
Expand the use
In fact, MICA-Auto is not only used to simplify the development of Starter. In our daily development, we often encounter that business modules depend on external modules in multi-module projects. Usually, their package names are inconsistent.
- Customize a Starter and rely on the Starter
- write
@EnableXXX
Annotation, which is introduced into SpringBoot’s boot class - Boot class introduced in SpringBoot
@Import
Annotations, import configuration classes - Boot class introduced in SpringBoot
@ComponentScan
Note, manually scan the Component that is injected into an external module
All four methods are intrusive, but mica-auto writes to meta-INF /spring.factories for each Component, SpringBoot when start according to @ SpringBootApplication – > @ EnableAutoConfiguration – > find AutoConfigurationImportSelector @ Import, His selectImports method scans the meta-INF /spring.factories file and infuses it into spring’s bean container.
Therefore, just add mica-auto to the dependent module and you can happily use the external Component in the business module.
So Easy!