在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/beanshttp://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/beanshttp://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/beanshttp://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/beanshttp://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/beanshttp://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依赖检查相关推荐

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

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

  2. Spring的属性依赖检查

    Spring支持4种依赖检查:默认的是none none – No dependency checking. simple – If any properties of primitive type ...

  3. 分享Spring中Bean的4种依赖检查模式

    http://developer.51cto.com/art/201104/253227.htm 下面我们来看看Spring中的Bean的4中依赖检查模式:simple,object,all,none ...

  4. spring 依赖注入总结

    一.问题引入 官方为什么推荐构造器注入? 构造器注入和属性注入的区别是啥? 你知道有几种注入方式吗? 二  注入方式 spring的注入方式就两种 配置注入,注解注入(自动装配) 而这两种方式都实现了 ...

  5. Spring依赖注入–字段vs设置器vs构造函数注入

    欢迎使用Spring Dependency Injection –字段,设置器,构造函数注入教程. 了解场注入 , 二传手注入和构造函数注入之间的区别. 借助代码示例,我们将看到使用每个示例的好处以及 ...

  6. Java程序员必须掌握的Spring依赖管理原理

    Spring依赖注入 依赖注入(Dependency Injection)的意思就是对象通过构造器函数参数,工厂方法的参数,或者成员属性,定义了对象的依赖对象:容器在创建该对象时会负责注入这些依赖.这 ...

  7. Spring依赖处理过程源码分析

    AbstractAutowireCapableBeanFactory#doCreateBean创建Bean AbstractAutowireCapableBeanFactory#populateBea ...

  8. Spring依赖注入与自动装配

    Spring依赖注入与自动装配 首先推荐狂神说的Spring讲义 1.Beans.xml作用 简而言之,我们通过在beans.xml中进行配置,将各种类交给spring来管理. 2.依赖注入 推荐狂神 ...

  9. arg是什么函数_java后端开发三年!你还不了解Spring 依赖注入,凭什么给你涨薪...

    前言 前两天和一个同学吃饭的时候同学跟我说了一件事,说他公司有个做了两年的人向他提出要涨薪资,他就顺口问了一个问题关于spring依赖注入的,那个要求涨薪的同学居然被问懵了...事后回家想了想这一块确 ...

最新文章

  1. sqlerror.java.1055,at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1055)
  2. Python3.5在Windows7环境下Scrapy库的安装
  3. jwt判断token是否过期_前端也得搞懂 JWT 这个知识点
  4. DES的原理及python实现
  5. 2018年度全球收入52强App发行商榜单公布:腾讯连续三年居首
  6. Java学习之FileInputStream与FileReader的区别
  7. Django项目:CRM(客户关系管理系统)--83--73PerfectCRM实现CRM模板统一
  8. CSS大美集(关于细节)
  9. CDN的基本原理和基础架构
  10. HMM中的前向法(转)
  11. 酷比魔方iplay20_799元买国产“山寨板”是否值得?酷比魔方iPlay20 Pro评测
  12. 小白怎么入门网络安全?
  13. python系列教程158——iter函数
  14. 【Android】Android 封装 Http 请求工具
  15. 人工智能助力三维几何自动化建模
  16. 王菲微博“逗贫”语录暴光
  17. CN_计算机网络性能指标@信道利用率@信道吞吐率
  18. BG22蓝牙——第三弹 蓝牙的一些入门知识,整理了大佬们的文章和链接
  19. 云服务器有多少防御,高防服务器防御一般有多少?
  20. linux open firmware,Linux 5.3继续推进英特尔的Sound Open Firmware声音固件

热门文章

  1. OpenGL deferred shading延迟渲染的实例
  2. C++智能指针简单介绍
  3. c++TCP的三次握手和四次挥手
  4. 怎么用matlab处理数据,如何用Matlab处理.wfm格式的数据
  5. 第十天:估算活动持续时间,类比估算,参数估算,自下而上估算,三点估算解析表
  6. 09_EGIT插件的安装,Eclipse中克隆(clone),commit,push,pull操作演示
  7. 1高并发服务器:多进程服务器
  8. 微擎jsapi支付必须传openid怎么解决_面经腾讯微信支付面试记
  9. 怎么将数据进行正太转化_想要将电脑音频进行录制怎么操作
  10. mysql 动态游标_mysql动态游标与mysql存储过程游标(示例)