閱讀584 返回首頁    go 阿裏雲 go 技術社區[雲棲]


《Spring攻略(第2版)》——1.10 用@Required注解檢查屬性

本節書摘來自異步社區《Spring攻略(第2版)》一書中的第1章,第1.10節,作者: 【美】Gary Mak , Josh Long , Daniel Rubio著,更多章節內容可以訪問雲棲社區“異步社區”公眾號查看

1.10 用@Required注解檢查屬性

1.10.1 問題
Spring的依賴檢查功能僅能檢查某些類型的所有屬性。它的靈活性不夠,不能僅檢查特定的屬性。在大部分情況下,你希望檢查特定的屬性是否設置,而不是特定類型的所有屬性。

1.10.2 解決方案
RequiredAnnotationBeanPostProcessor是一個Spring bean後處理器,檢查帶有@Required注解的所有Bean屬性是否設置。Bean後處理器是一類特殊的Spring bean,能夠在每個Bean初始化之前執行附加的工作。為了啟用這個Bean後處理器進行屬性檢查,必須在Spring IoC容器中注冊它。注意,這個處理器隻能檢查屬性是否已經設置,而不能測試屬性是否非空。

1.10.3 工作原理
假定對於序列生成器來說,prefixGenerator和suffix屬性都是必要的。你可以用@Required注解它們的設值方法。

package com.apress.springrecipes.sequence;

import org.springframework.beans.factory.annotation.Required;

public class SequenceGenerator {
   private PrefixGenerator prefixGenerator;
   private String suffix;
   ...
   @Required
   public void setPrefixGenerator(PrefixGenerator prefixGenerator) {
     this.prefixGenerator = prefixGenerator;
   }

   @Required
   public void setSuffix(String suffix) {
     this.suffix = suffix;
   }
   ...
}

為了要求Spring檢查所有序列生成器實例上這些屬性是否已經設置,你必須在IoC容器中注冊一個RequiredAnnotationBeanPostProcessor實例。如果你打算使用Bean工廠,就必須通過API注冊這個Bean後處理器,否則,隻能在應用上下文中聲明這個Bean後處理器的實例。

<bean  />

如果你正在使用Spring 2.5或者更高版本,可以簡單地在Bean配置文件中包含元素,這將自動地注冊一個RequiredAnnotationBeanPostProcessor實例。

<beans xmlns="https://www.springframework.org/schema/beans"
   xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
   xmlns:context="https://www.springframework.org/schema/context"
   xsi:schemaLocation="https://www.springframework.org/schema/beans
     https://www.springframework.org/schema/beans/spring-beans-3.0.xsd
     https://www.springframework.org/schema/context
     https://www.springframework.org/schema/context/spring-context-3.0.xsd">

   <context:annotation-config />
   ...
</beans>

如果任何帶有@Required的屬性未設置,Bean後處理器將拋出一個BeanInitializationException異常。

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error
creating bean with name 'sequenceGenerator' defined in class path resource [beans.xml]: Initialization of bean failed; nested exception is org.springframework.beans.factory.
BeanInitializationException: Property 'prefixGenerator' is required for bean
'sequenceGenerator'

除了@Required注解之外,RequiredAnnotationBeanPostProcessor還能用自定義的注解檢查屬性。例如,你可以創建如下注解類型:

package com.apress.springrecipes.sequence;
...
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Mandatory {
}

然後將這個注解應用到必要屬性的設值方法。

package com.apress.springrecipes.sequence;

public class SequenceGenerator {

   private PrefixGenerator prefixGenerator;
   private String suffix;
   ...
   @Mandatory
   public void setPrefixGenerator(PrefixGenerator prefixGenerator) {
     this.prefixGenerator = prefixGenerator;
   }

   @Mandatory
   public void setSuffix(String suffix) {
     this.suffix = suffix;
   }
   ...
}

為了用這個注解類型檢查屬性,你必須在RequiredAnnotationBeanPostProcessor的required AnnotationType屬性中指定。

<bean >
   <property name="requiredAnnotationType">
     <value>com.apress.springrecipes.sequence.Mandatory</value>
   </property>
</bean>

最後更新:2017-05-31 15:31:29

  上一篇:go  揭密出席第九屆中國雲計算大會的專家—近20個國家學者共話雲計算大數據生態、應用
  下一篇:go  《Spring攻略(第2版)》——1.9 用依賴檢查屬性