最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Spring?Boot?條件注解詳情

 更新時(shí)間:2022年05月18日 11:38:10   作者:tuacy  
這篇文章主要介紹了Spring?Boot?條件注解詳情,SpringBoot條件注解@Conditional,可用于根據(jù)某個(gè)特定的條件來(lái)判斷是否需要?jiǎng)?chuàng)建某個(gè)特定的Bean,下文更多相關(guān)介紹,需要的小伙伴可以參考一下

前言:

SpringBoot條件注解@Conditional,可用于根據(jù)某個(gè)特定的條件來(lái)判斷是否需要?jiǎng)?chuàng)建某個(gè)特定的Bean。SpringBoot自動(dòng)配置功能里面就大量的使用了條件注解。接下來(lái)我們就對(duì)@Conditional的使用做一個(gè)簡(jiǎn)單的介紹。

@Conditional注解需要和Condition接口搭配一起使用。通過(guò)對(duì)應(yīng)Condition接口來(lái)告知是否滿足匹配條件。

@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Conditional {

    /**
     * 所有用于匹配的Condition接口(實(shí)現(xiàn)該接口的類),只有這些類都返回true才認(rèn)為是滿足條件
     */
    Class<? extends Condition>[] value();
}

@Conditional注解可以添加在@Configuration、@Component、@Service等修飾的類上用于控制對(duì)應(yīng)的Bean是否需要?jiǎng)?chuàng)建,或者添加在@Bean修飾的方法上用于控制方法對(duì)應(yīng)的Bean是否需要?jiǎng)?chuàng)建。

@Conditional添加在@Configuration修飾的類上,用于控制該類和該類里面所有添加的@Bean方法對(duì)應(yīng)的Bean是否需要?jiǎng)?chuàng)建。

一 @Conditional擴(kuò)展注解

為了方便我們的使用Spring Boot對(duì)@Conditional條件注解做了一些擴(kuò)展,提供了一些很實(shí)用的擴(kuò)展性條件注解。

上面的擴(kuò)展注解我們可以簡(jiǎn)單的分為以下幾類:

  • Bean作為條件:@ConditionalOnBean、@ConditionalOnMissingBean、@ConditionalOnSingleCandidate。
  • 類作為條件:@ConditionalOnClass、@ConditionalOnMissingClass。
  • SpEL表達(dá)式作為條件:@ConditionalOnExpression。
  • JAVA版本作為條件: @ConditionalOnJava
  • 配置屬性作為條件:@ConditionalOnProperty。
  • 資源文件作為條件:@ConditionalOnResource。
  • 是否Web應(yīng)用作為判斷條件:@ConditionalOnWebApplication、@ConditionalOnNotWebApplication。

1.1 Bean作為條件

1.1.1 @ConditionalOnBean

 @ConditionalOnBean對(duì)應(yīng)的Condition處理類是OnBeanCondition。如果Spring容器里面存在指定的Bean則生效。

@ConditionalOnBean配置參數(shù)

@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Conditional(OnBeanCondition.class)
public @interface ConditionalOnBean {
    /**
     * 需要作為條件的類的Class對(duì)象數(shù)組
     */
    Class<?>[] value() default {};
    /**
     * 需要作為條件的類的Name, Class.getName()
     */
    String[] type() default {};

    /**
     * (用于指定注解修飾的Bean)條件所需的注解類
     */
    Class<? extends Annotation>[] annotation() default {};
    /**
     * Spring容器中Bean的名字
     */
    String[] name() default {};
    /**
     * 搜索容器層級(jí),當(dāng)前容器,父容器
     */
    SearchStrategy search() default SearchStrategy.ALL;

    /**
     * 可能在其泛型參數(shù)中包含指定Bean類型的其他類
     */
    Class<?>[] parameterizedContainer() default {};
}

1.1.2 @ConditionalOnMissingBean

@ConditionalOnMissingBean對(duì)應(yīng)的Condition實(shí)現(xiàn)類是OnBeanCondition。如果Spring容器里面不存在指定的Bean則生效。

@ConditionalOnMissingBean配置參數(shù)

