在Spring中,可以使用依赖检查功能,以确保所要求的属性可设置或者注入。
依赖检查模式
4个依赖检查支持的模式:
  • none – 没有依赖检查,这是默认的模式。
  • simple – 如果基本类型(int, long,double…)和集合类型(map, list..)的任何属性都没有设置,UnsatisfiedDependencyException将被抛出。
  • objects – 如果对象类型的任何属性都没有设置,UnsatisfiedDependencyException将被抛出。
  • all – 如果任何类型的任何属性都没有被设置,UnsatisfiedDependencyException将被抛出。

注:默认模式是 none

示例

Customer和Person对象的示例。
package com.yiibai.common;

public class Customer
{
private Person person;
private int type;
private String action; //getter and setter methods
} package com.yiibai.common; public class Person
{
private String name;
private String address;
private int age; //getter and setter methods
}

1. none 依赖检查

Spring bean配置文件使用 “none” 依赖检查模式。
<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.yiibai.common.Customer" >
<property name="action" value="buy" />
</bean> <bean id="PersonBean" class="com.yiibai.common.Person">
<property name="name" value="yiibai" />
<property name="address" value="address ABC" />
<property name="age" value="29" />
</bean> </beans>
如果没有明确定义的依赖检查模式,其默认为“none”。没有依赖检查将执行。

2. simple 依赖检查

Spring bean的配置文件使用“simple”依赖检查模式。
<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.yiibai.common.Customer"
dependency-check="simple"> <property name="person" ref="PersonBean" />
<property name="action" value="buy" />
</bean> <bean id="PersonBean" class="com.yiibai.common.Person">
<property name="name" value="yiibai" />
<property name="address" value="address ABC" />
<property name="age" value="29" />
</bean>
</beans>
在“type”属性(基本类型或集合类型),如尚未设置,UnsatisfiedDependencyException将抛出。
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 依赖检查

Spring bean配置文件的 “objects”依赖检查模式。
<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.yiibai.common.Customer"
dependency-check="objects"> <property name="action" value="buy" />
<property name="type" value="1" />
</bean> <bean id="PersonBean" class="com.yiibai.common.Person">
<property name="name" value="yiibai" />
<property name="address" value="address ABC" />
<property name="age" value="29" />
</bean> </beans>
在'person'属性(对象类型),尚未设置,一个UnsatisfiedDependencyException将抛出。
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 依赖检查

Spring bean配置文件的“all”依赖检查模式。
<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.yiibai.common.Customer"
dependency-check="all"> <property name="action" value="buy" />
</bean> <bean id="PersonBean" class="com.yiibai.common.Person">
<property name="name" value="yiibai" />
<property name="address" value="address ABC" />
<property name="age" value="29" />
</bean> </beans>
对“simple”和“objects”模式的组合,如果有的话类型(原型,集合和对象)的任何属性都没有设置,一个UnsatisfiedDependencyException将被抛出。
全局默认的依赖检查
明确定义的依赖检查模式,每个Bean配置繁琐且容易出错,可以在<beans>根元素设置一个默认的依赖性检查属性强制<beans>根元素内声明的整个bean类适用此规则。然而,这根默认模式将通过一个bean自己指定的模式下可覆盖。
<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.yiibai.common.Customer">
<property name="action" value="buy" />
<property name="type" value="1" />
</bean> <bean id="PersonBean" class="com.yiibai.common.Person">
<property name="name" value="yiibai" />
<property name="address" value="address ABC" />
<property name="age" value="29" />
</bean> </beans>

在这个配置文件中声明所有的Bean类默都是“all”依赖检查模式。
@Required 注解

在大多数情况下,你只需要确保特定属性已经设置,但有一定是所有的类型(原始,集合或对象)属性。

