javascript
spring条件注解有哪些_Spring4有条件
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)行:
另一種方法是使用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)部基于基于條件的元注釋:
翻譯自: https://www.javacodegeeks.com/2013/10/spring-4-conditional.html
spring條件注解有哪些
總結(jié)
以上是生活随笔為你收集整理的spring条件注解有哪些_Spring4有条件的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 高防ddos怎么做的(ddos高防需要注
- 下一篇: 通过OAuth 2.0和Okta构建具有