美文网首页JAVA后台开发_从入门到精通
11 Spring使用@Required注解依赖检查

11 Spring使用@Required注解依赖检查

作者: 笑Skr人啊 | 来源:发表于2017-07-14 16:09 被阅读34次

    Spring依赖检查bean 配置文件用于确定的特定类型(基本,集合或对象)的所有属性被设置。在大多数情况下,你只需要确保特定属性已经设置但不是所有属性..

    对于这种情况,你需要 @Required 注解,请参见下面的例子:

    @Required示例

    Customer对象,适用@Required在 setPerson()方法,以确保 person 属性已设置。

    package com.gp6.dependencyCheck.required;
    
    import org.springframework.beans.factory.annotation.Required;
    
    public class Customer {
        private Person person;
        
        private String action;
        
        private int type;
    
    
        public String getAction() {
            return action;
        }
    
        public void setAction(String action) {
            this.action = action;
        }
    
        public int getType() {
            return type;
        }
    
        public void setType(int type) {
            this.type = type;
        }
    
        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.gp6.dependencyCheck.required.Customer">
            <property name="action" value="buy" />
            <property name="type" value="1" />
            <property name="person" ref="PersonBean" />
        </bean>
    
        <bean id="PersonBean" class="com.gp6.dependencyCheck.required.Person">
            <property name="name" value="gp6" />
            <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.gp6.dependencyCheck.required.Customer">
            <property name="action" value="buy" />
            <property name="type" value="1" />
            <!-- <property name="person" ref="PersonBean" /> -->
        </bean>
    
        <bean id="PersonBean" class="com.gp6.dependencyCheck.required.Person">
            <property name="name" value="gp6" />
            <property name="age" value="29" />
        </bean>
        
    </beans>
    

    如果你运行它,下面的错误信息会丢的,因为 person 的属性未设置。

    org.springframework.beans.factory.BeanInitializationException:  
        Property 'person' is required for bean 'CustomerBean'
    
    结论

    尝试@Required注解,它比依赖检查XML文件中更加灵活,因为它可以适用于只有一个特定属性。

    Spring自定义@Required-style注解

    @Required注解是用来确保特定属性已设置。如果您迁移现有项目到Spring框架或有自己的@Required-style注解,Spring允许您定义自定义@Required-style注解,相当于@Required注解。

    在这个例子中,您将创建一个名为 @Mandatory 定制 @Required-style 注解,相当于@Required注解。

    1.创建@MyRequired接口
    package com.gp6.dependencyCheck.myRequired;
    
    import java.lang.annotation.ElementType;
    import java.lang.annotation.Retention;
    import java.lang.annotation.RetentionPolicy;
    import java.lang.annotation.Target;
    
    @Retention(RetentionPolicy.RUNTIME)
    @Target(ElementType.METHOD)
    public @interface MyRequired {
        
    }
    
    
    2.应用它到属性
    package com.gp6.dependencyCheck.myRequired;
    
    public class Customer {
        private Person person;
        
        private String action;
        
        private int type;
    
    
        public String getAction() {
            return action;
        }
    
        public void setAction(String action) {
            this.action = action;
        }
    
        public int getType() {
            return type;
        }
    
        public void setType(int type) {
            this.type = type;
        }
    
        public Person getPerson() {
            return person;
        }
    
        @MyRequired
        public void setPerson(Person person) {
            this.person = person;
        }
    }
    
    
    3.注册它

    包函新@Mandatory注释到“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">
            <property name="requiredAnnotationType" value="com.gp6.dependencyCheck.myRequired.MyRequired"/>
        </bean>
        
        <bean id="CustomerBean" class="com.gp6.dependencyCheck.myRequired.Customer">
            <property name="action" value="buy" />
            <property name="type" value="1" />
            <property name="person" ref="PersonBean" />
        </bean>
        
        <bean id="PersonBean" class="com.gp6.dependencyCheck.myRequired.Person">
            <property name="name" value="gp6" />
            <property name="age" value="29" />
        </bean>
        
    </beans>
    
    4. 完成

    这样做,创建了一个新的自定义命名 @Required-style的@MyRequired 注解,相当于 @Required 注解

    相关文章

      网友评论

        本文标题:11 Spring使用@Required注解依赖检查

        本文链接:https://www.haomeiwen.com/subject/loonhxtx.html