Spring properties dependency checking
2015-08-21 16:21
441 查看
In Spring,you can use dependency checking feature to make sure the required properties have been set or injected.
none – No dependency checking.
simple – If any properties of primitive type (int, long,double…) and collection types (map, list..) have not been set, UnsatisfiedDependencyException will be thrown.
objects – If any properties of object type have not been set, UnsatisfiedDependencyException will be thrown.
all – If any properties of any type have not been set, an UnsatisfiedDependencyException will be thrown.
P.S The default mode is none
If you did not explicitly define the dependency checking mode, it’s default to ‘
The ‘type’ property (primitive type or collection types) have not been set, an UnsatisfiedDependencyException will throw.
The ‘
The combination of ‘
All beans declared in this configuration file are default to ‘
Dependency checking modes
4 dependency checking modes are supported:none – No dependency checking.
simple – If any properties of primitive type (int, long,double…) and collection types (map, list..) have not been set, UnsatisfiedDependencyException will be thrown.
objects – If any properties of object type have not been set, UnsatisfiedDependencyException will be thrown.
all – If any properties of any type have not been set, an UnsatisfiedDependencyException will be thrown.
P.S The default mode is none
Example
ACustomerand
Personobject for the demonstration.
package com.mkyong.common; public class Customer { private Person person; private int type; private String action; //getter and setter methods }
package com.mkyong.common; public class Person { private String name; private String address; private int age; //getter and setter methods }
1. none dependency checking
Spring bean configuration file with ‘none’ dependency checking mode.<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 id="CustomerBean" class="com.mkyong.common.Customer" > <property name="action" value="buy" /> </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 did not explicitly define the dependency checking mode, it’s default to ‘
none’. No dependency checking will perform.
2. simple dependency checking
Spring bean configuration file with ‘simple’ dependency checking mode.
<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 id="CustomerBean" class="com.mkyong.common.Customer" dependency-check="simple"> <property name="person" ref="PersonBean" /> <property name="action" value="buy" /> </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>
The ‘type’ property (primitive type or collection types) have not been set, an UnsatisfiedDependencyException will throw.
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'CustomerBean' defined in class path resource [config/Spring-Customer.xml]: Unsatisfied dependency expressed through bean property 'type': Set this property value or disable dependency checking for this bean.
3. objects dependency checking
Spring bean configuration file with ‘objects’ dependency checking mode.
<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 id="CustomerBean" class="com.mkyong.common.Customer" dependency-check="objects"> <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>
The ‘
person’ property (objects type) have not been set, an
UnsatisfiedDependencyExceptionwill throw.
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'CustomerBean' defined in class path resource [config/Spring-Customer.xml]: Unsatisfied dependency expressed through bean property 'person': Set this property value or disable dependency checking for this bean.
4. all dependency checking
Spring bean configuration file with ‘all’ dependency checking mode.
<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 id="CustomerBean" class="com.mkyong.common.Customer" dependency-check="all"> <property name="action" value="buy" /> </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>
The combination of ‘
simple’ and ‘
objects’ mode, if any properties of any type (
primitive,
collectionand
object) have not been set, an
UnsatisfiedDependencyExceptionwill be thrown.
Global default dependency checking
Explicitly define the dependency checking mode for every beans is tedious and error prone, you can set adefault-dependency-checkattribute in the
<beans>root element to force the entire beans declared within
<beans>root element to apply this rule. However, this root default mode will be overridden by a bean’s own mode if specified.
<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" default-dependency-check="all"> <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>
All beans declared in this configuration file are default to ‘
all’ dependency checking mode.
@Required Annotation
In most scenarios, you just need to make sure a particular property has been set, but not all properties of a certain types (primitive, collection or object). The@RequiredAnnotation can enforce this 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数组的基本操作方法整理
- java继承
- java变量类型
- Error: opening registry key 'Software\JavaSoft\Java Runtime Environment' could not find java.dll
- SpringMVC 表单提交参数不匹配报错
- springmvc学习笔记(二)路径配置与数据传递