@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Conditional(OnBeanCondition.class)
public @interface ConditionalOnMissingBean {
    /**
     * 需要作為條件的類的Class對(duì)象數(shù)組
     */
    Class<?>[] value() default {};

    /**
     * 需要作為條件的類的Name, Class.getName()
     */
    String[] type() default {};

    /**
     * 匹配Bean的時(shí)候需要忽視的Class對(duì)象數(shù)組,一般是父類
     * @ConditionalOnMissingBean(value = JdbcFactory.class, ignored = MySqlDefaultFactory.class)
     */
    Class<?>[] ignored() default {};

    /**
     * 匹配Bean的時(shí)候需要忽視的類的Name, Class.getName()
     */
    String[] ignoredType() default {};
    /**
     * (用于指定注解修飾的Bean)條件所需的注解類
     */
    Class<? extends Annotation>[] annotation() default {};

    /**
     * Spring容器中Bean的名字
     */
    String[] name() default {};

    /**
     * 搜索容器層級(jí),當(dāng)前容器,父容器
     */
    SearchStrategy search() default SearchStrategy.ALL;

    /**
     * 可能在其泛型參數(shù)中包含指定Bean類型的其他類
     */
    Class<?>[] parameterizedContainer() default {};
}

比如如下的實(shí)例,當(dāng)容器里面不存在redisTemplate對(duì)應(yīng)的Bean的時(shí)候,就會(huì)創(chuàng)建一個(gè)RedisTemplate添加到容器里面去。

    @Bean
    @ConditionalOnMissingBean(name = "redisTemplate")
    public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory)
            throws UnknownHostException {
        RedisTemplate<Object, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(redisConnectionFactory);
        return template;
    }

1.1.3 @ConditionalOnSingleCandidate

 @ConditionalOnSingleCandidate對(duì)應(yīng)的Condition處理類是OnBeanCondition。如果當(dāng)指定Bean在容器中只有一個(gè),或者雖然有多個(gè)但是指定首選Bean的時(shí)候則生效。

@ConditionalOnSingleCandidate配置參數(shù)

@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Conditional(OnBeanCondition.class)
public @interface ConditionalOnSingleCandidate {
    /**
     * 需要作為條件的類的Class對(duì)象
     */
    Class<?> value() default Object.class;

    /**
     * 需要作為條件的類的Name, Class.getName()
     */
    String type() default "";

    /**
     * 搜索容器層級(jí),當(dāng)前容器,父容器
     */
    SearchStrategy search() default SearchStrategy.ALL;
}

1.2 類作為條件

1.2.1 @ConditionalOnClass

 @ConditionalOnClass對(duì)應(yīng)的Condition處理類是OnClassCondition。如果當(dāng)前類路徑下面有指定的類的時(shí)候則生效。

@ConditionalOnClass配置屬性介紹

@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Conditional(OnClassCondition.class)
public @interface ConditionalOnClass {
    /**
     * 需要作為條件的類的Class對(duì)象數(shù)組
     */
    Class<?>[] value() default {};
    /**
     * 需要作為條件的類的Name, Class.getName()
     */
    String[] name() default {};
}

1.2.2 @ConditionalOnMissingClass

 @ConditionalOnMissingClass對(duì)應(yīng)的Condition處理類是OnClassCondition。如果當(dāng)前類路徑下面沒(méi)有指定的類的時(shí)候則生效。

@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Conditional(OnClassCondition.class)
public @interface ConditionalOnMissingClass {
    /**
     * 需要作為條件的類的Name, Class.getName()
     */
    String[] value() default {};
}

1.3 SpEL表達(dá)式作為條件

@ConditionalOnExpression對(duì)應(yīng)的Condition處理類是OnExpressionCondition。只有當(dāng)SpEL表達(dá)式滿足條件的時(shí)候則生效。

@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.TYPE, ElementType.METHOD })
@Documented
@Conditional(OnExpressionCondition.class)
public @interface ConditionalOnExpression {
    /**
     * 要作為條件的SpEL表達(dá)式
     */
    String value() default "true";
}

例如@ConditionalOnExpression("${test.enabled:true}"),只有當(dāng)配置文件里面存在test.enabled: true的時(shí)候則生效。

