这里开始分析SpringBoot如何让spring扫描并自动装配。
BeanDefition
这里需要提到Spring一个重要知识点—BeanDefition。Spring实例化bean是一个复杂的过程,不仅仅金是new出实例,Bean有很多属性例如作用域、懒加载、别名等。Spring通过BeanDefition记录bean构造时的属性值、构造函数参数值以及具体实现提供的进一步信息。
核心-SpringBoot扫描bean
Spring 启动过程首先扫描获取所有BeanDefition,放入一个map中,然后执行初始化步骤,将所有单例bean初始化(实例化)并放入容器中。
而SpringBoot的自动装配,即是在扫描阶段,把需要自动装配的bean加入BeanDefition Map中。然后Spring自动初始化这些bean。
SpringBoot如何介入SpringBean扫描阶段,通过扫描获取到这些用户没有配置但会自动装配的BeanDefition 呢?
下面是Spring容器Context刷新的源码,仅看名字,貌似也看不出来哪个方法执行了bean扫描。每个方法点进去都是庞大的一堆代码,好在这段代码结构比较清晰,我们通过断点一步步查看,到底哪个方法扫描了需要自动装配的BeanDefition 。
@Override
public void refresh() throws BeansException, IllegalStateException {
synchronized (this.startupShutdownMonitor) {
// Prepare this context for refreshing.
prepareRefresh();
// Tell the subclass to refresh the internal bean factory.
ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();
// Prepare the bean factory for use in this context.
prepareBeanFactory(beanFactory);
try {
// Allows post-processing of the bean factory in context subclasses.
postProcessBeanFactory(beanFactory);
// Invoke factory processors registered as beans in the context.
invokeBeanFactoryPostProcessors(beanFactory);
// Register bean processors that intercept bean creation.
registerBeanPostProcessors(beanFactory);
// Initialize message source for this context.
initMessageSource();
// Initialize event multicaster for this context.
initApplicationEventMulticaster();
// Initialize other special beans in specific context subclasses.
onRefresh();
// Check for listener beans and register them.
registerListeners();
// Instantiate all remaining (non-lazy-init) singletons.
finishBeanFactoryInitialization(beanFactory);
// Last step: publish corresponding event.
finishRefresh();
}
catch (BeansException ex) {
if (logger.isWarnEnabled()) {
logger.warn("Exception encountered during context initialization - " +
"cancelling refresh attempt: " + ex);
}
// Destroy already created singletons to avoid dangling resources.
destroyBeans();
// Reset 'active' flag.
cancelRefresh(ex);
// Propagate exception to caller.
throw ex;
}
finally {
// Reset common introspection caches in Spring's core, since we
// might not ever need metadata for singleton beans anymore...
resetCommonCaches();
}
}
}
执行完postProcessBeanFactory方法后,beanDefinitionMap中存在6个BeanDefition,除了入口类,都是processor,显然不对,继续走。。
执行完invokeBeanFactoryPostProcessors方法,beanDefinitionMap中的对象暴增到124了,其中还有我自己定义的一个bean myConfig。
可猜测invokeBeanFactoryPostProcessors承载了SpringBoot接入bean扫描的工作,看方法描述主要作用是实例化并调用所有已注册的BeanFactoryPostProcessor bean。
invokeBeanFactoryPostProcessors方法比较长,大致的处理逻辑如下:
分离出BeanDefinitionRegistryPostProcessor和BeanFactoryPostProcessor类型的处理器,优先对BeanDefinitionRegistryPostProcessor类型处理,然后处理BeanFactoryPostProcessor类型,且每次都按照优先级及处理,优先级为:
实现接口PriorityOrdered > 实现接口Ordered > 剩下的
而符合BeanDefinitionRegistryPostProcessor且实现接口PriorityOrdered的处理器为:
org.springframework.context.annotation.internalConfigurationAnnotationProcessor
这是容器创建时AnnotatedBeanDefinitionReader自动添加了其对应的BeanDefinition,对应的class为ConfigurationClassPostProcessor。
ConfigurationClassPostProcessor是一个重量级的处理器,加载所有Configuration配置类
public class ConfigurationClassPostProcessor implements BeanDefinitionRegistryPostProcessor,
PriorityOrdered, ResourceLoaderAware, BeanClassLoaderAware, EnvironmentAware
按照上述逻辑,ConfigurationClassPostProcessor是最早被执行的,因为它既是BeanDefinitionRegistryPostProcessor类型,而且实现了PriorityOrdered接口,所以首先对其进行实例化。
然后开始执行BeanDefinition的发现与注册,发现步骤用于解析获取Configuration中定义了哪些获取bean的方式,注册步骤用于将所有定义的bean转化注册,会立即注册扫描到的Configuration类。
BeanDefinition发现
- 使用ConfigurationClassParser工具类转化配置类(如SpringBoot项目入口main方法的类)。
- 解析配置类的元数据处理转化为ConfigurationClass,doProcessConfigurationClass,包括@PropertySource、@ComponentScan、@Import(延时处理)、@ImportResource、@Bean及接口方法等,基本的configuration已经处理完毕
。 - 处理@Import涉及的所有配置的Configuration,SpringBoot项目时使用EnableAutoConfiguration.AutoConfigurationImportSelector将META-INF/spring.factories下的所有自动注入类,用于接下来转化为ConfigurationClass,这个注解是在入口类的SpringBootApplication里的EnableAutoConfiguration注解中设置的。
/**
* Return the auto-configuration class names that should be considered. By default
* this method will load candidates using {@link SpringFactoriesLoader} with
* {@link #getSpringFactoriesLoaderFactoryClass()}.
* @param metadata the source metadata
* @param attributes the {@link #getAttributes(AnnotationMetadata) annotation
* attributes}
* @return a list of candidate configurations
*/
protected List<String> getCandidateConfigurations(AnnotationMetadata metadata, AnnotationAttributes attributes) {
List<String> 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;
}
BeanDefinition条件判断与跳过
上一步中获取需要注入的类后,执行ConfigurationClassParser.parse方法,将所有扫描到的configurationClass进一步处理,判断是否需要转化,如需要则递归获取configuration类。
protected void processConfigurationClass(ConfigurationClass configClass, Predicate<String> filter) throws IOException {
if (this.conditionEvaluator.shouldSkip(configClass.getMetadata(), ConfigurationPhase.PARSE_CONFIGURATION)) {
return;
}
ConfigurationClass existingClass = this.configurationClasses.get(configClass);
if (existingClass != null) {
if (configClass.isImported()) {
if (existingClass.isImported()) {
existingClass.mergeImportedBy(configClass);
}
// Otherwise ignore new imported config class; existing non-imported class overrides it.
return;
}
else {
// Explicit bean definition found, probably replacing an import.
// Let's remove the old one and go with the new one.
this.configurationClasses.remove(configClass);
this.knownSuperclasses.values().removeIf(configClass::equals);
}
}
// Recursively process the configuration class and its superclass hierarchy.
SourceClass sourceClass = asSourceClass(configClass, filter);
do {
sourceClass = doProcessConfigurationClass(configClass, sourceClass, filter);
}
while (sourceClass != null);
this.configurationClasses.put(configClass, configClass);
}
这就是springboot最核心的东西了,上面代码中的判断是否跳过,基于Spring条件注解,满足注解条件则会加载类,最终定义的bean会被加入到BeanDefinitionMap中,最终会自动装配,否则会被忽略。
条件判断
条件判断的支持来源于autoconfiguration包,判断是否是web项目,判断是否存在配置,判断是否存在类等。
如下示例,如果配置文件中配置了“myconfig”则类会被加载并实例化到容器中,否则不会。
/**
* <p></p>
*
* @author nec
* @since 2020-10-09 15:18
*/
@Component
@ConditionalOnProperty({"myconfig"})
public class MyConfig {
@Bean
public TestUtil getTestUtil() {
System.out.println("============>start init getTestUtil!!!");
return new TestUtil();
}
}
如果判断条件是否满足呢,根据类头上的条件注解,就像上述类,注解了@ConditionalOnProperty({“myconfig”}),那么判断这个类是否可以加载就在于condition.matches能否匹配到Property中存在"myconfig"这个条件,存在就返回true,这个类就会执行转化操作最终获取beanDefination,否则跳过处理下一个configuration。
/**
* Determine if an item should be skipped based on {@code @Conditional} annotations.
* @param metadata the meta data
* @param phase the phase of the call
* @return if the item should be skipped
*/
public boolean shouldSkip(@Nullable AnnotatedTypeMetadata metadata, @Nullable ConfigurationPhase phase) {
if (metadata == null || !metadata.isAnnotated(Conditional.class.getName())) {
return false;
}
if (phase == null) {
if (metadata instanceof AnnotationMetadata &&
ConfigurationClassUtils.isConfigurationCandidate((AnnotationMetadata) metadata)) {
return shouldSkip(metadata, ConfigurationPhase.PARSE_CONFIGURATION);
}
return shouldSkip(metadata, ConfigurationPhase.REGISTER_BEAN);
}
List<Condition> conditions = new ArrayList<>();
for (String[] conditionClasses : getConditionClasses(metadata)) {
for (String conditionClass : conditionClasses) {
Condition condition = getCondition(conditionClass, this.context.getClassLoader());
conditions.add(condition);
}
}
AnnotationAwareOrderComparator.sort(conditions);
for (Condition condition : conditions) {
ConfigurationPhase requiredPhase = null;
if (condition instanceof ConfigurationCondition) {
requiredPhase = ((ConfigurationCondition) condition).getConfigurationPhase();
}
if ((requiredPhase == null || requiredPhase == phase) && !condition.matches(this.context, metadata)) {
return true;
}
}
return false;
}
再进一步,怎么匹配的?如果是ConditionalOnProperty条件,则由对应的OnPropertyCondition处理,会根据条件值,从environment中找是否存在对应的配置。找到返回匹配,否则不匹配!
示例-自动开启aop
最后来看一个完整的自动装配示例,aop如何自动启用。
Spring项目如果需要开启AOP,需要手动设置@EnableAspectJAutoProxy。
而SpringBoot AopAutoConfiguration默认是支持自动启用aop的,因为matchIfMissing =true表示即使未配置spring.aop.auto,也会匹配,如果要关闭自动配置,则需要设置spring.aop.auto=false
当存在Advice.class时,AspectJAutoProxyingConfiguration 会自动启用,然后根据spring.aop.proxy-target-class是否配置来决定使用jdk动态代理还是cglib动态代理,可以看到
JdkDynamicAutoProxyConfiguration 的匹配matchIfMissing = false,表示条件匹配不到不生效
CglibAutoProxyConfiguration 的匹配matchIfMissing = true,表示条件匹配不到也会生效
所以springboot默认启用的是cglib动态代理!
看下源码,注释表示自动开启aop相当于手动启用@EnableAspectJAutoProxy。
Equivalent to enabling {@link EnableAspectJAutoProxy @EnableAspectJAutoProxy} in your configuration.
/**
* {@link org.springframework.boot.autoconfigure.EnableAutoConfiguration
* Auto-configuration} for Spring's AOP support. Equivalent to enabling
* {@link EnableAspectJAutoProxy @EnableAspectJAutoProxy} in your configuration.
* <p>
* The configuration will not be activated if {@literal spring.aop.auto=false}. The
* {@literal proxyTargetClass} attribute will be {@literal true}, by default, but can be
* overridden by specifying {@literal spring.aop.proxy-target-class=false}.
*
* @author Dave Syer
* @author Josh Long
* @since 1.0.0
* @see EnableAspectJAutoProxy
*/
@Configuration(proxyBeanMethods = false)
@ConditionalOnProperty(prefix = "spring.aop", name = "auto", havingValue = "true", matchIfMissing = true)
public class AopAutoConfiguration {
@Configuration(proxyBeanMethods = false)
@ConditionalOnClass(Advice.class)
static class AspectJAutoProxyingConfiguration {
@Configuration(proxyBeanMethods = false)
@EnableAspectJAutoProxy(proxyTargetClass = false)
@ConditionalOnProperty(prefix = "spring.aop", name = "proxy-target-class", havingValue = "false",
matchIfMissing = false)
static class JdkDynamicAutoProxyConfiguration {
}
@Configuration(proxyBeanMethods = false)
@EnableAspectJAutoProxy(proxyTargetClass = true)
@ConditionalOnProperty(prefix = "spring.aop", name = "proxy-target-class", havingValue = "true",
matchIfMissing = true)
static class CglibAutoProxyConfiguration {
}
}
@Configuration(proxyBeanMethods = false)
@ConditionalOnMissingClass("org.aspectj.weaver.Advice")
@ConditionalOnProperty(prefix = "spring.aop", name = "proxy-target-class", havingValue = "true",
matchIfMissing = true)
static class ClassProxyingConfiguration {
ClassProxyingConfiguration(BeanFactory beanFactory) {
if (beanFactory instanceof BeanDefinitionRegistry) {
BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory;
AopConfigUtils.registerAutoProxyCreatorIfNecessary(registry);
AopConfigUtils.forceAutoProxyCreatorToUseClassProxying(registry);
}
}
}
}
下面来验证下
首先引入aop的starter
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
编写aop切面代码
aop配置类
@Aspect
@Component
public class AopTest {
@Before("execution(* cn.nec.test.util.TestUtil.sayHi())")
public void around() {
System.out.println("aop before");
}
}
被切的类
@Component
public class TestUtil {
public void sayHi() {
System.out.println("Hi!");
}
}
测试类
@RestController
@RequestMapping("/test")
public class TestController {
@Autowired
public TestUtil testUtil;
@GetMapping("/test")
public String test(){
testUtil.sayHi();
return "success";
}
}
- 配置文件不做任何配置直接启动,访问接口http://127.0.0.1:8080/test/test,结果如下
- 手动关闭aop自动配置,再次测试,结果如下
- 关闭aop自动配置后,手动开启aop,结果如下
- 总结:springboot aop自动装配相当于帮我们自动设置了@EnableAspectJAutoProxy,而如果不引入aop-starter,则需要手动引入aop依赖jar的maven配置,并且设置@EnableAspectJAutoProxy