Spring中@DependsOn注解的使用代碼實例
@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. * * <p>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. * * <p>May be used on any class directly or indirectly annotated with * {@link org.springframework.stereotype.Component} or on methods annotated * with {@link Bean}. * * <p>Using {@link DependsOn} at the class level has no effect unless component-scanning * is being used. If a {@link DependsOn}-annotated class is declared via XML, * {@link DependsOn} annotation metadata is ignored, and * {@code <bean depends-on="..."/>} is respected instead. * * @author Juergen Hoeller * @since 3.0 */ @Target({ElementType.TYPE, ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface DependsOn { String[] value() default {}; }
說明
- 使用范圍. 注解可以使用在類上,方法上. 在類上,一般與@Component注解使用 ; 在方法上, 通常會與@Bean注解使用.
- 注解中的字段是一個數(shù)組value, 填充的是bean對象,可以放置多個bean, 填入的bean會比使用該注解的對象早一點注冊到容器中.
使用場景
在一些場景, 需要去監(jiān)聽事件源的觸發(fā),從而處理相應(yīng)的業(yè)務(wù). 那么就需要監(jiān)聽類比事件源先注冊到容器中, 因為事件源觸發(fā)前,監(jiān)聽就應(yīng)該存在,否則就不滿足使用場景的要求.
@DependsOn的使用
案例1
1 準(zhǔn)備一個SpringBoot環(huán)境
2 添加監(jiān)聽類和事件源類
@Component public class ASource { public ASource(){ System.out.println("事件創(chuàng)建"); } }
@Component public class BListener { public BListener(){ System.out.println("監(jiān)聽創(chuàng)建"); } }
3 啟動項目,查看控制臺
// 事件創(chuàng)建 // 監(jiān)聽創(chuàng)建
上面明顯,不符合實際使用要求.
4 改造事件源類
@Component @DependsOn(value = {"BListener"}) public class ASource { public ASource(){ System.out.println("事件創(chuàng)建"); } }
5 啟動項目,查看控制臺
// 監(jiān)聽創(chuàng)建 // 事件創(chuàng)建
上述輸出,滿足使用要求.
案例2
1 添加配置類
@Configuration public class Config { @Bean @DependsOn(value = {"BListener"}) public ASource aSource(){ return new ASource(); } @Bean public BListener bListener(){ return new BListener(); } }
2 啟動項目,查看控制臺
// 監(jiān)聽創(chuàng)建 // 事件創(chuàng)建
滿足使用要求.
到此這篇關(guān)于Spring中@DependsOn注解的使用代碼實例的文章就介紹到這了,更多相關(guān)@DependsOn注解內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot Redis實現(xiàn)接口冪等性校驗方法詳細(xì)講解
這篇文章主要介紹了SpringBoot Redis實現(xiàn)接口冪等性校驗方法,近期一個老項目出現(xiàn)了接口冪等性校驗問題,前端加了按鈕置灰,依然被人拉著接口參數(shù)一頓輸出,還是重復(fù)調(diào)用了接口,通過復(fù)制粘貼,完成了后端接口冪等性調(diào)用校驗2022-11-11java HashMap,TreeMap與LinkedHashMap的詳解
這篇文章主要介紹了 java HashMap,TreeMap與LinkedHashMap的詳解的相關(guān)資料,這里提供實例代碼,幫助大家學(xué)習(xí)理解 這部分的內(nèi)容,需要的朋友可以參考下2016-11-11如何優(yōu)雅的拋出Spring Boot注解的異常詳解
這篇文章主要給大家介紹了關(guān)于如何優(yōu)雅的拋出Spring Boot注解的異常的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-12-12eclipse的web項目實現(xiàn)Javaweb購物車的方法
這篇文章主要介紹了eclipse的web項目實現(xiàn)Javaweb購物車的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10