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的更多相关文章

  1. Spring dependency checking with @Required Annotation

    Spring's dependency checking in bean configuration file is used to make sure all properties of a cer ...

  2. spring properties resolve 问题

    在stackoverflow上看到一个问题 配置如下: <context:property-placeholder location="/WEB-INF/application-cus ...

  3. Benefits of Using the Spring Framework Dependency Injection 依赖注入 控制反转

    小结: 1. Dependency Injection is merely one concrete example of Inversion of Control. 依赖注入是仅仅是控制反转的一个具 ...

  4. [Spring] Properties for project configuration

    We might have some project specific configuration need to setup. The good approach to do this in Spr ...

  5. spring security maven dependency

    Unable to locate Spring NamespaceHandler for XML schema namespace [ spring secutity dependency: < ...

  6. 【Spring源码分析】.properties文件读取及占位符${...}替换源码解析

    前言 我们在开发中常遇到一种场景,Bean里面有一些参数是比较固定的,这种时候通常会采用配置的方式,将这些参数配置在.properties文件中,然后在Bean实例化的时候通过Spring将这些.pr ...

  7. Spring boot 配置文件详解 (properties 和yml )

    从其他框架来看 我们都有自己的配置文件, hibernate有hbm,mybatis 有properties, 同样, Spring boot 也有全局配置文件. Springboot使用一个全局的配 ...

  8. eclips环境下开发spring boot项目,application.properties配置文件下中文乱码解决方案

    如以上,application.properties文件下中文乱码.发生乱码一般都是由于编码格式不一样导致的. 打开Window-Preferences-General-content Types-T ...

  9. spring boot项目,application.properties配置文件下中文乱码解决方案

    转自:https://blog.csdn.net/qq_40408534/article/details/79831807 如以上,application.properties文件下中文乱码.发生乱码 ...

随机推荐

  1. When you’re nearly 40 and unmarried, and you realize you’re going to be okay

    https://medium.com/the-gathering-kind/when-you-re-nearly-40-and-unmarried-and-you-realize-you-re-goi ...

  2. Android View 绘制过程

    Android的View绘制是从根节点(Activity是DecorView)开始,他是一个自上而下的过程.View的绘制经历三个过程:Measure.Layout.Draw.基本流程如下图: per ...

  3. 【转】android 自定义控件

    Android自定义View实现很简单 继承View,重写构造函数.onDraw,(onMeasure)等函数. 如果自定义的View需要有自定义的属性,需要在values下建立attrs.xml.在 ...

  4. 实际举例C#引用类型和值类型的区别<网摘>

    我们都知道,c#的两大数据类型分别为值类型和引用类型.很多人或许闭着眼睛都能说出值类型包括简单类型.结构体类型和枚举类型,引用类型包括自定义类.数组.接口.委托等,但是当被问及到二者之间的联系和区别, ...

  5. Ajax、Comet与Websocket

    从 http 协议说起 1996年IETF  HTTP工作组发布了HTTP协议的1.0版本 ,到现在普遍使用的版本1.1,HTTP协议经历了17 年的发展.这种分布式.无状态.基于TCP的请求/响应式 ...

  6. 生产者和消费者(.net实现)

    生产者和消费者,是多线程中的经典问题,听过java方面的这个问题的培训,闲暇时用.net实现了这 个问题.在此实现的是,生产一个消息后,消费一个消息,再生产一个消息,循环往复.  1.消息代码  us ...

  7. 最简单的视音频播放示例7:SDL2播放RGB/YUV

    本文记录SDL播放视频的技术.在这里使用的版本是SDL2.实际上SDL本身并不提供视音频播放的功能,它只是封装了视音频播放的底层API.在Windows平台下,SDL封装了Direct3D这类的API ...

  8. 入门视频采集与处理(学会分析YUV数据)

    做视频采集与处理,自然少不了要学会分析YUV数据.因为从采集的角度来说,一般的视频采集芯片输出的码流一般都是YUV数据流的形式,而从视频处理(例如H.264.MPEG视频编解码)的角度来说,也是在原始 ...

  9. 【树状数组(二叉索引树)】轻院热身—candy、NYOJ-116士兵杀敌(二)

    [概念] 转载连接:树状数组 讲的挺好. 这两题非常的相似,查询区间的累加和.更新结点.Add(x,d) 与 Query(L,R) 的操作 [题目链接:candy] 唉,也是现在才发现这题用了这个知识 ...

  10. printk 驱动调试

    驱动的调试,printk()添加调试信息 printk相当于printf的孪生姐妹,它们一个运行在用户态,另一个则在内核态. 需要包含<linux/device.h>或者<linux ...