当前位置: 首页 > news >正文

SpringBoot之@EnableAutoConfiguration原理及自定义扩展

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

spring Boot是一个偏执的开源框架,它可用于创建可执行的Spring应用程序,采用了习惯优于配置的方法。 
此框架的神奇之处在于@EnableAutoConfiguration注释,此注释自动载入应用程序所需的所有Bean——这依赖于Spring Boot在类路径中的查找。

一、@Enable*注释

@Enable*注释并不是新发明的注释,早在Spring 3框架就引入了这些注释,用这些注释替代XML配置文件。 
很多Spring开发者都知道@EnableTransactionManagement注释,它能够声明事务管理;@EnableWebMvc注释,它能启用Spring MVC;以及@EnableScheduling注释,它可以初始化一个调度器。 
这些注释事实上都是简单的配置,通过@Import注释导入。

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import({ EnableAutoConfigurationImportSelector.class,
        AutoConfigurationPackages.Registrar.class })
public @interface EnableAutoConfiguration {

    /**
     * Exclude specific auto-configuration classes such that they will never be applied.
     */
    Class<?>[] exclude() default {};

}

EnableAutoConfigurationImportSelector类使用了Spring Core包的SpringFactoriesLoader类的loadFactoryNamesof()方法。 
SpringFactoriesLoader会查询META-INF/spring.factories文件中包含的JAR文件。 
当找到spring.factories文件后,SpringFactoriesLoader将查询配置文件命名的属性。在例子中,是org.springframework.boot.autoconfigure.EnableAutoConfiguration。 
让我们来看看spring-boot-autoconfigure JAR文件,它真的包含了一个spring.factories文件,内容如下:

# Initializers
org.springframework.context.ApplicationContextInitializer=\
org.springframework.boot.autoconfigure.logging.AutoConfigurationReportLoggingInitializer

# 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.data.JpaRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.MongoRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.redis.RedisAutoConfiguration,\
org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration,\
org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration,\
org.springframework.boot.autoconfigure.jms.JmsTemplateAutoConfiguration,\
org.springframework.boot.autoconfigure.jmx.JmxAutoConfiguration,\
org.springframework.boot.autoconfigure.mobile.DeviceResolverAutoConfiguration,\
org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration,\
org.springframework.boot.autoconfigure.mongo.MongoTemplateAutoConfiguration,\
org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration,\
org.springframework.boot.autoconfigure.reactor.ReactorAutoConfiguration,\
org.springframework.boot.autoconfigure.security.SecurityAutoConfiguration,\
org.springframework.boot.autoconfigure.security.FallbackWebSecurityAutoConfiguration,\
org.springframework.boot.autoconfigure.thymeleaf.ThymeleafAutoConfiguration,\
org.springframework.boot.autoconfigure.web.EmbeddedServletContainerAutoConfiguration,\
org.springframework.boot.autoconfigure.web.DispatcherServletAutoConfiguration,\
org.springframework.boot.autoconfigure.web.ServerPropertiesAutoConfiguration,\
org.springframework.boot.autoconfigure.web.MultipartAutoConfiguration,\
org.springframework.boot.autoconfigure.web.HttpMessageConvertersAutoConfiguration,\
org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration,\
org.springframework.boot.autoconfigure.websocket.WebSocketAutoConfiguration

在这个文件中,可以看到一系列Spring Boot自动配置的列表。下面我们来看这些配置的细节,以MongoAutoConfiguration为例:

@Configuration
@ConditionalOnClass(Mongo.class)
@EnableConfigurationProperties(MongoProperties.class)
public class MongoAutoConfiguration {

    @Autowired
    private MongoProperties properties;

    private Mongo mongo;

    @PreDestroy
    public void close() throws UnknownHostException {
        if (this.mongo != null) {
            this.mongo.close();
        }
    }

    @Bean
    @ConditionalOnMissingBean
    public Mongo mongo() throws UnknownHostException {
        this.mongo = this.properties.createMongoClient();
        return this.mongo;
    }

}

这个类进行了简单的Spring配置,声明了MongoDB所需典型Bean。 
这个类跟其它很多类一样,重度依赖于Spring Boot注释: 
1)@ConditionOnClass激活一个配置,在类路径中只能存在一到几个这样的类。 
2)@EnableConfigurationProperties自动映射一个POJO到Spring Boot配置文件(默认是application.properties文件)的属性集。 
3)@ConditionalOnMissingBean启用一个Bean定义,但必须是这个Bean之前未定义过才有效。 
还可以使用@ AutoConfigureBefore注释、@AutoConfigureAfter注释来定义这些配置类的载入顺序。

