网站建设资讯

NEWS

网站建设资讯

SpringBoot自动配置(auto-configurtion)揭秘

本章,我们为你揭秘Spring Boot自动配置(Auto Configuration)运行机制,谈到auto-configuration,肯定离不开@EnableAutoConfiguration注解。

成都创新互联主要为客户提供服务项目涵盖了网页视觉设计、VI标志设计、成都全网营销推广、网站程序开发、HTML5响应式成都网站建设公司手机网站开发、微商城、网站托管及成都网站维护公司、WEB系统开发、域名注册、国内外服务器租用、视频、平面设计、SEO优化排名。设计、前端、后端三个建站步骤的完善服务体系。一人跟踪测试的建站服务标准。已经为成都地磅秤行业客户提供了网站设计服务。

package org.springframework.boot.autoconfigure;

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
@Import(EnableAutoConfigurationImportSelector.class)
public @interface EnableAutoConfiguration {

   Class[] exclude() default {};
   String[] excludeName() default {};
}

这里涉及了两个元注解: @AutoConfigurationPackage, @Import(EnableAutoConfigurationImportSelector.class),其中@AutoConfigurationPackage定义如下:

package org.springframework.boot.autoconfigure;

import ....

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@Import(AutoConfigurationPackages.Registrar.class)
public @interface AutoConfigurationPackage {

}

@AutoConfigurationPackage注解定义中使用了@Import元注解,注解属性value取值为AutoConfigurationPackages.Registrar.class,AutoConfigurationPackages.Registrar类实现了接口ImportBeanDefinitionRegistrar

@Import注解可以接受以下几种定义类型的Java类

  • 使用@Configuration注解的类
  • ImportSelector实现类:以代码方式处理@Configuration注解类
  • DeferredImportSelector实现类:与ImportSelector类似,区别在于处理操作被延迟到所有其他配置项都处理完毕再进行。
    • ImportBeanDefinitionRegistrar实现类

AutoConfigurationPackages.Registrar会向Spring容器注册Bean,Bean本身会存储用户自定义配置包列表。Spring Boot 本身会使用这个列表。例如:对于spring-boot-autoconfigure数据访问配置类,可以通过静态方法:AutoConfigurationPackages.get(BeanFactory)来获取到这个配置列表,下面是示例代码。

package com.logicbig.example;

import ...

@EnableAutoConfiguration
public class AutoConfigurationPackagesTest {

   public static void main (String[] args) {

       SpringApplication app =
                     new SpringApplication(AutoConfigurationPackagesTest.class);
       app.setBannerMode(Banner.Mode.OFF);
       app.setLogStartupInfo(false);
       ConfigurableApplicationContext c = app.run(args);
       List packages = AutoConfigurationPackages.get(c);
       System.out.println("packages: "+packages);
   }
}

代码输出如下:

