Spring dependency checking with @Required Annotation
2015-08-21 16:25
381 查看
Spring’s dependency checking in bean configuration file is used to make sure all properties of a certain types (primitive, collection or object) have been set. In most scenarios, you just need to make sure a particular property has been set, but not all properties..
For this case, you need
Simply apply the
The
Full example,
If you run it , the following error message will be throw, because
For this case, you need
@Requiredannotation, see following example :
@Required example
ACustomerobject, apply
@Requiredin
setPerson()method to make sure the
personproperty has been set.
package com.mkyong.common; import org.springframework.beans.factory.annotation.Required; public class Customer { private Person person; private int type; private String action; public Person getPerson() { return person; } @Required public void setPerson(Person person) { this.person = person; } }
Simply apply the
@Requiredannotation will not enforce the property checking, you also need to register an
RequiredAnnotationBeanPostProcessorto aware of the
@Requiredannotation in bean configuration file.
The
RequiredAnnotationBeanPostProcessorcan be enabled in two ways.
1. Include
Add Spring context and<context:annotation-config />in bean configuration file.
<beans ... xmlns:context="http://www.springframework.org/schema/context" ... http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> ... <context:annotation-config /> ... </beans>
Full example,
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <context:annotation-config /> <bean id="CustomerBean" class="com.mkyong.common.Customer"> <property name="action" value="buy" /> <property name="type" value="1" /> </bean> <bean id="PersonBean" class="com.mkyong.common.Person"> <property name="name" value="mkyong" /> <property name="address" value="address ABC" /> <property name="age" value="29" /> </bean> </beans>
2. Include RequiredAnnotationBeanPostProcessor
Include ‘RequiredAnnotationBeanPostProcessor’ directly in bean configuration file.
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean class="org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor"/> <bean id="CustomerBean" class="com.mkyong.common.Customer"> <property name="action" value="buy" /> <property name="type" value="1" /> </bean> <bean id="PersonBean" class="com.mkyong.common.Person"> <property name="name" value="mkyong" /> <property name="address" value="address ABC" /> <property name="age" value="29" /> </bean> </beans>
If you run it , the following error message will be throw, because
personproperty is unset.
org.springframework.beans.factory.BeanInitializationException: Property 'person' is required for bean 'CustomerBean'
Conclusion
Try@Requiredannotation, it is more flexible than dependency checking in XML file, because it can apply to a particular property only.
相关文章推荐
- Java添加注册表协议
- Java NIO
- java性能调优
- java synchronized 线程同步机制详解
- Spring properties dependency checking
- Eclipse调试Bug的七种常用技巧
- Eclipse调试Bug的七种常用技巧
- Atitit.html解析器的选型 jsoup nsoup ,java c# .net 版本
- Atitit.html解析器的选型 jsoup nsoup ,java c# .net 版本
- Atitit.html解析器的选型 jsoup nsoup ,java c# .net 版本
- java Swing设置窗口无限大并且没有修饰
- java Swing设置无限大
- java 生成二维码 实践 图片流形式
- Eclipse 无限编译Invoking 'Maven Project Builder'导致卡主
- Spring bean configuration inheritance
- “错误: 找不到或无法加载主类”解决办法
- 那些猥琐的Java笔试题集锦(持续更新)
- java获取文件md5值
- [leetcode-166]Fraction to Recurring Decimal(java)
- Java数组的基本操作方法整理