用@Required注解检查属性依赖

Spring的依赖检查功能仅能检查某些类型的所有属性,不能仅检查特定的属性,灵活性不够。而往往我们只需要检查特定的属性是否设置,而不是特定类型的所有属性。

      RequiredAnnotationBeanPostProcessor是一个Spring bean后处理器,它检查带有@Required注解的所有Bean属性是否设置。 Bean后处理器是一类特殊的Spring bean,能够在每个Bean初始化之前执行附加的操作。为了启用这个bean后处理器进行属性检查,必须在Spring IoC容器中注册它。注意,这个bean后处理器只能检查属性是否已经设置,而不能检查属性是否非空。

     代码示例: 

/*
 * Copyright 2013-2015
 */
package com.jackie.codeproject.springrecipesnote.springioc;

import java.text.DecimalFormat;
import java.util.Set;

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

/**
 * Title: SequenceGenerator.java 
 * 序列生成器
 * 
 * @author jackie
 * @since Apr 13, 2013 12:56:57 PM
 * @version V1.0
 */
public class SequenceGenerator {

    /**
     * @Fields prefixGenerator : 前缀生成器
     */
    private PrefixGenerator prefixGenerator;
   
    /**
     * @Fields suffix : 后缀
     */
    private Set<Integer> suffixes;

    /**
     * @Fields initial : 初始值
     */
    private int initial;

    /**
      * @Fields counter : 计数器
      */
    private int counter;

    /**
      * 获取序列号,声明为同步,使其成为线程安全的方法
      * @author jackie  
      * @date Apr 13, 2013
      * @return    
      * @return String   
      */
    public synchronized String getSquence() {
        StringBuffer buffer = new StringBuffer();
        buffer.append(prefixGenerator.getPrefix());
        buffer.append(initial + counter++);
        DecimalFormat formatter = new DecimalFormat("0000");
        for (Object suffix : suffixes) {
            buffer.append("-");
            buffer.append(formatter.format(suffix));
        }
        return buffer.toString();
    }

    /**
     * @param suffix
     *            the suffix to set
     */
    @Required
    public void setSuffixes(Set<Integer> suffixes) {
        this.suffixes = suffixes;
    }

    /**
     * @param initial
     *            the initial to set
     */
    public void setInitial(int initial) {
        this.initial = initial;
    }

    /**
     * @param prefixGenerator the prefixGenerator to set
     */
    @Required
    public void setPrefixGenerator(PrefixGenerator prefixGenerator) {
        this.prefixGenerator = prefixGenerator;
    }
}
     为了要求Spring检查序列生成器上所有带有@Required注解的bean属性是否已经设置,必须在IoC容器中注册一个RequiredAnnotationBeanPostProcessor实例。 如果打算使用Bean工厂,就必须通过API注册这个Bean后处理器,否则只能在应用程序上下文中声明。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util"
   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
   http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">	
   <bean id="sequenceGenerator" class="com.jackie.codeproject.springrecipesnote.springioc.SequenceGenerator">
      <property name="initial" value="100000" />
      <property name="suffixes">

   <ref local="suffixes"/>
      </property>
   </bean> 

   <util:set id="suffixes">
      <value>5</value>
      <value>10</value>
      <value>20</value>
   </util:set>
    
   <bean class="org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor" />
  
   <bean id="datePrefixGenerator" class="com.jackie.codeproject.springrecipesnote.springioc.DatePrefixGenerator">
      <property name="pattern" value="yyyyMMdd" />
   </bean>
</beans>
     Spring2.5或更高版本,可以简单在Bean配置文件中包含 <context:annotation-config> 元素,这将自动注册一个RequiredAnnotationBeanPostProcessor实例。 
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util"
   xmlns:context="http://www.springframework.org/schema/context"
   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
   http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd
   http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">	
   <context:annotation-config />
   。。。。。。
</beans>
       如果任何带有@Required的属性未设置,Bean后处理器将抛出一个BeanInitializationException异常。
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sequenceGenerator' defined in class path resource [applicationContext.xml]: Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanInitializationException: Property 'prefixGenerator' is required for bean 'sequenceGenerator'
       除了@Required注解之外,RequiredAnnotationBeanPostProcessor还能用自定义的注解检查属性。例如,创建如下注解类型:
/*  
  * Copyright 2013-2015  
  */
package com.jackie.codeproject.springrecipesnote.springioc;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * Title: Mmandatory.java
 *  类的功能说明
 * 
 * @author jackie
 * @since Apr 21, 2013 9:06:39 PM
 * @version V1.0
 */
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Mmandatory {

}
然后将注解应用到必要属性的设值方法。
/*
 * Copyright 2013-2015
 */
package com.jackie.codeproject.springrecipesnote.springioc;

import java.text.DecimalFormat;
import java.util.Set;

/**
 * Title: SequenceGenerator.java 
 * 序列生成器
 * 
 * @author jackie
 * @since Apr 13, 2013 12:56:57 PM
 * @version V1.0
 */
public class SequenceGenerator {

    /**
     * @Fields prefixGenerator : 前缀生成器
     */
    private PrefixGenerator prefixGenerator;
   
    /**
     * @Fields suffix : 后缀
     */
    private Set<Integer> suffixes;

    。。。。。。。。。。。

    /**
     * @param suffix
     *            the suffix to set
     */
    @Mandatory
    public void setSuffixes(Set<Integer> suffixes) {
        this.suffixes = suffixes;
    }

    /**
     * @param prefixGenerator the prefixGenerator to set
     */
    @Mandatory
    public void setPrefixGenerator(PrefixGenerator prefixGenerator) {
        this.prefixGenerator = prefixGenerator;
    }
}
为了用这个注解类型检查属性,必须在RequiredAnnotationBeanPostProcessor的 requireAnnotationType 属性中指定。
<bean class="org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor">
   <property name="requiredAnnotationType" value="com.jackie.codeproject.springrecipesnote.springioc.Mandatory" />
</bean>

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值