更加詳細(xì)的用法可以去看下SpEL表達(dá)式的使用。

1.4 JAVA版本作為判斷條件

 @ConditionalOnJava對(duì)應(yīng)的Condition處理類是OnJavaCondition。只有當(dāng)指定的JAVA版本條件滿足的時(shí)候,才會(huì)創(chuàng)建對(duì)應(yīng)的Bean。

@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Conditional(OnJavaCondition.class)
public @interface ConditionalOnJava {
    /**
     * 比較方式,Range.EQUAL_OR_NEWER:當(dāng)前版本等于或高于、Range.OLDER_THAN:當(dāng)前版本老于,越早的版本越老
     */
    Range range() default Range.EQUAL_OR_NEWER;

    /**
     * 指定JAVA版本
     */
    JavaVersion value();
    /**
     * Range options.
     */
    enum Range {
        /**
         * Equal to, or newer than the specified {@link JavaVersion}.
         */
        EQUAL_OR_NEWER,

        /**
         * Older than the specified {@link JavaVersion}.
         */
        OLDER_THAN
    }
}

1.5 配置屬性作為判斷條件

@ConditionalOnProperty對(duì)應(yīng)的Condition實(shí)現(xiàn)類OnPropertyCondition。只有當(dāng)對(duì)應(yīng)的配置屬性和給定條件的值相等的時(shí)候則生效。

@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.TYPE, ElementType.METHOD })
@Documented
@Conditional(OnPropertyCondition.class)
public @interface ConditionalOnProperty {
    /**
     * 對(duì)應(yīng)property名稱的值
     */
    String[] value() default {};
    String[] name() default {};
    /**
     * property名稱的前綴,可有可無(wú)
     */
    String prefix() default "";

    /**
     * 與name組合使用,比較獲取到的屬性值與havingValue給定的值是否相同,相同才加載配置
     */
    String havingValue() default "";
    /**
     * 缺少該property時(shí)是否可以加載。如果為true,沒(méi)有該property也會(huì)正常加載;反之報(bào)錯(cuò)
     */
    boolean matchIfMissing() default false;
}

 @ConditionalOnProperty(prefix = “spring.aop”, name = “auto”, havingValue = “true”)表示當(dāng)配置文件里面spring.aop.auto=true的時(shí)候才會(huì)加載對(duì)應(yīng)的Bean。

1.6 資源文件是否存在作為判斷條件

@ConditionalOnResource對(duì)應(yīng)的Condition處理類OnResourceCondition。只有當(dāng)指定的資源文件出現(xiàn)在classpath中則生效。

@ConditionalOnResource配置屬性

@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Conditional(OnResourceCondition.class)
public @interface ConditionalOnResource {
    /**
     * 要作為判斷條件的資源文件名稱  @ConditionalOnResource(resources=”mybatis.xml”)
     */
    String[] resources() default {};
}

1.7 是否Web應(yīng)用作為判斷條件

1.7.1 @ConditionalOnWebApplication

@ConditionalOnWebApplication對(duì)應(yīng)的Condition處理類是OnWebApplicationCondition。只有當(dāng)當(dāng)前項(xiàng)目是Web項(xiàng)目的時(shí)候則生效。

@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Conditional(OnWebApplicationCondition.class)
public @interface ConditionalOnWebApplication {
    /**
     * 需要作為條件的Web應(yīng)用程序的必需類型
     */
    Type type() default Type.ANY;

    /**
     * Available application types.
     */
    enum Type {

        /**
         * 任何web應(yīng)用都將匹配
         */
        ANY,

        /**
         * 僅基于servlet的Web應(yīng)用程序?qū)⑵ヅ?
         */
        SERVLET,

        /**
         * 僅基于反應(yīng)式的Web應(yīng)用程序?qū)⑵ヅ?
         */
        REACTIVE
    }
}

1.7.2 @ConditionalOnNotWebApplication

@ConditionalOnNotWebApplication對(duì)應(yīng)的Condition處理類是OnWebApplicationCondition。只有當(dāng)當(dāng)前項(xiàng)目不是Web項(xiàng)目的時(shí)候則生效。

