Spring学习笔记 关于spring 2.x中dependency-check标签与Spring3中的实现方式
2012-07-19 19:01
585 查看
在Bean被创建时Bean的属性(property)如果在配置文件Bean的定义中没有进行初始化赋值,默认情况下Spring对于没有进行初始化的属性(property)是不做检查的。但是很多情况下会要求Bean特定的属性必须进行初始化赋值,在Spring2.x中通过在bean标签中使用dependency-check属性设定由Spring进行强制检查的方式。denpendency-check属性有四个值
当检查没有通过时会抛出org.springframework.beans.factory.UnsatisfiedDependencyException异常。
看了以上的教程那会儿我就跑到自己的工程上写代码测试下。结果抛出了以下异常
![](http://my.csdn.net/uploads/201207/19/1342687777_2306.jpg)
我的spring-config.xml代码如下:
上网查了下资料发现,在我上边代码中使用了spring3.0的xsd(http://www.springframework.org/schema/beans/spring-beans-3.0.xsd)(用来验证xml文档有效性,同DTD文件作用相同),而在spring3.0的xsd文件中根本没有dependency-check属性的定义,原因是在spring3+中已经放弃使用这个属性了,如果需要使用这个属性则需要使用spring2.5的xsd文件。
而既然Spring3中放弃使用了dependency-check属性一定就会有替代它的功能出现。
查了下资料,果然,在spring3中替代dependency-check有4条建议:
Use constructors (constructor injection instead of setter injection) exclusively to ensure the right properties are set.
使用构造方法(使用构造方法注入替代setter注入)专门用来确认特定属性被初始化
Create setters with a dedicated init method implemented.
用init方法初始化setter的属性
Create setters with @Required annotation when the property is required.
在需要强制进行初始化的setters上标注@Required,可以参考http://www.mkyong.com/spring/spring-dependency-checking-with-required-annotation/
Use @Autowired-driven injection which also implies a required property by default.
使用@Autowired-driven 注入也可以实现
<bean id="ernie" class="com.***." dependency-check="none"> //如果不进行设置设就是Spring中dependency-check的默认值,不进行任何检查。
<bean id="ernie" class="com.***." dependency-check="simple"> //只检查简单类型属性以及集合类型属性
<bean id="ernie" class="com.***." dependency-check="object"> //检查除简单类型属性以及集合类型属性外的引用类型属性
<bean id="ernie" class="com.***." dependency-check="all"> //检查所有类型属性
当检查没有通过时会抛出org.springframework.beans.factory.UnsatisfiedDependencyException异常。
看了以上的教程那会儿我就跑到自己的工程上写代码测试下。结果抛出了以下异常
![](http://my.csdn.net/uploads/201207/19/1342687777_2306.jpg)
我的spring-config.xml代码如下:
<?xml version="1.0" encoding="UTF-8"?> <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-3.0.xsd"> <bean id="toBeCheckedBean" class="dependencycheck.ToBeCheckedBean" dependency-check="simple" /> </beans>
上网查了下资料发现,在我上边代码中使用了spring3.0的xsd(http://www.springframework.org/schema/beans/spring-beans-3.0.xsd)(用来验证xml文档有效性,同DTD文件作用相同),而在spring3.0的xsd文件中根本没有dependency-check属性的定义,原因是在spring3+中已经放弃使用这个属性了,如果需要使用这个属性则需要使用spring2.5的xsd文件。
而既然Spring3中放弃使用了dependency-check属性一定就会有替代它的功能出现。
查了下资料,果然,在spring3中替代dependency-check有4条建议:
Use constructors (constructor injection instead of setter injection) exclusively to ensure the right properties are set.
使用构造方法(使用构造方法注入替代setter注入)专门用来确认特定属性被初始化
Create setters with a dedicated init method implemented.
用init方法初始化setter的属性
Create setters with @Required annotation when the property is required.
在需要强制进行初始化的setters上标注@Required,可以参考http://www.mkyong.com/spring/spring-dependency-checking-with-required-annotation/
Use @Autowired-driven injection which also implies a required property by default.
使用@Autowired-driven 注入也可以实现
相关文章推荐
- Spring学习笔记 关于spring 2.x中dependency-check标签与Spring3中的实现方式
- MyBatis学习笔记-Spring集成DAO层实现方式记录
- spring学习笔记7--使用spring进行面向切面的(AOP)编程(1)注解方式实现
- Spring学习笔记(14)----使用Spring的注解方式实现AOP
- Spring学习笔记(三)Spring注解方式实现AOP
- ITCAST视频-Spring学习笔记(使用Spring的注解方式实现AOP入门)
- ITCAST视频-Spring学习笔记(使用Spring的注解方式实现AOP的细节)
- java学习笔记10 - 以@AspectJ方式在Spring中实现AOP
- Spring学习笔记 关于Bean定义的重用-使用abstract以及parent标签属性
- Spring学习笔记 关于Bean定义的重用-使用abstract以及parent标签属性
- spring学习教程9-spring中关于集合的配置方式
- Spring学习笔记(18)----使用Spring配置文件实现事务管理
- 【Spring学习笔记】五、使用JDBCTemplate以对象方式操作数据库
- 学习笔记_用hibernateQBC动态查询+jsp的jstl标签库实现的动态查询以及分页特效
- iOS学习笔记02——以编码的方式实现Auto Layout自动布局(一)
- 关于SQLServer2005的学习笔记——约束、Check、触发器的执行顺序
- 【unity学习笔记】手机RPG游戏中角色移动方式实现
- spring学习(八)—AOP通过注解方式实现
- iOS学习笔记-124.多线程23——多图下载3_第三种实现方式(使用SDWebImage)
- spring框架学习(七)—Spring实现IoC的多种方式