Spring中@DependsOn注解 Spring中@DependsOn注解的作用及实现原理解析
brucelwl 人气:11本文给大家讲解Spring中@DependsOn注解的作用及实现原理!
官方文档解释
Beans on which the current bean depends. Any beans specified are guaranteed to be created by the container before this bean. Used infrequently in cases where a bean does not explicitly depend on another through properties or constructor arguments, but rather depends on the side effects of another bean's initialization.
A depends-on declaration can specify both an initialization-time dependency and, in the case of singleton beans only, a corresponding destruction-time dependency. Dependent beans that define a depends-on relationship with a given bean are destroyed first, prior to the given bean itself being destroyed. Thus, a depends-on declaration can also control shutdown order.
May be used on any class directly or indirectly annotated with org.springframework.stereotype.Component or on methods annotated with Bean.
Using DependsOn at the class level has no effect unless component-scanning is being used. If a DependsOn-annotated class is declared via XML, DependsOn annotation metadata is ignored, and <bean depends-on="..."/> is respected instead.
@DependsOn注解的作用
org.springframework.context.annotation.DependsOn
该注解的属性是一个字符串数组,数组的元素是每个依赖的bean的名称。
@Target({ElementType.TYPE, ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface DependsOn { String[] value() default {}; }
@DependsOn
注解主要用于指定当前bean所依赖的beans。任何被指定依赖的bean都由Spring保证在当前bean之前创建。在少数情况下,bean不是通过属性或构造函数参数显式依赖于另一个bean,但却需要要求另一个bean优先完成初始化,则可以使用@DependsOn
这个注解。
@DependsOn
既可以指定初始化依赖顺序,也可以指定bean相应的销毁执行顺序(仅在单例bean的情况下)。
可用于任何直接或间接带@Component
注解的bean或在用@Bean
注释的方法上。
如果使用的是xml配置,则需要使用<bean dependens on=“…”/>
标签.
简单描述就是@DependsOn
可以控制bean的创建、初始化(InitializingBean)、销毁方法执行顺序。
示例:假如有三个Bean类叫Aaa、Bbb、Ccc分别实现了如下两个接口。
org.springframework.beans.factory.InitializingBean
org.springframework.beans.factory.DisposableBean
Ccc通过@DependsOn指定依赖bean创建的顺序为Bbb > Aaa
@DependsOn({"bbb","ccc"}) @Service public class Aaa implements InitializingBean, DisposableBean { private static final Logger logger = LoggerFactory.getLogger(Aaa.class); public Aaa() { logger.info(this.getClass().getName() + " Construction"); } @Override public void afterPropertiesSet() throws Exception { logger.info(this.getClass().getName() + " afterPropertiesSet"); } @Override public void destroy() throws Exception { logger.info(this.getClass().getName() + " destroy"); } }
Bbb Ccc类实现如下
@Service public class Bbb implements InitializingBean, DisposableBean { //实现和Aaa相同 } @Service public class Ccc implements InitializingBean, DisposableBean { //实现和Aaa相同 }
那么初始顺序如下: bbb --> ccc --> aaa
而销毁方法执行顺序正好相反如下: aaa --> ccc --> bbb
@DependsOn注解的实现原理
Spring在启动时扫描到一个bean,会封装成一个BeanDefinition,如果是AnnotatedBeanDefinition
则解析类上的注解信息,发现@DependsOn
注解,则读取value值,调用BeanDefinition#setDependsOn
保存。
源码见ClassPathBeanDefinitionScanner#doScan
,AnnotationConfigUtils#processCommonDefinitionAnnotations(AnnotatedBeanDefinition, AnnotatedTypeMetadata)
创建bean时,也就是调用AbstractBeanFactory#doGetBean
时,会获取这些被依赖的beanName,按照数组顺序,再调用AbstractBeanFactory#getBean(beanName)
来优先创建被依赖的bean,从而达到控制依赖顺序。
除此之外,在创建bean时,还会调用AbstractBeanFactory#registerDisposableBeanIfNecessary
来向Spring中注册带有销毁方法的bean,源码见DefaultSingletonBeanRegistry#registerDisposableBean
,内部通过LinkedHashMap保存。key为bean名称。进程退出时,会逆序调用销毁方法。
源码见DefaultSingletonBeanRegistry#destroySingletons
public void destroySingletons() { if (logger.isTraceEnabled()) { logger.trace("Destroying singletons in " + this); } synchronized (this.singletonObjects) { this.singletonsCurrentlyInDestruction = true; } String[] disposableBeanNames; synchronized (this.disposableBeans) { disposableBeanNames = StringUtils.toStringArray(this.disposableBeans.keySet()); } for (int i = disposableBeanNames.length - 1; i >= 0; i--) { destroySingleton(disposableBeanNames[i]); } this.containedBeanMap.clear(); this.dependentBeanMap.clear(); this.dependenciesForBeanMap.clear(); clearSingletonCache(); }
加载全部内容