2017-01-03 10:17:37.372  INFO 10752 --- [           main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@67b467e9: startup date [Tue Jan 03 10:17:37 CST 2017]; root of context hierarchy
2017-01-03 10:17:38.155  INFO 10752 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
packages: [com.logicbig.example]
2017-01-03 10:17:38.170  INFO 10752 --- [       Thread-1] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@67b467e9: startup date [Tue Jan 03 10:17:37 CST 2017]; root of context hierarchy
2017-01-03 10:17:38.171  INFO 10752 --- [       Thread-1] o.s.j.e.a.AnnotationMBeanExporter        : Unregistering JMX-exposed beans on shutdown

@Import(EnableAutoConfigurationImportSelector.class)注解是auto-configuration 机制的启动入口。EnableAutoConfigurationImportSelector实现了接口DeferredImportSelector,其内部调用了SpringFactoriesLoader.loadFactoryNames()方法,方法会从META-INF/spring.factories中加载配置类。

    protected List getCandidateConfigurations(AnnotationMetadata metadata,
            AnnotationAttributes attributes) {
        List configurations = SpringFactoriesLoader.loadFactoryNames(
                getSpringFactoriesLoaderFactoryClass(), 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;
    }

从spring.factories中查找键值org.springframework.boot.autoconfigure.EnableAutoConfiguration的值:

Spring Boot 自动配置(auto-configurtion) 揭秘

spring-boot-autoconfigure默认隐式包含在所有启动程序中

下面其中的一个配置类JmxAutoConfiguration的代码段

 package org.springframework.boot.autoconfigure.jmx;

   .......
 import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
 import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
 import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
 import org.springframework.boot.autoconfigure.condition.SearchStrategy;
  .....

 @Configuration
 @ConditionalOnClass({ MBeanExporter.class })
 @ConditionalOnProperty(prefix = "spring.jmx", name = "enabled", havingValue = "true", matchIfMissing = true)
 public class JmxAutoConfiguration implements
                                    EnvironmentAware, BeanFactoryAware {
    .....
 }
@ConditionalOnClass

@ConditionalOnClass是由元注解@Conditional(OnClassCondition.class定义的注解,我们知道,@Conditional是条件注解,只有条件为真时,@Conditional注解的类、方法才会被加载到Spring组件容器中。对于上面的实例代码段,只有当MBeanExporter.class已经包含在classpath中(具体校验类似于Class.forName的加载逻辑,当目标类包含在classpath中,方法返回为true,否则返回false),OnClassCondition#matches()才会返回为true。

@ConditionalOnProperty

@ConditionalOnClass类似,@ConditionalOnProperty是另一个@Conditional类型变量,是由元注解@Conditional(OnPropertyCondition.class)所定义的注解。只有当目标属性包含了指定值,OnPropertyCondition#matches()才会返回真,还是上面的代码段:

  @ConditionalOnProperty(prefix = "spring.jmx", name = "enabled",
                         havingValue = "true", matchIfMissing = true)

如果我们应用配置了spring.jmx.enabled=true,那么Spring容器将自动注册JmxAutoConfiguration,matchIfMissing=true表示默认情况下(配置属性未设置)为真。

其他一些条件注解

包‘org.springframework.boot.autoconfigure.condition,所有条件注解均遵循ConditionalOnXyz`命名约定。如果想要开发自定义启动包,你需要了解这些API,对于别的开发人员来说,最好也能了解基本的运行机制。

Spring Boot 自动配置(auto-configurtion) 揭秘

使用–debug参数

@EnableAutoConfiguration
public class DebugModeExample {

  public static void main (String[] args) {
      //just doing this programmatically for demo
      String[] appArgs = {"--debug"};

      SpringApplication app = new SpringApplication(DebugModeExample.class);
      app.setBannerMode(Banner.Mode.OFF);
      app.setLogStartupInfo(false);
      app.run(appArgs);
    }
}

输出

2017-01-02 21:15:17.322 DEBUG 5704 --- [           main] o.s.boot.SpringApplication               : Loading source class com.logicbig.example.DebugModeExample
2017-01-02 21:15:17.379 DEBUG 5704 --- [           main] o.s.b.c.c.ConfigFileApplicationListener  : Skipped (empty) config file 'file:/D:/LogicBig/example-projects/spring-boot/boot-customizing-autoconfig/target/classes/application.properties' (classpath:/application.properties)
2017-01-02 21:15:17.379 DEBUG 5704 --- [           main] o.s.b.c.c.ConfigFileApplicationListener  : Skipped (empty) config file 'file:/D:/LogicBig/example-projects/spring-boot/boot-customizing-autoconfig/target/classes/application.properties' (classpath:/application.properties) for profile default
2017-01-02 21:15:17.384  INFO 5704 --- [           main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@2f0a87b3: startup date [Mon Jan 02 21:15:17 CST 2017]; root of context hierarchy
2017-01-02 21:15:18.032  INFO 5704 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2017-01-02 21:15:18.047 DEBUG 5704 --- [           main] utoConfigurationReportLoggingInitializer :

=========================
AUTO-CONFIGURATION REPORT
=========================

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

   GenericCacheConfiguration matched:
      - Cache org.springframework.boot.autoconfigure.cache.GenericCacheConfiguration automatic cache type (CacheCondition)

   JmxAutoConfiguration matched:
      - @ConditionalOnClass found required class 'org.springframework.jmx.export.MBeanExporter' (OnClassCondition)
      - @ConditionalOnProperty (spring.jmx.enabled=true) matched (OnPropertyCondition)

   JmxAutoConfiguration#mbeanExporter matched:
      - @ConditionalOnMissingBean (types: org.springframework.jmx.export.MBeanExporter; SearchStrategy: current) did not find any beans (OnBeanCondition)

   JmxAutoConfiguration#mbeanServer matched:
      - @ConditionalOnMissingBean (types: javax.management.MBeanServer; SearchStrategy: all) did not find any beans (OnBeanCondition)

   JmxAutoConfiguration#objectNamingStrategy matched:
      - @ConditionalOnMissingBean (types: org.springframework.jmx.export.naming.ObjectNamingStrategy; SearchStrategy: current) did not find any beans (OnBeanCondition)

   NoOpCacheConfiguration matched:
      - Cache org.springframework.boot.autoconfigure.cache.NoOpCacheConfiguration automatic cache type (CacheCondition)

   PropertyPlaceholderAutoConfiguration#propertySourcesPlaceholderConfigurer matched:
      - @ConditionalOnMissingBean (types: org.springframework.context.support.PropertySourcesPlaceholderConfigurer; SearchStrategy: current) did not find any beans (OnBeanCondition)

   redisCacheConfiguration matched:
      - Cache org.springframework.boot.autoconfigure.cache.RedisCacheConfiguration automatic cache type (CacheCondition)

   SimpleCacheConfiguration matched:
      - Cache org.springframework.boot.autoconfigure.cache.SimpleCacheConfiguration automatic cache type (CacheCondition)

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

   ActiveMQAutoConfiguration:
      Did not match:
         - @ConditionalOnClass did not find required classes 'javax.jms.ConnectionFactory', 'org.apache.activemq.ActiveMQConnectionFactory' (OnClassCondition)

   AopAutoConfiguration:
      Did not match:
         - @ConditionalOnClass did not find required classes 'org.aspectj.lang.annotation.Aspect', 'org.aspectj.lang.reflect.Advice' (OnClassCondition)

   ArtemisAutoConfiguration:
      Did not match:
         - @ConditionalOnClass did not find required classes 'javax.jms.ConnectionFactory', 'org.apache.activemq.artemis.jms.client

 ...............................
 ....................

Exclusions:
-----------

    None

Unconditional classes:
----------------------

    org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration

    org.springframework.boot.autoconfigure.web.WebClientAutoConfiguration

    org.springframework.boot.autoconfigure.context.ConfigurationPropertiesAutoConfiguration

    org.springframework.boot.autoconfigure.info.ProjectInfoAutoConfiguration

2017-01-02 21:15:18.058  INFO 5704 --- [       Thread-1] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@2f0a87b3: startup date [Mon Jan 02 21:15:17 CST 2017]; root of context hierarchy
2017-01-02 21:15:18.059  INFO 5704 --- [       Thread-1] o.s.j.e.a.AnnotationMBeanExporter        : Unregistering JMX-exposed beans on shutdown

在上面的输出中

  • Positive matches:@Conditional条件为真,配置类被Spring容器加载。
  • Negative matches: @Conditional条件为假,配置类未被Spring容器加载。
  • Exclusions: 应用端明确排除加载配置
  • Unconditional classes: 自动配置类不包含任何类级别的条件,也就是说,类始终会被自动加载。
禁止特定类的auto-configuration
@EnableAutoConfiguration(exclude = {JmxAutoConfiguration.class})
public class ExcludeConfigExample {

    public static void main (String[] args) {
         //just doing this programmatically for demo
         String[] appArgs = {"--debug"};

        SpringApplication app = new SpringApplication(ExcludeConfigExample.class);
        app.setBannerMode(Banner.Mode.OFF);
        app.setLogStartupInfo(false);
        app.run(appArgs);
    }
}

输出


  .............

=========================
AUTO-CONFIGURATION REPORT
=========================

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

   GenericCacheConfiguration matched:
      - Cache org.springframework.boot.autoconfigure.cache.GenericCacheConfiguration automatic cache type (CacheCondition)

   NoOpCacheConfiguration matched:
      - Cache org.springframework.boot.autoconfigure.cache.NoOpCacheConfiguration automatic cache type (CacheCondition)

   PropertyPlaceholderAutoConfiguration#propertySourcesPlaceholderConfigurer matched:
      - @ConditionalOnMissingBean (types: org.springframework.context.support.PropertySourcesPlaceholderConfigurer; SearchStrategy: current) did not find any beans (OnBeanCondition)

   RedisCacheConfiguration matched:
      - Cache org.springframework.boot.autoconfigure.cache.RedisCacheConfiguration automatic cache type (CacheCondition)

   SimpleCacheConfiguration matched:
      - Cache org.springframework.boot.autoconfigure.cache.SimpleCacheConfiguration automatic cache type (CacheCondition)

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

   ActiveMQAutoConfiguration:
      Did not match:
         - @ConditionalOnClass did not find required classes 'javax.jms.ConnectionFactory', 'org.apache.activemq.ActiveMQConnectionFactory' (OnClassCondition)

   AopAutoConfiguration:
      Did not match:
         - @ConditionalOnClass did not find required classes 'org.aspectj.lang.annotation.Aspect', 'org.aspectj.lang.reflect.Advice' (OnClassCondition)

  .................................

Exclusions:
-----------

    org.springframework.boot.autoconfigure.jmx.JmxAutoConfiguration

Unconditional classes:
----------------------

    org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration

    org.springframework.boot.autoconfigure.web.WebClientAutoConfiguration

    org.springframework.boot.autoconfigure.context.ConfigurationPropertiesAutoConfiguration

    org.springframework.boot.autoconfigure.info.ProjectInfoAutoConfiguration

标题名称:SpringBoot自动配置(auto-configurtion)揭秘
网站路径:http://cdweb.net/article/jggsce.html