@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Conditional(OnWebApplicationCondition.class)
public @interface ConditionalOnNotWebApplication {
}

二 @Conditional自定義

上面介紹每個(gè)擴(kuò)展注解的時(shí)候都特意提到了每個(gè)注解對(duì)應(yīng)的Condition實(shí)現(xiàn)類。其實(shí)我們可以仿照這些Condition實(shí)現(xiàn)類來(lái)實(shí)現(xiàn)我們自己的@Conditional注解。下面我們同個(gè)兩個(gè)簡(jiǎn)單的實(shí)例來(lái)看下怎么實(shí)現(xiàn)自己的@Conditional擴(kuò)展注解。

2.1 判斷是否配置指定屬性

 注意:和@ConditionalOnProperty不一樣哦,@ConditionalOnProperty是判斷是否有屬性并且判斷值是否等于我們指定的值。我們要實(shí)現(xiàn)的注解只判斷有沒(méi)有配置屬性,不管屬性對(duì)應(yīng)的值。

擴(kuò)展注解ConditionalOnPropertyExist。指定我們的Condition實(shí)現(xiàn)類OnPropertyExistCondition。并且指定兩個(gè)參數(shù)。一個(gè)是參數(shù)name用于指定屬性。另一個(gè)參數(shù)exist用于指定是判斷存在還是不存在。

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.METHOD})
@Documented
@Conditional(OnPropertyExistCondition.class)
public @interface ConditionalOnPropertyExist {

    /**
     * 配置文件里面對(duì)應(yīng)的key
     */
    String name() default "";

    /**
     * 是否有配置的時(shí)候判斷通過(guò)
     */
    boolean exist() default true;

}

OnPropertyExistCondition類就是簡(jiǎn)單的判斷下屬性存在與否。

public class OnPropertyExistCondition implements Condition {
    @Override
    public boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) {
        Map<String, Object> annotationAttributes = annotatedTypeMetadata.getAnnotationAttributes(ConditionalOnPropertyExist.class.getName());
        if (annotationAttributes == null) {
            return false;
        }
        String propertyName = (String) annotationAttributes.get("name");
        boolean values = (boolean) annotationAttributes.get("exist");
        String propertyValue = conditionContext.getEnvironment().getProperty(propertyName);
        if(values) {
            return !StringUtils.isEmpty(propertyValue);
        } else {
            return StringUtils.isEmpty(propertyValue);
        }
    }
}

2.1 判斷是否配置指定屬性

我們簡(jiǎn)單實(shí)現(xiàn)這樣一個(gè)功能,根據(jù)指定的系統(tǒng)加載不同的Bean。

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.METHOD})
@Documented
@Conditional(OnSystemCondition.class)
public @interface ConditionalOnSystem {
    /**
     * 指定系統(tǒng)
     */
    SystemType type() default SystemType.WINDOWS;

    /**
     * 系統(tǒng)類型
     */
    enum SystemType {

        /**
         * windows系統(tǒng)
         */
        WINDOWS,

        /**
         * linux系統(tǒng)
         */
        LINUX,

        /**
         * mac系統(tǒng)
         */
        MAC

    }
}
public class OnSystemCondition implements Condition {
    @Override
    public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
        Map<String, Object> annotationAttributes = metadata.getAnnotationAttributes(ConditionalOnSystem.class.getName());
        if (annotationAttributes == null) {
            return false;
        }
        ConditionalOnSystem.SystemType systemType = (ConditionalOnSystem.SystemType) annotationAttributes.get("type");
        switch (systemType) {
            case WINDOWS:
                return context.getEnvironment().getProperty("os.name").contains("Windows");
            case LINUX:
                return context.getEnvironment().getProperty("os.name").contains("Linux ");
            case MAC:
                return context.getEnvironment().getProperty("os.name").contains("Mac ");
        }
        return false;
    }
}

