日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 前端技术 > javascript >内容正文

javascript

spring条件注解有哪些_Spring4有条件

發(fā)布時(shí)間:2023/12/3 javascript 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 spring条件注解有哪些_Spring4有条件 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

spring條件注解有哪些

Spring 4引入了一個(gè)稱(chēng)為Conditional的新功能,該功能針對(duì)于生成bean的Spring組件,并注視這些bean的生成,實(shí)質(zhì)上,它提供了一種條件生成bean的方法。

考慮一個(gè)簡(jiǎn)單的例子:

我有一個(gè)名為“ CustomerService”的服務(wù),該服務(wù)有兩個(gè)實(shí)現(xiàn),例如“ CustomerService1”和“ CustomerService2”。 基于系統(tǒng)屬性(例如“ servicedefault”)的存在,我要?jiǎng)?chuàng)建默認(rèn)的“ CustomerService1”實(shí)現(xiàn),如果不存在,則要?jiǎng)?chuàng)建“ CustomerService2”的實(shí)例。

使用基于Java配置的Spring bean定義,我可以這樣進(jìn)行:

@Configuration public static class ContextConfig {@Beanpublic CustomerService customerService() {if (System.getProperty("servicedefault")!=null) {return new CustomerServiceImpl1();}return new CustomerServiceImpl2();} }

另一種方法是使用Spring 3.1引入的Spring Bean Profiles :

@Bean @Profile("default") public CustomerService service1() {return new CustomerServiceImpl1(); }@Bean @Profile("prod") public CustomerService service2() {return new CustomerServiceImpl2(); }

但是,此特定實(shí)例中的Profiles有點(diǎn)笨拙,因?yàn)楹茈y設(shè)置用于管理一個(gè)bean的實(shí)現(xiàn)策略的配置文件,它更適合需要控制一組bean的行為的情況。

Spring 4引入了條件注釋,可以用一些可重用的方式來(lái)實(shí)現(xiàn)此行為。

條件依賴(lài)于一組條件類(lèi)來(lái)指定謂詞,這種方式是:

class HardCodedSystemPropertyPresentCondition implements Condition {@Overridepublic boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {return (System.getProperty("servicedefault") != null);} }class HardCodedSystemPropertyAbsentCondition implements Condition {@Overridepublic boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {return (System.getProperty("servicedefault") == null);} }

我需要兩個(gè)謂詞,一個(gè)用于指定肯定條件,一個(gè)用于指定否定條件,這些謂詞現(xiàn)在可以應(yīng)用于bean定義:

@Bean @Conditional(HardCodedSystemPropertyPresentCondition.class) public CustomerService service1() {return new CustomerServiceImpl1(); }@Bean @Conditional(HardCodedSystemPropertyAbsentCondition.class) public CustomerService service2() {return new CustomerServiceImpl2(); }

但是,請(qǐng)注意,條件代碼中有一個(gè)硬編碼的系統(tǒng)屬性名稱(chēng)“ servicedefault”,可以使用元注釋進(jìn)一步清除它。 可以通過(guò)以下方式定義新的元注釋:

@Target({ ElementType.TYPE, ElementType.METHOD }) @Retention(RetentionPolicy.RUNTIME) @Conditional(OnSystemPropertyCondition.class) public @interface ConditionalOnSystemProperty {public String value();public boolean exists() default true; }

此元注釋ConditionalOnSystemProperty接受兩個(gè)用戶(hù)指定的屬性-系統(tǒng)屬性名稱(chēng)的“值”和“存在”以檢查該屬性是否存在或檢查該屬性不存在。 該元注釋使用@Conditional注釋進(jìn)行標(biāo)記,該注釋指向Condition類(lèi)以觸發(fā)使用此新的meta注釋進(jìn)行注釋的bean,Condition類(lèi)如下:

public class OnSystemPropertyCondition implements Condition {@Overridepublic boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {Map<String, Object> attributes = metadata.getAnnotationAttributes(ConditionalOnSystemProperty.class.getName());Boolean systemPropertyExistsCheck = (Boolean)attributes.get("exists");String systemProperty = (String)attributes.get("value");if ((systemPropertyExistsCheck && (System.getProperty(systemProperty) != null)) ||(!systemPropertyExistsCheck && (System.getProperty(systemProperty) == null))) {return true;}return false;} }

這里的邏輯是使用元注釋來(lái)掌握@Bean實(shí)例上定義的屬性,并基于附加的“ exists”屬性觸發(fā)對(duì)系統(tǒng)屬性是否存在的檢查。 現(xiàn)在,可以在@Bean實(shí)例上定義此可重用的元注釋,以通過(guò)以下方式有條件地創(chuàng)建bean:

@Configuration public static class ContextConfig {@Bean@ConditionalOnSystemProperty("servicedefault")public CustomerService service1() {return new CustomerServiceImpl1();}@Bean@ConditionalOnSystemProperty(value="servicedefault", exists=false)public CustomerService service2() {return new CustomerServiceImpl2();} }

結(jié)語(yǔ)

這里的示例很簡(jiǎn)單,可能不是很現(xiàn)實(shí),僅用于演示條件功能。 在Spring 4中,一個(gè)更好的例子是使用條件條件修改我先前提到的基于Spring 3.1的Profiles本身的行為的方式,
現(xiàn)在, Profiles內(nèi)部基于基于條件的元注釋:

@Conditional(ProfileCondition.class) public @interface Profile {String[] value(); }

參考: Spring4有條件,來(lái)自我們的JCG合作伙伴 Biju Kunjummen,在all和其他博客上。

翻譯自: https://www.javacodegeeks.com/2013/10/spring-4-conditional.html

spring條件注解有哪些

總結(jié)

以上是生活随笔為你收集整理的spring条件注解有哪些_Spring4有条件的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。