spring boot实战(第十三篇)自动配置原理分析
前言
spring Boot中引入了自动配置,让开发者利用起来更加的简便、快捷,本篇讲利用RabbitMQ的自动配置为例讲分析下Spring Boot中的自动配置原理。
在上一篇末尾讲述了Spring Boot 默认情况下会为ConnectionFactory、RabbitTemplate等bean,在前面的文章中也讲到嵌入的Tomcat默认配置为8080端口
这些都属于Spring Boot自动配置的范畴,当然其自动配置相当多。
EnableAutoConfiguration注解
在创建Application时我们使用了SpringBootApplication注解,在spring boot实战(第九篇)Application创建源码分析中曾有所分析,再来看下其定义:
- @Target(ElementType.TYPE)
- @Retention(RetentionPolicy.RUNTIME)
- @Documented
- @Inherited
- @Configuration
- @EnableAutoConfiguration
- @ComponentScan
- public @interface SpringBootApplication {
- Class<?>[] exclude() default {};
- }
该注解上存在元注解@EnableAutoConfiguration,这就是Spring Boot自动配置实现的核心入口;其定义为:
- @Target(ElementType.TYPE)
- @Retention(RetentionPolicy.RUNTIME)
- @Documented
- @Inherited
- @Import({ EnableAutoConfigurationImportSelector.class,
- AutoConfigurationPackages.Registrar.class })
- public @interface EnableAutoConfiguration {
- /**
- * Exclude specific auto-configuration classes such that they will never be applied.
- * @return the classes to exclude
- */
- Class<?>[] exclude() default {};
- }
很显然能看出有一特殊的注解@Import,该注解在spring boot实战(第十篇)Spring boot Bean加载源码分析中有讲解到,加载bean时会解析Import注解,因此需要讲目光聚集在这段代码
- @Import({ EnableAutoConfigurationImportSelector.class,
- AutoConfigurationPackages.Registrar.class })
EnableAutoConfigurationImportSelector
来看EnableAutoConfigurationImportSelector类
- public String[] selectImports(AnnotationMetadata metadata) {
- try {
- AnnotationAttributes attributes = AnnotationAttributes.fromMap(metadata
- .getAnnotationAttributes(EnableAutoConfiguration.class.getName(),
- true));
- Assert.notNull(attributes, "No auto-configuration attributes found. Is "
- + metadata.getClassName()
- + " annotated with @EnableAutoConfiguration?");
- // Find all possible auto configuration classes, filtering duplicates
- List<String> factories = new ArrayList<String>(new LinkedHashSet<String>(
- SpringFactoriesLoader.loadFactoryNames(EnableAutoConfiguration.class,
- this.beanClassLoader)));
- // Remove those specifically disabled
- factories.removeAll(Arrays.asList(attributes.getStringArray("exclude")));
- // Sort
- factories = new AutoConfigurationSorter(this.resourceLoader)
- .getInPriorityOrder(factories);
- return factories.toArray(new String[factories.size()]);
- }
- catch (IOException ex) {
- throw new IllegalStateException(ex);
- }
- }
看如下代码,获取类路径下spring.factories下key为EnableAutoConfiguration全限定名对应值
- SpringFactoriesLoader.loadFactoryNames(EnableAutoConfiguration.class,
- this.beanClassLoader))
其结果为:
- # Auto Configure
- org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
- org.springframework.boot.autoconfigure.aop.AopAutoConfiguration,\
- org.springframework.boot.autoconfigure.amqp.RabbitAutoConfiguration,\
- org.springframework.boot.autoconfigure.MessageSourceAutoConfiguration,\
- org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration,\
- org.springframework.boot.autoconfigure.batch.BatchAutoConfiguration,\
- org.springframework.boot.autoconfigure.cloud.CloudAutoConfiguration,\
- org.springframework.boot.autoconfigure.dao.PersistenceExceptionTranslationAutoConfiguration,\
- org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchRepositoriesAutoConfiguration,\
- org.springframework.boot.autoconfigure.data.jpa.JpaRepositoriesAutoConfiguration,\
- org.springframework.boot.autoconfigure.data.mongo.MongoRepositoriesAutoConfiguration,\
- org.springframework.boot.autoconfigure.data.solr.SolrRepositoriesAutoConfiguration,\
- org.springframework.boot.autoconfigure.data.rest.RepositoryRestMvcAutoConfiguration,\
- org.springframework.boot.autoconfigure.data.web.SpringDataWebAutoConfiguration,\
- org.springframework.boot.autoconfigure.freemarker.FreeMarkerAutoConfiguration,\
- org.springframework.boot.autoconfigure.gson.GsonAutoConfiguration,\
- org.springframework.boot.autoconfigure.hateoas.HypermediaAutoConfiguration,\
- org.springframework.boot.autoconfigure.integration.IntegrationAutoConfiguration,\
- org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration,\
- org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration,\
- org.springframework.boot.autoconfigure.jdbc.JndiDataSourceAutoConfiguration,\
- org.springframework.boot.autoconfigure.jdbc.XADataSourceAutoConfiguration,\
- org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration,\
- org.springframework.boot.autoconfigure.jms.JmsAutoConfiguration,\
- org.springframework.boot.autoconfigure.jmx.JmxAutoConfiguration,\
- org.springframework.boot.autoconfigure.jms.JndiConnectionFactoryAutoConfiguration,\
- org.springframework.boot.autoconfigure.jms.activemq.ActiveMQAutoConfiguration,\
- org.springframework.boot.autoconfigure.jms.hornetq.HornetQAutoConfiguration,\
- org.springframework.boot.autoconfigure.jta.JtaAutoConfiguration,\
- org.springframework.boot.autoconfigure.elasticsearch.ElasticsearchAutoConfiguration,\
- org.springframework.boot.autoconfigure.elasticsearch.ElasticsearchDataAutoConfiguration,\
- org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration,\
- org.springframework.boot.autoconfigure.groovy.template.GroovyTemplateAutoConfiguration,\
- org.springframework.boot.autoconfigure.jersey.JerseyAutoConfiguration,\
- org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration,\
- org.springframework.boot.autoconfigure.mail.MailSenderAutoConfiguration,\
- org.springframework.boot.autoconfigure.mobile.DeviceResolverAutoConfiguration,\
- org.springframework.boot.autoconfigure.mobile.DeviceDelegatingViewResolverAutoConfiguration,\
- org.springframework.boot.autoconfigure.mobile.SitePreferenceAutoConfiguration,\
- org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration,\
- org.springframework.boot.autoconfigure.mongo.MongoDataAutoConfiguration,\
- org.springframework.boot.autoconfigure.mustache.MustacheAutoConfiguration,\
- org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration,\
- org.springframework.boot.autoconfigure.reactor.ReactorAutoConfiguration,\
- org.springframework.boot.autoconfigure.redis.RedisAutoConfiguration,\
- org.springframework.boot.autoconfigure.security.SecurityAutoConfiguration,\
- org.springframework.boot.autoconfigure.security.FallbackWebSecurityAutoConfiguration,\
- org.springframework.boot.autoconfigure.social.SocialWebAutoConfiguration,\
- org.springframework.boot.autoconfigure.social.FacebookAutoConfiguration,\
- org.springframework.boot.autoconfigure.social.LinkedInAutoConfiguration,\
- org.springframework.boot.autoconfigure.social.TwitterAutoConfiguration,\
- org.springframework.boot.autoconfigure.solr.SolrAutoConfiguration,\
- org.springframework.boot.autoconfigure.velocity.VelocityAutoConfiguration,\
- org.springframework.boot.autoconfigure.thymeleaf.ThymeleafAutoConfiguration,\
- org.springframework.boot.autoconfigure.web.DispatcherServletAutoConfiguration,\
- org.springframework.boot.autoconfigure.web.EmbeddedServletContainerAutoConfiguration,\
- org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration,\
- org.springframework.boot.autoconfigure.web.GzipFilterAutoConfiguration,\
- org.springframework.boot.autoconfigure.web.HttpEncodingAutoConfiguration,\
- org.springframework.boot.autoconfigure.web.HttpMessageConvertersAutoConfiguration,\
- org.springframework.boot.autoconfigure.web.MultipartAutoConfiguration,\
- org.springframework.boot.autoconfigure.web.ServerPropertiesAutoConfiguration,\
- org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration,\
- org.springframework.boot.autoconfigure.websocket.WebSocketAutoConfiguration
以上为Spring Boot中所有的自动配置相关类;在启动过程中会解析对应类配置信息,以RabbitMQ为例,则会去解析RabbitAutoConfiguration
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会放在第一位,实在是太懂礼貌了~~
- @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;
- }
- }
- @Bean
- @ConditionalOnMissingBean(RabbitTemplate.class)
- public RabbitTemplate rabbitTemplate() {
- return new RabbitTemplate(this.connectionFactory);
- }
创建了默认的RabbitTemplate;下面创建的RabbitMessagingTemplate实现对RabbitTemplate的包装。
spring boot实战(第十三篇)自动配置原理分析的更多相关文章
- Spring boot - 梳理 - 根本上说,Spring Boot项目只不过是一个普通的Spring项目,只是使用了Spring Boot的起步依赖和自动配置
根本上说,Spring Boot项目只不过是一个普通的Spring项目,只是使用了Spring Boot的起步依赖和自动配置
- Spring Boot实战系列(7)集成Consul配置中心
本篇主要介绍了 Spring Boot 如何与 Consul 进行集成,Consul 只是服务注册的一种实现,还有其它的例如 Zookeeper.Etcd 等,服务注册发现在微服务架构中扮演这一个重要 ...
- Spring Boot数据访问之数据源自动配置
Spring Boot提供自动配置的数据访问,首先体验下,Spring Boot使用2.5.5版本: 1)导入坐标: 2.5.25版本支持8.0.26mysql数据库驱动.spring-boot-st ...
- Spring Boot框架 - 数据访问 - JDBC&自动配置
一.新建Spring Boot 工程 特殊勾选数据库相关两个依赖 Mysql Driver — 数据库驱动 Spring Data JDBC 二.配置文件application.properties ...
- (转)spring boot实战(第三篇)事件监听源码分析
原文:http://blog.csdn.net/liaokailin/article/details/48194777 监听源码分析 首先是我们自定义的main方法: package com.lkl. ...
- (转)spring boot实战(第六篇)加载application资源文件源码分析
原文:http://blog.csdn.net/liaokailin/article/details/48878447
- spring boot实战(第十二篇)整合RabbitMQ
前言 最近几篇文章将围绕消息中间件RabbitMQ展开,对于RabbitMQ基本概念这里不阐述,主要讲解RabbitMQ的基本用法.Java客户端API介绍.spring Boot与RabbitMQ整 ...
- Spring Boot自动配置原理与实践(一)
前言 Spring Boot众所周知是为了简化Spring的配置,省去XML的复杂化配置(虽然Spring官方推荐也使用Java配置)采用Java+Annotation方式配置.如下几个问题是我刚开始 ...
- Spring Boot的自动配置原理及启动流程源码分析
概述 Spring Boot 应用目前应该是 Java 中用得最多的框架了吧.其中 Spring Boot 最具特点之一就是自动配置,基于Spring Boot 的自动配置,我们可以很快集成某个模块, ...
随机推荐
- UVA 540 stl
Queues and Priority Queues are data structures which are known to most computer scientists. The Team ...
- Lucene 4.7 --创建索引
Lucene的最新版本和以前的语法或者类名,类规定都相差甚远 0.准备工作: 1). Lucene官方API http://lucene.apache.org/core/4_7_0/index.htm ...
- javaScript基础练习题-下拉框制作(神奇的代码)
http://www.oschina.net/code/snippet_12_46548#66319 http://www.codeproject.com/Tips/890021/Advanced-C ...
- c++ 函数调用在进入下一个循环的时候会再次初始化参数,将函数体直接写进去就正常
#include"stdafx.h" #include"string" #include<iostream> #include<vector& ...
- easyui datagrid 获取 title
function exportExecl(obj) { var cfs = $(obj).datagrid('getColumnFields'); //这是获取到所有的Fields //得到title ...
- 初学JDBC,调用存储过程
在JDBC简单封装的基础上实现 public class UserDao{ public static void testGetUser(String userName) throws Excepti ...
- hdu 2045 不容易系列之(3)—— LELE的RPG难题
解题思路: f(n)=1,2,.....n-2,n-1,n 前n-2个已经涂好,那么n-1有两种可能 1.n-1与n-2和1 的颜色都不同 1 粉, n-2 红, n-1 绿. 那么n的颜色 ...
- Ward BRDF实现心得
最近做了Ward BRDF的实现,相对于之前的lambert,phong来说,Ward是一个真正意义上的各向异性BRDF,但同样的,Ward模型也是一个基于经验的模型,并不是物理上正确的.它由ward ...
- ALTER 语句修改数据表
1.修改数据表名:alter table 表名 rename 新表名; 2.修改列名: alter table 表名 change 列名 新列名(可以与旧的一样) 类型 默认值; 3.修改类型: al ...
- js通过alert查看对象或数组内容
var arr=new Array("Saab","Volvo","BMW"); for(i in arr ){ alert(i); //获 ...