javascript
Spring4有条件
Spring 4引入了一個稱為Conditional的新功能,該功能針對于生成bean的Spring組件,并注視這些bean的生成,實質上,它提供了一種條件生成bean的方法。
考慮一個簡單的例子:
我有一個名為“ CustomerService”的服務,該服務有兩個實現,例如“ CustomerService1”和“ CustomerService2”。 基于系統屬性(例如“ servicedefault”)的存在,我要創建默認的“ CustomerService1”實現,如果不存在,則要創建“ CustomerService2”的實例。
使用基于Java配置的Spring bean定義,我可以這樣做:
另一種方法是使用Spring 3.1引入的Spring Bean Profiles :
@Bean @Profile("default") public CustomerService service1() {return new CustomerServiceImpl1(); }@Bean @Profile("prod") public CustomerService service2() {return new CustomerServiceImpl2(); }但是,此特定實例中的Profiles有點笨拙,因為很難設置用于管理一個bean的實現策略的配置文件,它更適合需要控制一組bean的行為的情況。
Spring 4引入了條件注釋,可以用一些可重用的方式來實現此行為。
條件依賴于一組條件類來指定謂詞,這種方式是:
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);} }我需要兩個謂詞,一個用于指定肯定條件,一個用于指定否定條件,這些謂詞現在可以應用于bean定義:
@Bean @Conditional(HardCodedSystemPropertyPresentCondition.class) public CustomerService service1() {return new CustomerServiceImpl1(); }@Bean @Conditional(HardCodedSystemPropertyAbsentCondition.class) public CustomerService service2() {return new CustomerServiceImpl2(); }但是,請注意,條件代碼中有一個硬編碼的系統屬性名稱“ servicedefault”,可以使用元注釋進一步清除它。 可以通過以下方式定義新的元注釋:
@Target({ ElementType.TYPE, ElementType.METHOD }) @Retention(RetentionPolicy.RUNTIME) @Conditional(OnSystemPropertyCondition.class) public @interface ConditionalOnSystemProperty {public String value();public boolean exists() default true; }此元注釋ConditionalOnSystemProperty接受兩個用戶指定的屬性-系統屬性名稱的“值”和“存在”以檢查該屬性是否存在或檢查該屬性不存在。 該元注釋使用@Conditional注釋進行標記,該注釋指向Condition類以觸發使用此新的meta注釋進行注釋的bean,Condition類如下:
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;} }這里的邏輯是使用元注釋來掌握@Bean實例上定義的屬性,并基于附加的“ exists”屬性觸發檢查系統屬性是否存在。 現在,可以在@Bean實例上定義此可重用的元注釋,以通過以下方式有條件地創建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();} }包起來
這里的示例很簡單,可能不是很現實,僅用于演示條件功能。 在Spring 4中,一個更好的例子是使用條件修改我之前提到的基于Spring 3.1的Profiles本身的行為的方式,
現在, Profiles在內部基于基于條件的元注釋:
翻譯自: https://www.javacodegeeks.com/2013/10/spring-4-conditional.html
總結
以上是生活随笔為你收集整理的Spring4有条件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux线程进程区别(linux线程进
- 下一篇: Spring Data Solr教程:排