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è)務. 那么就需要監(jiān)聽類比事件源先注冊到容器中, 因為事件源觸發(fā)前,監(jiān)聽就應該存在,否則就不滿足使用場景的要求.
@DependsOn的使用
案例1
1 準備一個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)建
滿足使用要求.
到此這篇關于Spring中@DependsOn注解的使用代碼實例的文章就介紹到這了,更多相關@DependsOn注解內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Spring?boot?整合RabbitMQ實現(xiàn)通過RabbitMQ進行項目的連接
RabbitMQ是一個開源的AMQP實現(xiàn),服務器端用Erlang語言編寫,支持多種客戶端,這篇文章主要介紹了Spring?boot?整合RabbitMQ實現(xiàn)通過RabbitMQ進行項目的連接,需要的朋友可以參考下2022-10-10
synchronized及JUC顯式locks?使用原理解析
這篇文章主要為大家介紹了synchronized及JUC顯式locks?使用原理解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-12-12
解決springboot遇到autowire注入為null的問題
這篇文章主要介紹了解決springboot遇到autowire注入為null的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-03-03
Pattern.compile函數(shù)提取字符串中指定的字符(推薦)
這篇文章主要介紹了Pattern.compile函數(shù)提取字符串中指定的字符,使用的是Java中的Pattern.compile函數(shù)來實現(xiàn)對指定字符串的截取,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-12-12
spring boot 實現(xiàn)Minio分片上傳的步驟
分片上傳,就是將所要上傳的文件,按照一定的大小,將整個文件分隔成多個數(shù)據(jù)塊來進行分別上傳,上傳完之后再由服務端對所有上傳的文件進行匯總整合成原始的文件,本文給大家介紹spring boot 實現(xiàn)Minio分片上傳的步驟,感興趣的朋友跟隨小編一起看看吧2023-10-10

