SpringBoot中的@DependsOn注解詳解
@DependsOn
@DependsOn注解主要用于指定當前BEAN所依賴的BEANS。任何被指定的依賴的BEAN都由Spring容器保證在當前BEAN之前創(chuàng)建和加載。
在某些場景下,BEAN不是通過屬性或構造函數(shù)參數(shù)顯式依賴于另一個BEAN,但卻需要要求另一個BEAN優(yōu)先完成初始化,則可以使用@DependsOn這個注解。
@DependsOn既可以指定初始化的依賴順序,也可以指定BEAN相應的銷毀執(zhí)行順序(僅在單例bean的情況下)。
注解@DependsOn
位于如下包
org.springframework.context.annotation
該注解用于聲明當前bean依賴于另外一個bean。所依賴的bean
會被容器確保在當前bean
實例化之前被實例化。
舉例來講,如果容器通過@DependsOn
注解方式定義了bean
plant
依賴于bean
water
,那么容器在會確保bean
water
的實例在實例化bean
plant
之前完成。
一般用在一個bean沒有通過屬性或者構造函數(shù)參數(shù)顯式依賴另外一個bean,但實際上會使用到那個bean或者那個bean產(chǎn)生的某些結果的情況。
使用目的
控制BEAN的加載順序。
用來表示一個BEAN-A的實例化依賴另一個BEAN-B的實例化, 但是BEAN-A并不需要持有一個BEAN-B的對象,如果需要則需要使用依賴注入或者ref標簽。
注解源碼
// 該注解的屬性是一個字符串數(shù)組,數(shù)組的元素是每個依賴的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.
使用說明
- @DependsOn注解可以定義在類和方法上;
- 直接或者間接標注在帶有@Component注解的類上面;
- 直接或者間接標注在帶有@Bean 注解的方法上面;
- 如果使用的是XML配置,則需要使用標簽;
- @DependsOn({“BEAN-NAME-1”, “BEAN-NAME-2”}),先創(chuàng)建BEAN-NAME-1,再創(chuàng)建BEAN-NAME-2,最后再創(chuàng)建當前的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()); } }
加載與創(chuàng)建順序:C、B、A
銷毀順序與創(chuàng)建順序相反:A、B、C
@DependsOn和@Lazy的區(qū)別
- @DependsOn注解用于指定各個BEAN的依賴順序;
- @Lazy注解用于指定該類是否能夠懶加載,也就是說如果一個類不需要在啟動時實例化,那么就可以使用@Lazy注解進行懶加載,在第一次使用時創(chuàng)建并實例化。
- 可以使用@Lazy注解將某些BEAN在第一次使用時創(chuàng)建并加載到SPRING容器中;
- @Lazy注解只對單例有用,它讓BEAN在SPRING啟動時不會加載到容器中,只有在代碼里第一次調(diào)用時才創(chuàng)建并加載到容器中;
- 如果只在某個類上添加@Lazy注解,但是其他代碼依賴了該類,那么即使在該類上添加了@Lazy注解,也依然會在IoC容器初始化時實例化該類,也就是@Lazy注解失效了,這就是由代碼之間的依賴導致的@Lazy注解失效;如果希望在使用時才去實例化,那么就需要在每個依賴該類的地方也添加@Lazy注解;
- 如上代碼所示,如果在C這個BEAN上也添加了@Lazy注解,則@Lazy注解也會失效,這就是由@DependsOn注解引起的@Lazy注解失效。
到此這篇關于SpringBoot中的@DependsOn注解詳解的文章就介紹到這了,更多相關@DependsOn注解內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
寶塔面板配置及部署javaweb教程(全網(wǎng)最全)
這篇文章主要介紹了寶塔面板配置及部署javaweb教程(全網(wǎng)最全),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-06-06spring?aop?pointcut?添加多個execution方式
這篇文章主要介紹了spring?aop?pointcut?添加多個execution方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-11-11SpringBoot整合EasyExcel實現(xiàn)批量導入導出
這篇文章主要為大家詳細介紹了SpringBoot整合EasyExcel實現(xiàn)批量導入導出功能的相關知識,文中的示例代碼講解詳細,需要的小伙伴可以參考下2024-03-03