Spring properties dependency checking
In Spring,you can use dependency checking feature to make sure the required properties have been set or injected.
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
A Customer
and Person
object 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 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 '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
, collection
and object
) have not been set, an UnsatisfiedDependencyException
will be thrown.
Global default dependency checking
Explicitly define the dependency checking mode for every beans is tedious and error prone, you can set a default-dependency-check
attribute 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 @Required
Annotation can enforce this checking.
Spring properties dependency checking的更多相关文章
- Spring dependency checking with @Required Annotation
Spring's dependency checking in bean configuration file is used to make sure all properties of a cer ...
- spring properties resolve 问题
在stackoverflow上看到一个问题 配置如下: <context:property-placeholder location="/WEB-INF/application-cus ...
- Benefits of Using the Spring Framework Dependency Injection 依赖注入 控制反转
小结: 1. Dependency Injection is merely one concrete example of Inversion of Control. 依赖注入是仅仅是控制反转的一个具 ...
- [Spring] Properties for project configuration
We might have some project specific configuration need to setup. The good approach to do this in Spr ...
- spring security maven dependency
Unable to locate Spring NamespaceHandler for XML schema namespace [ spring secutity dependency: < ...
- 【Spring源码分析】.properties文件读取及占位符${...}替换源码解析
前言 我们在开发中常遇到一种场景,Bean里面有一些参数是比较固定的,这种时候通常会采用配置的方式,将这些参数配置在.properties文件中,然后在Bean实例化的时候通过Spring将这些.pr ...
- Spring boot 配置文件详解 (properties 和yml )
从其他框架来看 我们都有自己的配置文件, hibernate有hbm,mybatis 有properties, 同样, Spring boot 也有全局配置文件. Springboot使用一个全局的配 ...
- eclips环境下开发spring boot项目,application.properties配置文件下中文乱码解决方案
如以上,application.properties文件下中文乱码.发生乱码一般都是由于编码格式不一样导致的. 打开Window-Preferences-General-content Types-T ...
- spring boot项目,application.properties配置文件下中文乱码解决方案
转自:https://blog.csdn.net/qq_40408534/article/details/79831807 如以上,application.properties文件下中文乱码.发生乱码 ...
随机推荐
- NDK(21)JNI的5大正确性缺陷及优化技巧(注意是正确性缺陷)
转自 : http://www.ibm.com/developerworks/cn/java/j-jni/index.html JNI 编程缺陷可以分为两类: 性能:代码能执行所设计的功能,但运行缓慢 ...
- Linux系统下统计目录及其子目录文件个数
(1)查看某目录下文件的个数: ls -l |grep "^-"|wc -l 或 find ./company -type f | wc -l (2)查看某目录下文件的个数,包括子 ...
- subsets-ii(需要思考,包括了子数组的求法)
还是有一定难度的. 基本方法,就是用队列,然后不断累加新的数.这是为了不重复而量身定制的. 如果运行重复,是有更简单清晰的方法,就是每次增加考虑一个数字,然后加到本来每一个结果的后面.如下: publ ...
- Python3 学习第三弹:异常情况如何处理?
python 的处理错误的方式: 1> 断言 assert condition 相当于 if not condition: crash program 断言设置的目的就是因为与其让程序晚点崩溃, ...
- struct TABLE_SHARE
struct TABLE_SHARE { TABLE_SHARE() {} /* Remove gcc warning */ /** Category of this table. */ TABLE_ ...
- javac编译过程
编译器把一种语言规范转化为另一种语言规范的这个过程需要哪些步骤:
- LA 3295 (计数 容斥原理) Counting Triangles
如果用容斥原理递推的办法,这道题确实和LA 3720 Highway很像. 看到大神们写的博客,什么乱搞啊,随便统计一下,这真的让小白很为难,于是我决定用比较严格的语言来写这篇题解. 整体思路很简单: ...
- bzoj1934: [Shoi2007]Vote 善意的投票
最大流..建图方式都是玄学啊.. //Dinic是O(n2m)的. #include<cstdio> #include<cstring> #include<cctype& ...
- NYOJ 536 开心的mdd【矩阵链乘】
题意:给出n个矩阵组成的序列,问最少的运算量 看的紫书: dp[i][j]表示从第i个矩阵到第j个矩阵最少的乘法次数 dp[i][j]=min(dp[i][j],dp[i][k]+dp[k+1][j] ...
- PS流格式
概念: 将具有共同时间基准的一个或多个PES组合(复合)而成的单一的数据流称为节目流(Program Stream). ES是直接从编码器出来的数据流,可以是编码过的视频数据流,音频数据流,或其他编码 ...