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

  

2. simple dependency checking

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
< 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 >

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

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

  

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

  


==============================================================================
本文转自被遗忘的博客园博客,原文链接:http://www.cnblogs.com/rollenholt/archive/2012/12/27/2835270.html,如需转载请自行联系原作者
相关文章
|
1月前
|
XML Java 数据格式
Spring5入门到实战------7、IOC容器-Bean管理XML方式(外部属性文件)
这篇文章是Spring5框架的实战教程,主要介绍了如何在Spring的IOC容器中通过XML配置方式使用外部属性文件来管理Bean,特别是数据库连接池的配置。文章详细讲解了创建属性文件、引入属性文件到Spring配置、以及如何使用属性占位符来引用属性文件中的值。
Spring5入门到实战------7、IOC容器-Bean管理XML方式(外部属性文件)
|
2月前
|
Java 测试技术 数据库
Spring Boot中的项目属性配置
本节课主要讲解了 Spring Boot 中如何在业务代码中读取相关配置,包括单一配置和多个配置项,在微服务中,这种情况非常常见,往往会有很多其他微服务需要调用,所以封装一个配置类来接收这些配置是个很好的处理方式。除此之外,例如数据库相关的连接参数等等,也可以放到一个配置类中,其他遇到类似的场景,都可以这么处理。最后介绍了开发环境和生产环境配置的快速切换方式,省去了项目部署时,诸多配置信息的修改。
|
27天前
|
Java Spring 开发者
Spring 框架配置属性绑定大比拼:@Value 与 @ConfigurationProperties,谁才是真正的王者?
【8月更文挑战第31天】Spring 框架提供 `@Value` 和 `@ConfigurationProperties` 两种配置属性绑定方式。`@Value` 简单直接,适用于简单场景,但处理复杂配置时略显不足。`@ConfigurationProperties` 则以类级别绑定配置,简化代码并更好组织配置信息。本文通过示例对比两者特点,帮助开发者根据具体需求选择合适的绑定方式,实现高效且易维护的配置管理。
34 0
|
30天前
|
缓存 Java 数据库连接
Spring Boot 资源文件属性配置,紧跟技术热点,为你的应用注入灵动活力!
【8月更文挑战第29天】在Spring Boot开发中,资源文件属性配置至关重要,它让开发者能灵活定制应用行为而不改动代码,极大提升了可维护性和扩展性。Spring Boot支持多种配置文件类型,如`application.properties`和`application.yml`,分别位于项目的resources目录下。`.properties`文件采用键值对形式,而`yml`文件则具有更清晰的层次结构,适合复杂配置。此外,Spring Boot还支持占位符引用和其他外部来源的属性值,便于不同环境下覆盖默认配置。通过合理配置,应用能快速适应各种环境与需求变化。
33 0
|
2月前
|
缓存 Java 开发者
Spring循环依赖问题之Spring循环依赖如何解决
Spring循环依赖问题之Spring循环依赖如何解决
|
1月前
|
前端开发 Java 测试技术
单元测试问题之在Spring MVC项目中添加JUnit的Maven依赖,如何操作
单元测试问题之在Spring MVC项目中添加JUnit的Maven依赖,如何操作
|
2月前
|
缓存 Java Spring
Spring循环依赖问题之Spring不支持构造器内的强依赖注入如何解决
Spring循环依赖问题之Spring不支持构造器内的强依赖注入如何解决
|
2月前
|
Java Spring
Spring循环依赖问题之构造器内的循环依赖如何解决
Spring循环依赖问题之构造器内的循环依赖如何解决
|
2月前
|
Java Spring 容器
Spring循环依赖问题之两个不同的Bean A,导致抛出异常如何解决
Spring循环依赖问题之两个不同的Bean A,导致抛出异常如何解决
|
2月前
|
存储 缓存 Java
Spring循环依赖问题之循环依赖异常如何解决
Spring循环依赖问题之循环依赖异常如何解决