《Spring攻略(第2版)》——1.10 用@Required注解检查属性

    xiaoxiao2024-05-19  108

    本节书摘来自异步社区《Spring攻略(第2版)》一书中的第1章,第1.10节,作者: 【美】Gary Mak , Josh Long , Daniel Rubio著,更多章节内容可以访问云栖社区“异步社区”公众号查看

    1.10 用@Required注解检查属性

    1.10.1 问题Spring的依赖检查功能仅能检查某些类型的所有属性。它的灵活性不够,不能仅检查特定的属性。在大部分情况下,你希望检查特定的属性是否设置,而不是特定类型的所有属性。

    1.10.2 解决方案RequiredAnnotationBeanPostProcessor是一个Spring bean后处理器,检查带有@Required注解的所有Bean属性是否设置。Bean后处理器是一类特殊的Spring bean,能够在每个Bean初始化之前执行附加的工作。为了启用这个Bean后处理器进行属性检查,必须在Spring IoC容器中注册它。注意,这个处理器只能检查属性是否已经设置,而不能测试属性是否非空。

    1.10.3 工作原理假定对于序列生成器来说,prefixGenerator和suffix属性都是必要的。你可以用@Required注解它们的设值方法。

    package com.apress.springrecipes.sequence; import org.springframework.beans.factory.annotation.Required; public class SequenceGenerator {    private PrefixGenerator prefixGenerator;    private String suffix;    ...    @Required    public void setPrefixGenerator(PrefixGenerator prefixGenerator) {      this.prefixGenerator = prefixGenerator;    }    @Required    public void setSuffix(String suffix) {      this.suffix = suffix;    }    ... }

    为了要求Spring检查所有序列生成器实例上这些属性是否已经设置,你必须在IoC容器中注册一个RequiredAnnotationBeanPostProcessor实例。如果你打算使用Bean工厂,就必须通过API注册这个Bean后处理器,否则,只能在应用上下文中声明这个Bean后处理器的实例。

    <bean class="org.springframework.beans.factory.annotation.![C:\Documents and Settings\cmm09a\桌面\0.tif{9}](/api/storage/getbykey/screenshow?key=1505ff37ffe85a509ae6)    RequiredAnnotationBeanPostProcessor" />

    如果你正在使用Spring 2.5或者更高版本,可以简单地在Bean配置文件中包含元素,这将自动地注册一个RequiredAnnotationBeanPostProcessor实例。

    <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-3.0.xsd      http://www.springframework.org/schema/context      http://www.springframework.org/schema/context/spring-context-3.0.xsd">    <context:annotation-config />    ... </beans>

    如果任何带有@Required的属性未设置,Bean后处理器将抛出一个BeanInitializationException异常。

    Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sequenceGenerator' defined in class path resource [beans.xml]: Initialization of bean failed; nested exception is org.springframework.beans.factory. BeanInitializationException: Property 'prefixGenerator' is required for bean 'sequenceGenerator'

    除了@Required注解之外,RequiredAnnotationBeanPostProcessor还能用自定义的注解检查属性。例如,你可以创建如下注解类型:

    package com.apress.springrecipes.sequence; ... @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) public @interface Mandatory { }

    然后将这个注解应用到必要属性的设值方法。

    package com.apress.springrecipes.sequence; public class SequenceGenerator {    private PrefixGenerator prefixGenerator;    private String suffix;    ...    @Mandatory    public void setPrefixGenerator(PrefixGenerator prefixGenerator) {      this.prefixGenerator = prefixGenerator;    }    @Mandatory    public void setSuffix(String suffix) {      this.suffix = suffix;    }    ... }

    为了用这个注解类型检查属性,你必须在RequiredAnnotationBeanPostProcessor的required AnnotationType属性中指定。

    <bean class="org.springframework.beans.factory.annotation.    RequiredAnnotationBeanPostProcessor">    <property name="requiredAnnotationType">      <value>com.apress.springrecipes.sequence.Mandatory</value>    </property> </bean>
    最新回复(0)