Spring中@DependsOn注解的使用代碼實(shí)例
@DependsOn的簡(jiǎn)介
/**
* 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 {};
}說(shuō)明
- 使用范圍. 注解可以使用在類上,方法上. 在類上,一般與@Component注解使用 ; 在方法上, 通常會(huì)與@Bean注解使用.
- 注解中的字段是一個(gè)數(shù)組value, 填充的是bean對(duì)象,可以放置多個(gè)bean, 填入的bean會(huì)比使用該注解的對(duì)象早一點(diǎn)注冊(cè)到容器中.
使用場(chǎng)景
在一些場(chǎng)景, 需要去監(jiān)聽(tīng)事件源的觸發(fā),從而處理相應(yīng)的業(yè)務(wù). 那么就需要監(jiān)聽(tīng)類比事件源先注冊(cè)到容器中, 因?yàn)槭录从|發(fā)前,監(jiān)聽(tīng)就應(yīng)該存在,否則就不滿足使用場(chǎng)景的要求.
@DependsOn的使用
案例1
1 準(zhǔn)備一個(gè)SpringBoot環(huán)境
2 添加監(jiān)聽(tīng)類和事件源類
@Component
public class ASource {
public ASource(){
System.out.println("事件創(chuàng)建");
}
}@Component
public class BListener {
public BListener(){
System.out.println("監(jiān)聽(tīng)創(chuàng)建");
}
}3 啟動(dòng)項(xiàng)目,查看控制臺(tái)
// 事件創(chuàng)建 // 監(jiān)聽(tīng)創(chuàng)建
上面明顯,不符合實(shí)際使用要求.
4 改造事件源類
@Component
@DependsOn(value = {"BListener"})
public class ASource {
public ASource(){
System.out.println("事件創(chuàng)建");
}
}
5 啟動(dòng)項(xiàng)目,查看控制臺(tái)
// 監(jiān)聽(tīng)創(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 啟動(dòng)項(xiàng)目,查看控制臺(tái)
// 監(jiān)聽(tīng)創(chuàng)建 // 事件創(chuàng)建
滿足使用要求.
到此這篇關(guān)于Spring中@DependsOn注解的使用代碼實(shí)例的文章就介紹到這了,更多相關(guān)@DependsOn注解內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring?boot?整合RabbitMQ實(shí)現(xiàn)通過(guò)RabbitMQ進(jìn)行項(xiàng)目的連接
RabbitMQ是一個(gè)開(kāi)源的AMQP實(shí)現(xiàn),服務(wù)器端用Erlang語(yǔ)言編寫(xiě),支持多種客戶端,這篇文章主要介紹了Spring?boot?整合RabbitMQ實(shí)現(xiàn)通過(guò)RabbitMQ進(jìn)行項(xiàng)目的連接,需要的朋友可以參考下2022-10-10
synchronized及JUC顯式locks?使用原理解析
這篇文章主要為大家介紹了synchronized及JUC顯式locks?使用原理解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12
解決springboot遇到autowire注入為null的問(wèn)題
這篇文章主要介紹了解決springboot遇到autowire注入為null的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-03-03
Java責(zé)任鏈設(shè)計(jì)模式實(shí)例分析
這篇文章主要介紹了Java責(zé)任鏈設(shè)計(jì)模式,結(jié)合實(shí)例形式詳細(xì)分析了Java責(zé)任鏈設(shè)計(jì)模式的原理與相關(guān)操作技巧,需要的朋友可以參考下2019-07-07
Pattern.compile函數(shù)提取字符串中指定的字符(推薦)
這篇文章主要介紹了Pattern.compile函數(shù)提取字符串中指定的字符,使用的是Java中的Pattern.compile函數(shù)來(lái)實(shí)現(xiàn)對(duì)指定字符串的截取,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-12-12
Java中實(shí)現(xiàn)線程間通信的實(shí)例教程
線程通信的目標(biāo)是使線程間能夠互相發(fā)送信號(hào),另一方面線程通信使線程能夠等待其他線程的信號(hào),這篇文章主要給大家介紹了關(guān)于Java中實(shí)現(xiàn)線程間通信的相關(guān)資料,本文通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2021-09-09
spring boot 實(shí)現(xiàn)Minio分片上傳的步驟
分片上傳,就是將所要上傳的文件,按照一定的大小,將整個(gè)文件分隔成多個(gè)數(shù)據(jù)塊來(lái)進(jìn)行分別上傳,上傳完之后再由服務(wù)端對(duì)所有上傳的文件進(jìn)行匯總整合成原始的文件,本文給大家介紹spring boot 實(shí)現(xiàn)Minio分片上傳的步驟,感興趣的朋友跟隨小編一起看看吧2023-10-10

