Spring的属性依赖检查

简介:

Spring支持4种依赖检查:默认的是none

    • 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.

举个例子:

1
2
3
4
5
6
7
8
public class Customer
{
     private Person person;
     private int type;
     private String action;
  
     //getter and setter methods
}

  

1
2
3
4
5
6
7
8
public class Person
{
     private String name;
     private String address;
     private int age;
  
     //getter and setter methods
}

  

1. none dependency checking

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
     xsi:schemaLocation="http://www.springframework.org/schema/beans
  
     < 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 >

  

2. simple dependency checking

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
     xsi:schemaLocation="http://www.springframework.org/schema/beans
  
     < 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 >

  注意此处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 dependency checking

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
     xsi:schemaLocation="http://www.springframework.org/schema/beans
  
     < 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 >

  此处故意没有设置”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 dependency checking

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
     xsi:schemaLocation="http://www.springframework.org/schema/beans
  
     < 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 >

  

Global default dependency checking:

default-dependency-check="all"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
     xsi:schemaLocation="http://www.springframework.org/schema/beans
     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 >
目录
相关文章
|
7月前
|
Java 测试技术 数据库
Spring Boot中的项目属性配置
本节课主要讲解了 Spring Boot 中如何在业务代码中读取相关配置,包括单一配置和多个配置项,在微服务中,这种情况非常常见,往往会有很多其他微服务需要调用,所以封装一个配置类来接收这些配置是个很好的处理方式。除此之外,例如数据库相关的连接参数等等,也可以放到一个配置类中,其他遇到类似的场景,都可以这么处理。最后介绍了开发环境和生产环境配置的快速切换方式,省去了项目部署时,诸多配置信息的修改。
|
XML Java 数据格式
深入探索Spring的Bean注入:四种方式解析与循环依赖探讨
深入探索Spring的Bean注入:四种方式解析与循环依赖探讨
221 0
Zp
|
Java Spring 容器
Spring Bean的循环依赖解决方案
Spring Bean的循环依赖解决方案
Zp
227 0
Spring Bean的循环依赖解决方案
|
9月前
|
XML 安全 Java
在Spring Boot 2.6中如何添加依赖项
在Spring Boot 2.6中如何添加依赖项
330 1
|
Java Spring
Spring AOP 未导入四个包导致的错误:
Spring AOP 未导入四个包导致的错误:
111 0
Spring AOP 未导入四个包导致的错误:
|
Java Spring 容器
Spring中PropertySource属性源配置文件的优先级、顺序问题大解析(加载流程)【享学Spring】(中)
Spring中PropertySource属性源配置文件的优先级、顺序问题大解析(加载流程)【享学Spring】(中)
Spring中PropertySource属性源配置文件的优先级、顺序问题大解析(加载流程)【享学Spring】(中)
|
Java 应用服务中间件 Spring
Spring中PropertySource属性源配置文件的优先级、顺序问题大解析(加载流程)【享学Spring】(上)
Spring中PropertySource属性源配置文件的优先级、顺序问题大解析(加载流程)【享学Spring】(上)
Spring中PropertySource属性源配置文件的优先级、顺序问题大解析(加载流程)【享学Spring】(上)
|
缓存 Java 中间件
Spring 循环依赖解析(下)
Spring 现在其实是我们 Java 程序开发离不开的基础框架,个人觉得除了 JDK 我们用得最多的 Java 中间件就是 Spring ,今天我们一起来学习一下 Spring 的循环依赖。
216 0
Spring 循环依赖解析(下)
|
缓存 Java 中间件
Spring 循环依赖解析(上)
Spring 现在其实是我们 Java 程序开发离不开的基础框架,个人觉得除了 JDK 我们用得最多的 Java 中间件就是 Spring ,今天我们一起来学习一下 Spring 的循环依赖。
133 0
Spring 循环依赖解析(上)
|
缓存 Java Spring
Spring支持所有循环依赖的情况吗?
循环依赖的情况如下: 构造器循环依赖(singleton,prototype) Setter注入循环依赖(singleton,prototype)
Spring支持所有循环依赖的情况吗?