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文件下中文乱码.发生乱码 ...
随机推荐
- java导出excel报表
1.java导出excel报表: package cn.jcenterhome.util; import java.io.OutputStream;import java.util.List;impo ...
- Android开发之关于ListView中adapter调用notifyDataSetChanged无效的原因
1.数据源没有更新,调用notifyDataSetChanged无效. 2.数据源更新了,但是它指向新的引用,调用notifyDataSetChanged无效. 3.数据源更新了,但是adpter没有 ...
- Topcoder SRM 630 (500 floyed 暴力 _builtin_popcount())
题意:给n个点,保证图联通,给点相连的距离,求一个最多的点,这些点之间的距离都是相同的. 分析: 下面的代码是我们房间第一的大神的,写的很简洁,我的思路和他的一样,但是我不知道错哪了. 思路是暴力枚举 ...
- iOS开发:记录开发中遇到的编译或运行异常以及解决方案
1.部署到真机异常 dyld`dyld_fatal_error: -> 0x120015088 <+0>: brk #0x3 dyld: Library not loaded ...
- LeetCode: Word Break I && II
I title: https://leetcode.com/problems/word-break/ Given a string s and a dictionary of words dict, ...
- BUFFER CACHE之主要的等待事件
原因:资源紧张,等待其释放. 原因的原因:1. lgwr和DBWn进程写太慢:2. Buffer和latch不可用 原因的原因的原因:全表扫描.library cache latches数太多等. 视 ...
- VS2010开发2dx无法解析的外部符号解决记录
首先新建HelloWorld项目... 想使用Cocos2d扩展包需要引入相关头文件,如:#include “cocos-ext.h”...接下来我们右键工程属性->配置属性->c/c++ ...
- 一天一个Java基础——反射
1.概念 反射主要是指程序可以访问,检测和修改它本身的状态或行为的一种能力 Java中的反射是一种强大的工具,它能够创建灵活的代码,这些代码可以运行时装配,无须在组件之间进行链接 反射允许在编写与执行 ...
- TCP/IP详解学习笔记(12)-TCP的超时与重传
超时重传是TCP协议保证数据可靠性的另一个重要机制,其原理是在发送某一个数据以后就开启一个计时器,在一定时间内如果没有得到发送的数据报的ACK报文,那么就重新发送数据,直到发送成功为止. 1.超时 超 ...
- sharepoint2010 创建自定义列表
转:http://boke.25k5.com/kan77298.html 如何创建自定义列表 首先了解创建自定义列表中涉及到的几个名词:栏.内容类型. ①栏:栏即列.字段(Field),MSDN中给出 ...