SpringBoot - @DependsOn注解详解

注解说明


@DependsOn注解主要用于指定当前BEAN所依赖的BEANS。任何被指定的依赖的BEAN都由Spring容器保证在当前BEAN之前创建和加载。在某些场景下,BEAN不是通过属性或构造函数参数显式依赖于另一个BEAN,但却需要要求另一个BEAN优先完成初始化,则可以使用@DependsOn这个注解。
@DependsOn既可以指定初始化的依赖顺序,也可以指定BEAN相应的销毁执行顺序(仅在单例bean的情况下)。

使用目的


控制BEAN的加载顺序。
用来表示一个BEAN-A的实例化依赖另一个BEAN-B的实例化, 但是BEAN-A并不需要持有一个BEAN-B的对象,如果需要则需要使用依赖注入或者ref标签。

注解源码
 

// 该注解的属性是一个字符串数组,数组的元素是每个依赖的BEAN的名称。
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface DependsOn {
	String[] value() default {};
}

官方英文说明:

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 is respected instead.


使用说明


(1)@DependsOn注解可以定义在类和方法上;
(2)直接或者间接标注在带有@Component注解的类上面;
(3)直接或者间接标注在带有@Bean 注解的方法上面;
(4)如果使用的是XML配置,则需要使用标签;
(5)@DependsOn({“BEAN-NAME-1”, “BEAN-NAME-2”}),先创建BEAN-NAME-1,再创建BEAN-NAME-2,最后再创建当前的BEAN。

组合使用

可以与@Component注解和@Bean注解组合使用。

示例代码

@DependsOn({"c", "b"})
@Component
public class A implements InitializingBean, DisposableBean {

    public A() {
        System.out.println("TO CREATE: " + this.getClass().getName());
    }

    @Override
    public void afterPropertiesSet() throws Exception {
    }

    @Override
    public void destroy() throws Exception {
        System.out.println("TO DESTROY: " + this.getClass().getName());
    }
}

@Component
public class B implements InitializingBean, DisposableBean {

    public B() {
        System.out.println("TO CREATE: " + this.getClass().getName());
    }

    @Override
    public void afterPropertiesSet() throws Exception {
    }
    
    @Override
    public void destroy() throws Exception {
        System.out.println("TO DESTROY: " + this.getClass().getName());
    }
}

@Component
public class C implements InitializingBean, DisposableBean {

    public C() {
        System.out.println("TO CREATE: " + this.getClass().getName());
    }

    @Override
    public void afterPropertiesSet() throws Exception {

    }
    
    @Override
    public void destroy() throws Exception {
        System.out.println("TO DESTROY: " + this.getClass().getName());
    }
}

加载与创建顺序:C、B、A
销毁顺序与创建顺序相反:A、B、C

@DependsOn和@Lazy的区别


@DependsOn注解用于指定各个BEAN的依赖顺序;@Lazy注解用于指定该类是否能够懒加载,也就是说如果一个类不需要在启动时实例化,那么就可以使用@Lazy注解进行懒加载,在第一次使用时创建并实例化。

(1)可以使用@Lazy注解将某些BEAN在第一次使用时创建并加载到SPRING容器中;
(2)@Lazy注解只对单例有用,它让BEAN在SPRING启动时不会加载到容器中,只有在代码里第一次调用时才创建并加载到容器中;
(3)如果只在某个类上添加@Lazy注解,但是其他代码依赖了该类,那么即使在该类上添加了@Lazy注解,也依然会在IoC容器初始化时实例化该类,也就是@Lazy注解失效了,这就是由代码之间的依赖导致的@Lazy注解失效;如果希望在使用时才去实例化,那么就需要在每个依赖该类的地方也添加@Lazy注解;
(4)如上代码所示,如果在C这个BEAN上也添加了@Lazy注解,则@Lazy注解也会失效,这就是由@DependsOn注解引起的@Lazy注解失效。
 

猜你喜欢

转载自blog.csdn.net/Angel_asp/article/details/131002363