到此這篇關(guān)于Spring Boot 條件注解詳情的文章就介紹到這了,更多相關(guān)Spring Boot內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • java圖搜索算法之圖的對(duì)象化描述示例詳解

    java圖搜索算法之圖的對(duì)象化描述示例詳解

    這篇文章主要為大家介紹了java圖搜索算法之圖的對(duì)象化描述示例詳解,可以帶你秒殺所有的圖搜索算法,有需要的朋友可以借鑒參考下,希望能夠有所幫助
    2021-11-11
  • SpringBoot限制接口訪問(wèn)頻率功能實(shí)現(xiàn)

    SpringBoot限制接口訪問(wèn)頻率功能實(shí)現(xiàn)

    最近在基于SpringBoot做一個(gè)面向普通用戶的系統(tǒng),為了保證系統(tǒng)的穩(wěn)定性,防止被惡意攻擊,我想控制用戶訪問(wèn)每個(gè)接口的頻率,接下來(lái)通過(guò)本文給大家介紹SpringBoot限制接口訪問(wèn)頻率功能實(shí)現(xiàn),需要的朋友可以參考下
    2023-05-05
  • Spring cloud gateway設(shè)置context-path服務(wù)路由404排查過(guò)程

    Spring cloud gateway設(shè)置context-path服務(wù)路由404排查過(guò)程

    這篇文章主要介紹了Spring cloud gateway設(shè)置context-path服務(wù)路由404排查過(guò)程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-08-08
  • Java文件操作類 File實(shí)現(xiàn)代碼

    Java文件操作類 File實(shí)現(xiàn)代碼

    這篇文章主要介紹了Java文件操作類 File實(shí)現(xiàn)代碼,需要的朋友可以參考下
    2017-08-08
  • Josephus環(huán)的四種解法(約瑟夫環(huán))基于java詳解

    Josephus環(huán)的四種解法(約瑟夫環(huán))基于java詳解

    這篇文章主要介紹了Josephus環(huán)的四種解法(約瑟夫環(huán))基于java詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-09-09
  • spring?boot寫java?web和接口

    spring?boot寫java?web和接口

    這篇文章主要介紹了spring?boot寫java?web和接口,Spring?Boot是由Pivotal團(tuán)隊(duì)提供的全新框架,其設(shè)計(jì)目的是用來(lái)簡(jiǎn)化新Spring應(yīng)用的初始搭建以及開發(fā)過(guò)程,該框架使用了特定的方式來(lái)進(jìn)行配置,從而使開發(fā)人員不再需要定義樣板化的配置,下面詳細(xì)內(nèi)容需要的小伙伴可以參考一下
    2022-01-01
  • 詳解Java發(fā)送HTTP請(qǐng)求

    詳解Java發(fā)送HTTP請(qǐng)求

    這篇文章主要介紹了Java發(fā)送HTTP請(qǐng)求,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-03-03
  • aop注解方式實(shí)現(xiàn)全局日志管理方法

    aop注解方式實(shí)現(xiàn)全局日志管理方法

    下面小編就為大家分享一篇aop注解方式實(shí)現(xiàn)全局日志管理方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-01-01
  • 如何通過(guò)zuul添加或修改請(qǐng)求參數(shù)

    如何通過(guò)zuul添加或修改請(qǐng)求參數(shù)

    這篇文章主要介紹了如何通過(guò)zuul添加或修改請(qǐng)求參數(shù)的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-07-07
  • spring-boot整合Micrometer+Prometheus的詳細(xì)過(guò)程

    spring-boot整合Micrometer+Prometheus的詳細(xì)過(guò)程

    這篇文章主要介紹了springboot整合Micrometer+Prometheus的詳細(xì)過(guò)程,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧
    2024-05-05

最新評(píng)論

岐山县| 巴青县| 南皮县| 安溪县| 临沂市| 辽宁省| 原阳县| 漯河市| 北辰区| 吴忠市| 榕江县| 临颍县| 揭阳市| 义乌市| 隆林| 义马市| 精河县| 惠东县| 徐闻县| 广平县| 巴青县| 南投市| 连江县| 铜梁县| 罗定市| 滨海县| 革吉县| 楚雄市| 仁布县| 渝中区| 三门县| 巴中市| 随州市| 桂阳县| 沂源县| 池州市| 西乌珠穆沁旗| 襄汾县| 许昌县| 始兴县| 新余市|