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/beanshttp://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/beanshttp://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/beanshttp://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/beanshttp://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/beanshttp://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.
转载于:https://www.cnblogs.com/ghgyj/p/4748233.html
Spring properties dependency checking相关推荐
- Spring properties定义bean
2019独角兽企业重金招聘Python工程师标准>>> Spring提供了丰富的标签和注解来进行bean的定义,除此之外框架来提供了扩展机制让使用可以通过properties来定义b ...
- Spring Boot 配置 jar 包外面的 Spring Properties 文件
一.概述 Properties 文件是我们可以用来存储项目特定信息的常用方法.理想情况下,我们应该将其保留在 jar 包之外,以便能够根据需要对配置进行更改. 在这个教程中,我们将研究在 Spring ...
- Spring Cloud项目是如何读取bootstrap.properties文件的?
提前说明:关于Spring Cloud和Spring Boot源码分析基于的版本如下所示 <!-- Spring Dependencies --> <dependency> & ...
- 解决spring问题(Unsatisfied dependency)的意外发现
前几天在做一个Dao的单元测试的时候,碰到了一个spring的错误.如下: [quote]org.springframework.beans.factory.UnsatisfiedDependency ...
- Spring的属性依赖检查
Spring支持4种依赖检查:默认的是none none – No dependency checking. simple – If any properties of primitive type ...
- spring IOC 之篇三:默认标签的解析
private void parseDefaultElement(Element ele, BeanDefinitionParserDelegate delegate) {// 对import标签的处 ...
- Spring Security 案例实现和执行流程剖析
在线演示 演示地址:http://139.196.87.48:9002/kitty 用户名:admin 密码:admin Spring Security Spring Security 是 Sprin ...
- Spring 源码分析(七)--bean的加载详细分析
一:缓存中获取单例bean 前面已经提到过,单例在Spring的同一个容器内只会被创建一次,后续再获取bean直接从单例缓存中获取,当然这里也只是尝试加载,首先尝试从缓存中加载,然后再次尝试从sing ...
- Spring的bean定义 2 : 通用bean定义逻辑 -- AbstractBeanDefinition
概述 AbstractBeanDefinition是最终全功能BeanDefinition实现类的基类,也就是这些类的共同属性和公共逻辑实现. AbstractBeanDefinition中并没有太复 ...
- .spring 知识点总结
软件151 马清友 一.sping是什么? 在了解Spring之前,我们来了解在javaEE框架下企业级开发采用EJB框架的一些不足: (1) EJB太笨重,而且Entity EJB不能脱离容器 ( ...
最新文章
- mysql的query cache_MySQL 缓存 Query Cache
- 设计模式之: 装饰器模式
- eslint常用设置;eslint关闭驼峰命名;eslint关闭全等于===校验;eslint关闭未定义变量报错;eslint关闭声明后未使用变量报错;eslint关闭单闭合标签校验;
- vb如何定义微软服务器stul,VBScrip微软官方教程.doc
- HDU-1069 Monkey and Banana
- ZZULIOJ 1114: 逆序
- 力扣812.最大三角形面积
- ng-bind-html在ng-repeat中问题的解决办法
- 详解apply的用处
- 硕士研究生计算机专业录取分数线,全国所计算机研究生录取分数线.doc
- window MFC桌面下雪程序
- 红杉、IDG、北极光、顺为等投资大咖怎么看智能硬件
- 内存泄漏的原因及解决方法
- php实现兼容Unicode文字的字符串大小写转换strtolower()和strtoupper()
- ARM64开发板配置Java环境 OrangePi
- 如何将数据设计成报表模板
- H5的APP逆向方法
- You-Get,多网站视频下载工具,非常方便
- 基于Scrapy的链家二手房爬虫
- 故障分析 | 从 data_free 异常说起