欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Spring中@DependsOn注解的使用代碼實例

 更新時間:2024年01月26日 08:55:21   作者:韓_師兄  
這篇文章主要介紹了Spring中@DependsOn注解的使用代碼實例,Spring中@DependsOn,主要是使用在類和方法上, 作用是當(dāng)前對象要依賴另外一些對象,被依賴的對象會先注冊到Spring的IOC容器中,需要的朋友可以參考下

@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)接口冪等性校驗方法詳細(xì)講解

    這篇文章主要介紹了SpringBoot Redis實現(xiàn)接口冪等性校驗方法,近期一個老項目出現(xiàn)了接口冪等性校驗問題,前端加了按鈕置灰,依然被人拉著接口參數(shù)一頓輸出,還是重復(fù)調(diào)用了接口,通過復(fù)制粘貼,完成了后端接口冪等性調(diào)用校驗
    2022-11-11
  • java HashMap,TreeMap與LinkedHashMap的詳解

    java HashMap,TreeMap與LinkedHashMap的詳解

    這篇文章主要介紹了 java HashMap,TreeMap與LinkedHashMap的詳解的相關(guān)資料,這里提供實例代碼,幫助大家學(xué)習(xí)理解 這部分的內(nèi)容,需要的朋友可以參考下
    2016-11-11
  • 使用java8的方法引用替換硬編碼的示例代碼

    使用java8的方法引用替換硬編碼的示例代碼

    這篇文章主要介紹了使用java8的方法引用替換硬編碼,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-09-09
  • 如何優(yōu)雅的拋出Spring Boot注解的異常詳解

    如何優(yōu)雅的拋出Spring Boot注解的異常詳解

    這篇文章主要給大家介紹了關(guān)于如何優(yōu)雅的拋出Spring Boot注解的異常的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-12-12
  • eclipse的web項目實現(xiàn)Javaweb購物車的方法

    eclipse的web項目實現(xiàn)Javaweb購物車的方法

    這篇文章主要介紹了eclipse的web項目實現(xiàn)Javaweb購物車的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-10-10
  • JAVA annotation入門基礎(chǔ)

    JAVA annotation入門基礎(chǔ)

    以下是JAVA annotation入門基礎(chǔ),新手朋友們可以過來參考下。希望對你有所幫助
    2013-08-08
  • Maven倉庫的具體使用(本地倉庫+遠(yuǎn)程倉庫)

    Maven倉庫的具體使用(本地倉庫+遠(yuǎn)程倉庫)

    Maven 在某個統(tǒng)一的位置存儲所有項目的構(gòu)件,這個統(tǒng)一的位置,我們就稱之為倉庫,本文主要介紹了Maven倉庫的具體使用(本地倉庫+遠(yuǎn)程倉庫),感興趣的可以了解一下
    2023-11-11
  • Jar包沖突問題原理及解決方案

    Jar包沖突問題原理及解決方案

    這篇文章主要介紹了Jar包沖突問題原理及解決方案,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-07-07
  • Java序列化與反序列化

    Java序列化與反序列化

    這篇文章主要介紹了Java的序列化與反序列化,序列化把一個對象Java Object變?yōu)橐粋€二進(jìn)制字節(jié)序列byte[];反序列化就是把一個二進(jìn)制字節(jié)序列byte[]變?yōu)镴ava對象Java Object。感興趣的小伙伴可以參考閱讀
    2023-04-04
  • SpringBoot 之啟動流程詳解

    SpringBoot 之啟動流程詳解

    SpringBoot 是一個基于 Spring 框架的快速開發(fā)框架,旨在簡化 Spring 應(yīng)用程序的開發(fā)和部署。在本文中,我們將深入分析 SpringBoot 啟動過程的源代碼,并提供必要的解釋和說明
    2023-04-04

最新評論