通过 @SpringBootApplication 注解导入
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(excludeFilters = { @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
@Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })
public @interface SpringBootApplication {
通过 @EnableAutoConfiguration 注解
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
@Import({AutoConfigurationImportSelector.class})
public @interface EnableAutoConfiguration {
org.springframework.boot.autoconfigure.AutoConfigurationImportSelector
spring 加载自动配置类依靠的是 SpringFactoriesLoader 类,该类会自动加载 ClassLoader 下所有 jar 包中的 META-INF/spring.factories 文件所配置的 Bean。
舉例子: RabbitAutoConfiguration 配置类
首先来看RabbitAutoConfiguration类上的注解:
@Configuration
@ConditionalOnClass({ RabbitTemplate.class, Channel.class })
@EnableConfigurationProperties(RabbitProperties.class)
@Import(RabbitAnnotationDrivenConfiguration.class)
public class RabbitAutoConfiguration {
@Configuration: 应该不需要解释
@ConditionalOnClass:表示存在对应的Class文件时才会去解析RabbitAutoConfiguration,否则直接跳过不解析,这也是为什么在不导入RabbitMQ依赖Jar时工程能正常启动的原因
@EnableConfigurationProperties:表示对@ConfigurationProperties的内嵌支持,默认会将对应Class这是为bean,例如这里值为RabbitProperties.class,其定义为:
@ConfigurationProperties(prefix = "spring.rabbitmq")
public class RabbitProperties {
/**
* RabbitMQ host.
*/
private String host = "localhost";
/**
* RabbitMQ port.
*/
private int port = 5672; .... //省略部分代码}
RabbitProperties提供对RabbitMQ的配置信息,其前缀为spring.rabbitmq,因此在上篇中配置的host、port等信息会配置到该类上,
随后@ EnableConfigurationProperties会将RabbitProperties注册为一个bean。
@Import为导入配置,RabbitAnnotationDrivenConfiguration具体实现如下:
@Configuration
@ConditionalOnClass(EnableRabbit.class)
class RabbitAnnotationDrivenConfiguration {
@Autowired(required = false)
private PlatformTransactionManager transactionManager;
@Bean
@ConditionalOnMissingBean(name = "rabbitListenerContainerFactory")
public SimpleRabbitListenerContainerFactory rabbitListenerContainerFactory(
ConnectionFactory connectionFactory) {
SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
factory.setConnectionFactory(connectionFactory);
if (this.transactionManager != null) {
factory.setTransactionManager(this.transactionManager);
}
return factory;
}
@EnableRabbit
@ConditionalOnMissingBean(name = RabbitListenerConfigUtils.RABBIT_LISTENER_ANNOTATION_PROCESSOR_BEAN_NAME)
protected static class EnableRabbitConfiguration {
}
}
这里又涉及到一个重要的注解:@ConditionalOnMissingBean,其功能为如果存在指定name的bean,则该注解标注的bean不创建,
@ConditionalOnMissingBean(name = "rabbitListenerContainerFactory")
表示的意思为:如果存在名称为rabbitListenerContainerFactory的bean,则该部分代码直接忽略,这是Spring Boot人性化体现之一,开发者申明的bean会放在第一位,实在是太懂礼貌了~~
本篇中涉及到的注解比较多,其具体实现原理在以后有时间再具体分析。
再回到RabbitAutoConfiguration类的具体实现
首先来看:
@Configuration
@ConditionalOnMissingBean(ConnectionFactory.class)
protected static class RabbitConnectionFactoryCreator {
@Bean
public ConnectionFactory rabbitConnectionFactory(RabbitProperties config) {
CachingConnectionFactory factory = new CachingConnectionFactory();
String addresses = config.getAddresses();
factory.setAddresses(addresses);
if (config.getHost() != null) {
factory.setHost(config.getHost());
factory.setPort(config.getPort());
}
if (config.getUsername() != null) {
factory.setUsername(config.getUsername());
}
if (config.getPassword() != null) {
factory.setPassword(config.getPassword());
}
if (config.getVirtualHost() != null) {
factory.setVirtualHost(config.getVirtualHost());
}
return factory;
}
}
创建了默认的ConnectionFactory,需要注意的时,这里的ConnectionFactory无回调的设置(解答了上篇中的疑问)
@Bean
@ConditionalOnMissingBean(RabbitTemplate.class)
public RabbitTemplate rabbitTemplate() {
return new RabbitTemplate(this.connectionFactory);
}
创建了默认的RabbitTemplate;下面创建的RabbitMessagingTemplate实现对 RabbitTemplate的包装。
@SpringBootApplication => @EnableAutoConfiguration =>@Import(
AutoConfigurationImportSelector(自动配置文件收集器))
1. 通过AutoConfigurationImportSelector.selectImports() 其中主要逻辑是
通过SpringFactoriesLoader的FactoryClassNames 获取 springBoot 默认配置的自动注入配置类的说有全路径名(EnableAutoConfiguration) 放入list 中)
=> 通过反射实例化到spring的上下文。
注意
1. 自动注入 只注入 META-INF/spring.factories 中 key = org.springframework.boot.autoconfigure.EnableAutoConfiguration
2. 我们自己写主动注入的时候必须要在 写 META-INF/spring.factories 中 key = org.springframework.boot.autoconfigure.EnableAutoConfiguration