二、属性映射

下面看MongoProperties类,它是一个Spring Boot属性映射的例子:

@ConfigurationProperties(prefix = "spring.data.mongodb")
public class MongoProperties {

    private String host;
    private int port = DBPort.PORT;
    private String uri = "mongodb://localhost/test";
    private String database;

    // ... getters/ setters omitted
}

@ConfigurationProperties注释将POJO关联到指定前缀的每一个属性。例如,spring.data.mongodb.port属性将映射到这个类的端口属性。 
强烈建议Spring Boot开发者使用这种方式来删除与配置属性相关的瓶颈代码。

三、@Conditional注释

Spring Boot的强大之处在于使用了Spring 4框架的新特性:@Conditional注释,此注释使得只有在特定条件满足时才启用一些配置。 
在Spring Boot的org.springframework.boot.autoconfigure.condition包中说明了使用@Conditional注释能给我们带来什么,下面对这些注释做一个概述:

  • @ConditionalOnBean
  • @ConditionalOnClass
  • @ConditionalOnExpression
  • @ConditionalOnMissingBean
  • @ConditionalOnMissingClass
  • @ConditionalOnNotWebApplication
  • @ConditionalOnResource
  • @ConditionalOnWebApplication

以@ConditionalOnExpression注释为例,它允许在Spring的EL表达式中写一个条件。

@Conditional(OnExpressionCondition.class)
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.TYPE, ElementType.METHOD })
public @interface ConditionalOnExpression {

    /**
     * The SpEL expression to evaluate. Expression should return {@code true} if the
     * condition passes or {@code false} if it fails.
     */
    String value() default "true";

}

在这个类中,我们想利用@Conditional注释,条件在OnExpressionCondition类中定义:

public class OnExpressionCondition extends SpringBootCondition {

    @Override
    public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) {
        // ...
        // we first get a handle on the EL context via the ConditionContext

        boolean result = (Boolean) resolver.evaluate(expression, expressionContext);

        // ...
        // here we create a message the user will see when debugging

        return new ConditionOutcome(result, message.toString());
    }
}

在最后,@Conditional通过简单的布尔表达式(即ConditionOutcome方法)来决定。

四、应用程序上下文初始化器

spring.factories还提供了第二种可能性,即定义应用程序的初始化。这使得我们可以在应用程序载入前操纵Spring的应用程序上下文ApplicationContext。 
特别是,可以在上下文创建监听器,使用ConfigurableApplicationContext类的addApplicationListener()方法。 
AutoConfigurationReportLoggingInitializer监听到系统事件时,比如上下文刷新或应用程序启动故障之类的事件,Spring Boot可以执行一些工作。这有助于我们以调试模式启动应用程序时创建自动配置的报告。 
要以调试模式启动应用程序,可以使用-Ddebug标识,或者在application.properties文件这添加属性debug= true。

五、调试Spring Boot自动配置

