react 前端框架如何驱动企业数字化转型与创新发展
634
2023-02-06
Spring中@DependsOn注解的作用及实现原理解析
本文给大家讲解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'spCVbgyK 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
@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配置,则需要使用
简单描述就是@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;
synchrpCVbgyKonized (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();
}
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~