用@Required注解检查属性依赖
来源:互联网 发布:时时彩免费源码下载 编辑:程序博客网 时间:2024/05/03 20:12
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>
0 0
- 用@Required注解检查属性依赖
- Spring @Required 注解检查属性
- Spring @Required 注解检查属性
- Spring入门学习——用@Required注解检查属性
- Spring 3.0 Required注解检查
- Spring的属性依赖检查
- Spring 注解实现Bean依赖注入之@Required
- Spring关于@required注解
- Spring容器拆分、自动装配和依赖检查机制,基于注解的sessionFactory
- (7) 用@Resource注解完成属性装配 【依赖注入----手动装配】 以及 编码解析其原理【经典】
- (6) 用@Resource注解完成属性装配 【依赖注入----手动装配】 以及 编码解析其原理【经典】
- html required属性
- mvc注解使用 @Repository @Required
- Dubbo服务依赖检查
- Spring中的依赖检查
- Spring中的依赖检查
- 依赖属性
- 依赖属性
- 微信支付注意事项
- 基数排序
- 博客地址
- 【服务器】JavaWeb配置Servlet定时任务
- Oracl docs 命令导出导入数据
- 用@Required注解检查属性依赖
- 前台国际化i18next使用
- JavaScript/jQuery 表单美化插件小结
- Android Support兼容包详解
- 省钱神功:男子趴公交前行
- 8、linux进程间的通信方式
- 正则表达式常用元字符小结
- Android学习笔记--WebView打开网页相关的注意事项
- 【小明开发一百问】(二)关于Genymotion你知道多少