Spring Boot的官方文档(http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#howto-troubleshoot-auto-configuration)有助于理解自动配置期间发生了什么。当以调试模式运行时,Spring Boot会产生一个报告,如下:

Positive matches:
-----------------

   MessageSourceAutoConfiguration
      - @ConditionalOnMissingBean (types: org.springframework.context.MessageSource; SearchStrategy: all) found no beans (OnBeanCondition)

   JmxAutoConfiguration
      - @ConditionalOnClass classes found: org.springframework.jmx.export.MBeanExporter (OnClassCondition)
      - SpEL expression on org.springframework.boot.autoconfigure.jmx.JmxAutoConfiguration: ${spring.jmx.enabled:true} (OnExpressionCondition)
      - @ConditionalOnMissingBean (types: org.springframework.jmx.export.MBeanExporter; SearchStrategy: all) found no beans (OnBeanCondition)

   DispatcherServletAutoConfiguration
      - found web application StandardServletEnvironment (OnWebApplicationCondition)
      - @ConditionalOnClass classes found: org.springframework.web.servlet.DispatcherServlet (OnClassCondition)


Negative matches:
-----------------

   DataSourceAutoConfiguration
      - required @ConditionalOnClass classes not found: org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType (OnClassCondition)

   DataSourceTransactionManagerAutoConfiguration
      - required @ConditionalOnClass classes not found: org.springframework.jdbc.core.JdbcTemplate,org.springframework.transaction.PlatformTransactionManager (OnClassCondition)

   MongoAutoConfiguration
      - required @ConditionalOnClass classes not found: com.mongodb.Mongo (OnClassCondition)

   FallbackWebSecurityAutoConfiguration
      - SpEL expression on org.springframework.boot.autoconfigure.security.FallbackWebSecurityAutoConfiguration: !${security.basic.enabled:true} (OnExpressionCondition)

   SecurityAutoConfiguration
      - required @ConditionalOnClass classes not found: org.springframework.security.authentication.AuthenticationManager (OnClassCondition)

   EmbeddedServletContainerAutoConfiguration.EmbeddedJetty
      - required @ConditionalOnClass classes not found: org.eclipse.jetty.server.Server,org.eclipse.jetty.util.Loader (OnClassCondition)

   WebMvcAutoConfiguration.WebMvcAutoConfigurationAdapter#localeResolver
      - @ConditionalOnMissingBean (types: org.springframework.web.servlet.LocaleResolver; SearchStrategy: all) found no beans (OnBeanCondition)
      - SpEL expression: '${spring.mvc.locale:}' != '' (OnExpressionCondition)

   WebSocketAutoConfiguration
      - required @ConditionalOnClass classes not found: org.springframework.web.socket.WebSocketHandler,org.apache.tomcat.websocket.server.WsSci (OnClassCondition)

对于每个自动配置,可以看到它启动或失败的原因。

六、结论

Spring Boot很好地利用了Spring 4框架的功能,可以创建一个自动配置的可执行JAR。 
不要忘记,可以使用自己的配置来替代自动配置,见: http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#using-boot-replacing-auto-configuration 
over!

转载于:https://my.oschina.net/xiaominmin/blog/1791616

相关文章:

  • CentOS 6.5 安全加固
  • python之路——常用模块
  • 排序算法之选择排序
  • PostgreSQL入门及提权
  • 面向对象1
  • Lambda表达式(Java)
  • 区块链将会怎样颠覆Google、Amazon、Facebook和Apple?
  • ECMAScript 6 学习之路 ( 四 ) String 字符串扩展
  • Windows Server 2012的服务管理自动化 -启动类型设置,手动启动还是自动启动
  • JVM 组成以及各部分作用
  • PHP 500报错的快速解决方法
  • windows网络模型之完成端口(CompletionPort)详解 (转)
  • [转]区块链代码快速学习实践
  • 《王牌特工2》情景再现,Youbionic推出可穿戴式机械手
  • 扩展GenericServlet实现Servlet程序 学习笔记
  • Apache Pulsar 2.1 重磅发布
  • Facebook AccountKit 接入的坑点
  • Git 使用集
  • Git学习与使用心得(1)—— 初始化
  • Java 网络编程(2):UDP 的使用
  • JavaScript服务器推送技术之 WebSocket
  • Nacos系列:Nacos的Java SDK使用
  • Node 版本管理
  • nodejs:开发并发布一个nodejs包
  • Python连接Oracle
  • Spark RDD学习: aggregate函数
  • 关键词挖掘技术哪家强(一)基于node.js技术开发一个关键字查询工具
  • 官方新出的 Kotlin 扩展库 KTX,到底帮你干了什么?
  • 简析gRPC client 连接管理
  • 解决jsp引用其他项目时出现的 cannot be resolved to a type错误
  • 类orAPI - 收藏集 - 掘金
  • 理解 C# 泛型接口中的协变与逆变(抗变)
  • 项目实战-Api的解决方案
  • 消息队列系列二(IOT中消息队列的应用)
  • 移动端 h5开发相关内容总结(三)
  • 异常机制详解
  • 因为阿里,他们成了“杭漂”
  • postgresql行列转换函数
  • 仓管云——企业云erp功能有哪些?
  • 通过调用文摘列表API获取文摘
  • ​configparser --- 配置文件解析器​
  • ​DB-Engines 11月数据库排名:PostgreSQL坐稳同期涨幅榜冠军宝座
  • (阿里云万网)-域名注册购买实名流程
  • (初研) Sentence-embedding fine-tune notebook
  • (二)Linux——Linux常用指令
  • (二)基于wpr_simulation 的Ros机器人运动控制,gazebo仿真
  • (十二)springboot实战——SSE服务推送事件案例实现
  • (收藏)Git和Repo扫盲——如何取得Android源代码
  • (一)WLAN定义和基本架构转
  • (转)大道至简,职场上做人做事做管理
  • (转载)PyTorch代码规范最佳实践和样式指南
  • (转载)微软数据挖掘算法:Microsoft 时序算法(5)
  • .NET 4.0中使用内存映射文件实现进程通讯
  • .NET Core 中的路径问题
  • .net web项目 调用webService