zoukankan      html  css  js  c++  java
  • Spring学习(十)-----Spring依赖检查

    在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/beans
        http://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/beans
        http://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/beans
        http://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/beans
        http://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/beans
        http://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 注解
    在大多数情况下,你只需要确保特定属性已经设置,但有一定是所有的类型(原始,集合或对象)属性。
    作者:逆舟
    https://www.cnblogs.com/zy-jiayou/
    本博客文章均为作者原创,转载请注明作者和原文链接。
  • 相关阅读:
    作业4.称体重
    一、虚拟环境.二、路由配置主页与404.三、2.x路由分发.四、伪静态.五、request对象.六、FBV与CBV.七、文件上传.
    一、数据库表中字段的增删改查,二、路由基础.三、有名无名分组.四、多app共存的路由分配.五、多app共存时模板冲突问题.六、创建app流程.七、路由分发.八、路由别名,九、名称空间.十、反向解析.十一、2.x新特性.十二、自定义转换器
    Django项目的创建与介绍.应用的创建与介绍.启动项目.pycharm创建启动项目.生命周期.三件套.静态文件.请求及数据.配置Mysql完成数据迁移.单表ORM记录的增删改查
    学习Django,http协议,
    值类型之间的相互转化,运算符,if条件判断,循环,函数
    js导读,js引入,js选择器,事件,操作页面文档,计算后样式,数据类型
    字体图标,盒子显隐,overflow属性,伪类设计边框,盒子阴影2d形变
    浮动布局,定位布局(固定定位,绝对定位,相对定位),过渡动画
    盒子总结,文本属性操作,reset操作,高级选择器,高级选择器优先级,边界圆角(了解),a标签的四大伪类,背景图片操作,背景图片之精灵图
  • 原文地址:https://www.cnblogs.com/zy-jiayou/p/7700375.html
Copyright © 2011-2022 走看看