zoukankan      html  css  js  c++  java
  • Spring学习(十一)-----Spring使用@Required注解依赖检查

    bean 配置文件用于确定的特定类型(基本,集合或对象)的所有属性被设置。在大多数情况下,你只需要确保特定属性已经设置但不是所有属性..
    对于这种情况,你需要 @Required 注解,请参见下面的例子:

    @Required示例

    Customer对象,适用@Required在 setPerson()方法,以确保 person 属性已设置。
    package com.yiibai.common;
    
    import org.springframework.beans.factory.annotation.Required;
    
    public class Customer 
    {
        private Person person;
        private int type;
        private String action;
        
        public Person getPerson() {
            return person;
        }
        @Required
        public void setPerson(Person person) {
            this.person = person;
        }
    }
    简单地套用@Required注解不会强制执行该属性的检查,还需要注册一个RequiredAnnotationBeanPostProcessor以了解在bean配置文件@Required注解。
    RequiredAnnotationBeanPostProcessor可以用两种方式来启用。

    1. 包函 <context:annotation-config />

    添加 Spring 上下文和 <context:annotation-config />在bean配置文件。
    <beans 
        ...
        xmlns:context="http://www.springframework.org/schema/context"
        ...
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-2.5.xsd">
        ...
        <context:annotation-config />
        ...
    </beans>

    完整的实例,

    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-2.5.xsd">
    
        <context:annotation-config />
    
        <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>

    2. 包函 RequiredAnnotationBeanPostProcessor

    直接在 bean 配置文件包函“RequiredAnnotationBeanPostProcessor”。
    <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 
    class="org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor"/>
        
        <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>
    如果你运行它,下面的错误信息会丢的,因为 person 的属性未设置。
    org.springframework.beans.factory.BeanInitializationException: 
        Property 'person' is required for bean 'CustomerBean'
    结论
    尝试@Required注解,它比依赖检查XML文件中更加灵活,因为它可以适用于只有一个特定属性。

    定义@Required

    作者:逆舟
    https://www.cnblogs.com/zy-jiayou/
    本博客文章均为作者原创,转载请注明作者和原文链接。
  • 相关阅读:
    [云计算&大数据]概念辨析:数据仓库 | 数据湖 | 数据中心 | 数据中台 | 数据平台 【待续】
    [云计算]概念辨析:云计算 [IaaS/PaaS/SaaS & 公有云/私有云/混合云]
    [Linux]浅析"command > /dev/null 2>&1 &" 与 "command 1>/dev/null 2>&1 &"
    [Python]【Form Data vs Request Payload】之 python 爬虫如何实现 POST request payload 形式的请求
    [Python]PyCharm中出现unresolved reference的解决方法
    [数据库/MYSQL]MYSQL开启Bin-Log
    scrapy采集gb2312网页中文乱码笔记
    uTools 桌面软件。
    Asp.Net Core Grpc 入门实践
    ASP.NET Core 中间件(Middleware)(一)
  • 原文地址:https://www.cnblogs.com/zy-jiayou/p/7700434.html
Copyright © 2011-2022 走看看