beanfactory的实现类_面试官:SpringBoot是如何实现自动配置?

点击蓝色“java版web项目”关注我哟

加个“星标”,优质文章,第一时间送达

0296932ca97294c5dcc65824e69e5fb4.png

作者:编程Crazy

blog.51cto.com/14226230/2421423(点解阅读全文前往)

上一篇:这300G的Java资料是我师傅当年给我的,免费分享给大家

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

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的值:

7ae653ac3093cb85a77c37543754fcf9.png

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,对于别的开发人员来说,最好也能了解基本的运行机制。

9b6e997fc9b52707eeda0bfbace78d21.png

使用–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
说句题外话,有不少人想加老赵的微信,姑且放出来,但是坑位有限哦

ed38f7e7545ca33de7c2f609377e8bf8.png

猜你喜欢

1、我是如何把3000行代码重构成15行

2、2019编程语言就业榜:告诉你该学那种语言

3、不敢相信?System.currentTimeMillis()存在性能问题

4、理解 IntelliJ IDEA 的项目配置和 Web 部署

5、干货!一份超详细的Spring Boot知识清单

6、集群、限流、缓存 BAT 大厂无非也就是这么做

7、详解 Tomcat 的连接数与线程池

8、你向 MySQL 数据库插入 100w 条数据用了多久?

强烈推荐一位大佬的公众号

a992b46ceb7522113851562f30b091c5.png

好文章,我在看

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值