Spring学习(十)-----Spring依赖检查的更多相关文章

  1. Spring学习(十一)-----Spring使用@Required注解依赖检查

    Spring学习(九)-----Spring依赖检查 bean 配置文件用于确定的特定类型(基本,集合或对象)的所有属性被设置.在大多数情况下,你只需要确保特定属性已经设置但不是所有属性.. 对于这种 ...

  2. Spring学习笔记之依赖的注解(2)

    Spring学习笔记之依赖的注解(2) 1.0 注解,不能单独存在,是Java中的一种类型 1.1 写注解 1.2 注解反射 2.0 spring的注解 spring的 @Controller@Com ...

  3. Spring学习(六)-----Spring使用@Autowired注解自动装配

    Spring使用@Autowired注解自动装配 在上一篇 Spring学习(三)-----Spring自动装配Beans示例中,它会匹配当前Spring容器任何bean的属性自动装配.在大多数情况下 ...

  4. Spring使用@Required注解依赖检查

    Spring依赖检查 bean 配置文件用于确定的特定类型(基本,集合或对象)的所有属性被设置.在大多数情况下,你只需要确保特定属性已经设置但不是所有属性.. 对于这种情况,你需要 @Required ...

  5. Spring学习(十九)----- Spring的五种事务配置详解

    前段时间对Spring的事务配置做了比较深入的研究,在此之间对Spring的事务配置虽说也配置过,但是一直没有一个清楚的认识.通过这次的学习发觉Spring的事务配置只要把思路理清,还是比较好掌握的. ...

  6. Spring学习笔记——Spring依赖注入原理分析

    我们知道Spring的依赖注入有四种方式,各自是get/set方法注入.构造器注入.静态工厂方法注入.实例工厂方法注入 以下我们先分析下这几种注入方式 1.get/set方法注入 public cla ...

  7. spring学习12 -Spring 框架模块以及面试常见问题注解等

    以下为spring常见面试问题: 1.Spring 框架中都用到了哪些设计模式? Spring框架中使用到了大量的设计模式,下面列举了比较有代表性的: 代理模式—在AOP和remoting中被用的比较 ...

  8. Spring学习【Spring概述】

    从本文開始,我们就要一起学习Spring框架,首先不得不说Spring框架是一个优秀的开源框架. 当中採用IoC原理实现的基于Java Beans的配置管理和AOP的思想都是非常值得学习与使用的.以下 ...

  9. Spring学习笔记——Spring中lazy-init与abstract具体解释

    Spring的懒载入的作用是为了避免无谓的性能开销,就是当真正须要数据的时候才去运行数据的载入操作.不只在Spring中.我们在实际的编码过程中也应该借鉴这种思想,来提高我们程序的效率. 首先我们看一 ...

随机推荐

  1. C/C++——指针,引用做函数形参

    函数中的形参是普通形参的时,函数只是操纵的实参的副本,而无法去修改实参. 引用形参是对实参的直接操纵,指针形参是对 它所指向的值(*p) 的直接操纵,但是对于这个指针变量(p)来说,依然只是副本. 指 ...

  2. 智慧监狱来了!SaCa EMM 助推现代监狱建设迈上新台阶

    近几年来,移动化已经成为警务信息化建设的必然方向,为紧急和突发事件的处理提供了信息依据.为监狱民警提供移动警务所需的信息管理系统,司法系统从很早就开始推动警务通项目.为了落实移动警务的工作需求,很多监 ...

  3. No.1 - 制作一个简单的菜单动画效果---百度IFE

    最近比较闲,在家做点训练 http://ife.baidu.com/course/detail/id/18?t=1527144851578#learn CSS3新特性,兼容性,兼容方法总结 https ...

  4. Ubuntu 14.04 系统安装后无法上网的问题(eth0识别不出)

    Ubuntu 14.04 1.网口处网线状态等正常跳动 2.ifconfig 指令查询不到网卡信息 说明缺少了网卡驱动. 使用 lspci 指令查看系统中所有的驱动信息,找到 Ethernet Con ...

  5. PAT——1021. 个位数统计

    给定一个k位整数N = dk-1*10k-1 + ... + d1*101 + d0 (0<=di<=9, i=0,...,k-1, dk-1>0),请编写程序统计每种不同的个位数字 ...

  6. Android Asynctask与Handler的比较,优缺点区别,Asynctask源码

    1  AsyncTask实现的原理,和适用的优缺点 AsyncTask,是android提供的轻量级的异步类,可以直接继承AsyncTask,在类中实现异步操作,并提供接口反馈当前异步执行的程度(可以 ...

  7. Segmentation fault (core dumped) 错误的一种解决场景

    错误类型 Segmentation fault (core dumped) 产生原因 Segmentation fault 段错误. Core Dump 核心转储(是操作系统在进程收到某些信号而终止运 ...

  8. 搭建springboot项目

    1.搭建环境windows10+jdk1.8+eclipse4.8+maven 2.为了学习微服务架构学习搭建基础项目 3.分为两种搭建方式为maven项目和单独建立springboot项目(ecli ...

  9. iOS 推送功能打包后获取不到deviceToken

    公司项目用ionic3构建, 用了极光推送插件(cordova-plugin-jpush). 开发时一切将各种Bundle Id, 推送证书等都绑定完测试一切正常. 可是要给测试人员打Ad-Hoc包时 ...

  10. NIO流—理解Buffer、Channel概念和NIO的读写操作

    NIO流与IO流的区别 面向流与面向块 IO流是每次处理一个或多个字节,效率很慢(字符流处理的也是字节,只是对字节进行编码和解码处理). NIO流是以数据块为单位来处理,缓冲区就是用于读写的数据块.缓 ...