Scenario: In the project, if you want to automatically assemble other customized Maven project library configurations in the current maven project, you will get multiple maven references. If you want simple configurations, spring boot will automatically assemble database configuration classes.
From then on, we think about how our new project assembles third-party jar s. Tracking the spring boot source shows that the method is actually very simple. springboot achieves this through the @EableAutoConfiguration annotation.
Principle: The annotation @EnableAutoConfiguration refers to the @Import ({AutoConfiguration ImportSelector. class}) annotation and defines the getCandidateConfigurations method to load the custom configuration class in the AutoConfiguration ImportSelector class.
protected List<String> getCandidateConfigurations(AnnotationMetadata metadata, AnnotationAttributes attributes) { List<String> configurations = SpringFactoriesLoader.loadFactoryNames(this.getSpringFactoriesLoaderFactoryClass(), this.getBeanClassLoader()); Assert.notEmpty(configurations, "No auto configuration classes found in META-INF/spring.factories. If you are using a custom packaging, make sure that file is correct."); return configurations; }
As you can see from the information prompted by this method, if you customize the package you need to assemble, you need to define it in the META-INF/spring.factories file. How to define it? Find the file org/spring framework/boot/spring-boot-autoconfigure/2.1.5.RELEASE/spring-boot-autoconfigure-2.1.5.RELEASE.jar!/META-INF/spring.factories in the import package (version 2.1.5 I currently use, find the corresponding method according to the actual version used)
# Auto Configure org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ org.springframework.boot.autoconfigure.admin.SpringApplicationAdminJmxAutoConfiguration,\ org.springframework.boot.autoconfigure.aop.AopAutoConfiguration
See Auto Configure, then we need to import resources META-INF spring. factories into the current maven project file, add the full path of the configuration class that needs to be assembled in the file, as follows
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.boot.config.core.config.BeanConfiguration #Full paths of configuration classes that need to be assembled
At this point, the configuration class instance in the current project block.
So far, here's the code
Preparing two maven projects
First project
(1) Two bean s are defined in the domain, without attributes and methods, and without annotations. The BeanConfiguration class is as follows
BeanConfiguration.java1 package com.boot.config.core.config; 2 3 import com.boot.config.core.domain.Order; 4 import com.boot.config.core.domain.Product; 5 import org.springframework.context.annotation.Bean; 6 import org.springframework.context.annotation.Configuration; 7 8 @Configuration 9 public class BeanConfiguration { 10 11 @Bean 12 public Order creatOrder() { 13 return new Order(); 14 } 15 16 @Bean 17 public Product createProduct() { 18 return new Product(); 19 } 20 }