Configuration annotation
Import annotation
The @Import annotation only supports importing configuration classes
before 4.2. In 4.2, the @Import annotation supports importing ordinary java classes and declaring them as a bean
scenes to be used
The import annotation is mainly used in the process of explicitly creating beans based on java code, and is used to fuse multiple scattered java config configuration classes into a larger config class. In fact, in addition to the import annotation, there is also the importResource annotation, which has a similar function. The combination of configuration classes mainly occurs in the cross-module or cross-package configuration class reference process
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<import resource="config/customer.xml"/>
<import resource="config/scheduler.xml"/>
</beans>
equivalent to
@Configuration
@Import({ CustomerConfig.class, SchedulerConfig.class })
public class AppConfig {
}
common usage
@Import introduces ordinary classes
@Configuration
@Import({ImportBean.class, BeanImportSelector.class, MyImportBeanDefinitionRegistrar.class})
public class BeanImportConfig {
}
@Import introduces configuration classes (classes modified by @Configuration)
@Import({ImportBean.class, BeanImportSelector.class, MyImportBeanDefinitionRegistrar.class})
public class BeanImportConfig {
}
The class introduced by import here is the configuration class
If the configuration class is under the standard SpringBoot package structure (under the root directory of the SpringBootApplication startup class package). There is no need for @Import to import configuration classes, SpringBoot does it automatically. The above situation is generally used when the @Configuration configuration class is not under the standard SpringBoot package structure. So it is generally used when customizing the starter.
@Import introduces the implementation class of ImportSelector
Implement the ImportSelector interface
@Import introduces the implementation class of ImportBeanDefinitionRegistrar
Implement the ImportBeanDefinitionRegistrar interface
imorpt annotation principle
EnableAutoConfiguration annotation
The principle of automatic configuration
The @SpringBootApplication annotation is the main entry
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(excludeFilters = { @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
@Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })
public @interface SpringBootApplication {
The annotation related to configuration here is @EnableAutoConfiguration, continue to read
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
@Import(AutoConfigurationImportSelector.class)
public @interface EnableAutoConfiguration {}
Here @Import(AutoConfigurationImportSelector.class), referring to the import annotation principle above, here directly introduces AutoConfigurationImportSelector, directly implements DeferredImportSelector, DeferredImportSelector inherits ImportSelector
It can be seen that the logic is in the implementation method of selectImports
The tracking mainly depends on the getCandidateConfigurations method, the calling logic inside is in loadFactoryNames, and "META-INF/spring.factories" is loaded; the data inside is
obtained here is the configuration key as the value in org.springframework.boot.autoconfigure.EnableAutoConfiguration
The spring.factories file is a collection of key=value, as shown below
The class configured here is returned to the import interface to execute the registration process. Here you can refer to the above source code analysis
Each class loads logic in AutoConfiguration
@Configuration(proxyBeanMethods = false)
@ConditionalOnClass({ RabbitTemplate.class, Channel.class })
@EnableConfigurationProperties(RabbitProperties.class)
@Import(RabbitAnnotationDrivenConfiguration.class)
public class RabbitAutoConfiguration {}
For example, in this class, its property injection is implemented through
@EnableConfigurationProperties(RabbitProperties.class), and in RabbitProperties.class, the configuration file is implemented through
@ConfigurationProperties(prefix = “spring.rabbitmq”)
public class RabbitProperties {} associated with the configuration class
refer to
https://afoo.me/posts.htmlAutomatic
configuration principlehttps://blog.csdn.net/u014745069/article/details/83820511
Import annotations use https://www.jianshu.com/p/6b2f672e2446