2.9 Spring Framework 5.x 之基于注解的容器配置

注解是否比配置Spring的XML更好?

基于注解的配置的引入引发了这种方法是否比XML“更好”的问题。简短的回答是“它取决于。”长期的答案是每种方法都有其优点和缺点,通常,由开发人员决定哪种策略更适合他们。由于它们的定义方式,注解在其声明中提供了大量上下文,从而导致更短更简洁的配置。但是,XML擅长在不触及源代码或重新编译它们的情况下连接组件。一些开发人员更喜欢将布线靠近源,而另一些开发人员则认为注解类不再是POJO,而且配置变得分散且难以控制。

无论选择如何,Spring都可以兼顾两种风格,甚至可以将它们混合在一起。值得指出的是,通过其JavaConfig选项,Spring允许以非侵入方式使用注解,而无需触及目标组件源代码,并且在工具方面,Spring Tool Suite支持所有配置样式 。

基于注解的配置提供了XML设置的替代方案,该配置依赖于字节码元数据来连接组件而不是角括号声明。开发人员不是使用XML来描述bean连接,而是通过在相关的类,方法或字段声明上使用注解将配置移动到组件类本身。如示例中所述:RequiredAnnotationBeanPostProcessor使用BeanPostProcessor与注解结合使用是扩展Spring IoC容器的常用方法。

例如,Spring 2.0引入了使用@Required注解强制执行所需属性的可能性。

Spring 2.5使得有可能采用相同的通用方法来驱动Spring的依赖注入。基本上,@Autowired注解提供与自动装配协作者中描述的相同的功能,但具有更细粒度的控制和更广泛的适用性。Spring 2.5还增加了对JSR-250注解的支持,例如 @PostConstruct和@PreDestroy。

Spring 3.0增加了对javax.inject包中包含的JSR-330(Java的依赖注入)注解的支持,例如@Inject 和@Named。有关这些注解的详细信息,请参阅 相关章节。

在注入XML之前执行注解注入。因此,XML配置会覆盖通过这两种方法连接的属性的注解。

与往常一样,您可以将它们注册为单独的bean定义,但也可以通过在基于XML的Spring配置中包含以下标记来隐式注册它们(请注意包含context命名空间):

<?xml version="1.0" encoding="UTF-8"?>
<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.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">

    <context:annotation-config/>

</beans>

在隐式注册后处理器包括 AutowiredAnnotationBeanPostProcessor, CommonAnnotationBeanPostProcessor, PersistenceAnnotationBeanPostProcessor,和前面提到的 RequiredAnnotationBeanPostProcessor。)

context:annotation-config/仅查找在定义它的同一应用程序上下文中的bean上的注释。这意味着,如果你context:annotation-config/输入一个WebApplicationContextfor DispatcherServlet,它只会检查@Autowired你的控制器中的bean,而不是你的服务。有关更多信息,请参阅 DispatcherServlet。

1.9.1. @Required 注解

该@Required注解适用于bean属性setter方法,如下面的例子:

public class SimpleMovieLister {

    private MovieFinder movieFinder;

    @Required
    public void setMovieFinder(MovieFinder movieFinder) {
        this.movieFinder = movieFinder;
    }

    // ...
}

此注解指示必须在配置时通过bean定义中的显式属性值或通过自动装配填充受影响的bean属性。如果尚未填充受影响的bean属性,则容器将引发异常。这允许急切和明确的失败,以后避免NullPointerException实例等。我们仍然建议您将断言放入bean类本身(例如,转换为init方法)。即使您在容器外使用类,这样做也会强制执行那些必需的引用和值。

1.9.2 @Autowired 注解

在本节中包含的示例中,@Inject可以使用JSR 330的注解代替Spring的@Autowired注释。有关详细信息,请参见此处

您可以将@Autowired注解应用于构造函数,如以下示例所示:

public class MovieRecommender {

    private final CustomerPreferenceDao customerPreferenceDao;

    @Autowired
    public MovieRecommender(CustomerPreferenceDao customerPreferenceDao) {
        this.customerPreferenceDao = customerPreferenceDao;
    }

    // ...
}

从Spring Framework 4.3开始,@Autowired如果目标bean只定义了一个开头的构造函数,则不再需要对这样的构造函数进行注解。但是,如果有几个构造函数可用,则必须注释至少一个构造函数,以教导容器使用哪一个。

您还可以将@Autowired注解应用于“传统”setter方法,如以下示例所示:

public class SimpleMovieLister {

    private MovieFinder movieFinder;

    @Autowired
    public void setMovieFinder(MovieFinder movieFinder) {
        this.movieFinder = movieFinder;
    }

    // ...
}

您还可以将注解应用于具有任意名称和多个参数的方法,如以下示例所示:

public class MovieRecommender {

    private MovieCatalog movieCatalog;

    private CustomerPreferenceDao customerPreferenceDao;

    @Autowired
    public void prepare(MovieCatalog movieCatalog,
            CustomerPreferenceDao customerPreferenceDao) {
        this.movieCatalog = movieCatalog;
        this.customerPreferenceDao = customerPreferenceDao;
    }

    // ...
}

您也可以应用于@Autowired字段,甚至可以将其与构造函数混合使用,如以下示例所示:

public class MovieRecommender {

    private final CustomerPreferenceDao customerPreferenceDao;

    @Autowired
    private MovieCatalog movieCatalog;

    @Autowired
    public MovieRecommender(CustomerPreferenceDao customerPreferenceDao) {
        this.customerPreferenceDao = customerPreferenceDao;
    }

    // ...
}

确保您的目标组件(例如,MovieCatalog或CustomerPreferenceDao)始终由您用于@Autowired注释注入点的类型声明。否则,由于在运行时未找到类型匹配,注入可能会失败。

对于通过类路径扫描找到的XML定义的bean或组件类,容器通常预先知道具体类型。但是,对于@Bean工厂方法,您需要确保声明的返回类型具有足够的表现力。对于实现多个接口的组件或可能由其实现类型引用的组件,请考虑在工厂方法上声明最具体的返回类型(至少与引用bean的注入点所需的特定类型一致)。

您还可以ApplicationContext通过将注解添加到需要该类型数组的字段或方法来提供特定类型的所有bean ,如以下示例所示:

public class MovieRecommender {

    @Autowired
    private MovieCatalog[] movieCatalogs;

    // ...
}

这同样适用于类型化集合,如以下示例所示:

public class MovieRecommender {

    private Set<MovieCatalog> movieCatalogs;

    @Autowired
    public void setMovieCatalogs(Set<MovieCatalog> movieCatalogs) {
        this.movieCatalogs = movieCatalogs;
    }

    // ...
}

如果希望按特定顺序对数组或列表中的项进行排序,则目标bean可以实现org.springframework.core.Ordered接口或使用@Order或标准@Priority注释。否则,它们的顺序遵循容器中相应目标bean定义的注册顺序。

您可以@Order在目标类级别和@Bean方法上声明注释,可能是通过单个bean定义(在多个定义使用相同bean类的情况下)。@Order值可能会影响注入点的优先级,但要注意它们不会影响单例启动顺序,这是由依赖关系和@DependsOn声明确定的正交关注点。

请注意,标准javax.annotation.Priority注释在@Bean级别上不可用 ,因为它无法在方法上声明。它的语义可以通过@Order值与@Primary每种类型的单个bean 相结合来建模。

即使是类型化的Map实例也可以自动装配,只要预期的键类型是String。Map值包含所有期望类型的bean,并且键包含相应的bean名称,如以下示例所示:

public class MovieRecommender {

    private Map<String, MovieCatalog> movieCatalogs;

    @Autowired
    public void setMovieCatalogs(Map<String, MovieCatalog> movieCatalogs) {
        this.movieCatalogs = movieCatalogs;
    }

    // ...
}

默认情况下,只要零候选bean可用,自动装配就会失败。默认行为是将带注释的方法,构造函数和字段视为指示所需的依赖项。您可以在以下示例中更改此行为:

public class SimpleMovieLister {

    private MovieFinder movieFinder;

    @Autowired(required = false)
    public void setMovieFinder(MovieFinder movieFinder) {
        this.movieFinder = movieFinder;
    }

    // ...
}

每个类只能标记一个带注解的构造函数,但可以注解多个非必需的构造函数。在这种情况下,每个都被认为是候选者之一,Spring使用最贪婪的构造函数,其依赖性可以得到满足 - 也就是说,具有最多参数的构造函数。

@Autowired建议使用必需属性而不是@Required注解。required属性表示该属性不是自动装配所必需的。如果无法自动装配,则会忽略该属性。@Required另一方面,它更强大,因为它强制执行由容器支持的任何方式设置的属性。如果未注入任何值,则会引发相应的异常。

或者,您可以通过Java 8表达特定依赖关系的非必需特性java.util.Optional,如以下示例所示:

public class SimpleMovieLister {

    @Autowired
    public void setMovieFinder(Optional<MovieFinder> movieFinder) {
        ...
    }
}

从Spring Framework 5.0开始,您还可以使用@Nullable注解(任何包中的任何类型的注解 - 例如,javax.annotation.Nullable来自JSR-305):

public class SimpleMovieLister {

    @Autowired
    public void setMovieFinder(@Nullable MovieFinder movieFinder) {
        ...
    }
}

您还可以使用@Autowired对于那些众所周知的解析依赖接口:BeanFactory,ApplicationContext,Environment,ResourceLoader, ApplicationEventPublisher,和MessageSource。这些接口及其扩展接口(如ConfigurableApplicationContext或ResourcePatternResolver)会自动解析,无需特殊设置。以下示例自动装配一个ApplicationContext对象:

public class MovieRecommender {

    @Autowired
    private ApplicationContext context;

    public MovieRecommender() {
    }

    // ...
}

在@Autowired,@Inject,@Resource,和@Value注释由Spring处理 BeanPostProcessor实现。这意味着您无法在自己的类型BeanPostProcessor或BeanFactoryPostProcessor类型(如果有)中应用这些注释。必须使用XML或Spring @Bean方法显式地“连接”这些类型。

1.9.3 @Primary 注解

由于按类型自动装配可能会导致多个候选人,因此通常需要对选择过程有更多控制权。实现这一目标的一种方法是使用Spring的 @Primary注释。@Primary表示当多个bean可以自动装配到单值依赖项时,应该优先选择特定的bean。如果候选者中只存在一个主bean,则它将成为自动装配的值

请考虑以下定义firstMovieCatalog为主要的配置MovieCatalog:

@Configuration
public class MovieConfiguration {

    @Bean
    @Primary
    public MovieCatalog firstMovieCatalog() { ... }

    @Bean
    public MovieCatalog secondMovieCatalog() { ... }

    // ...
}

使用上述配置,以下MovieRecommender内容自动装配 firstMovieCatalog:

public class MovieRecommender {

    @Autowired
    private MovieCatalog movieCatalog;

    // ...
}

相应的bean定义如下:

<?xml version="1.0" encoding="UTF-8"?>
<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.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">

    <context:annotation-config/>

    <bean class="example.SimpleMovieCatalog" primary="true">
        <!-- inject any dependencies required by this bean -->
    </bean>

    <bean class="example.SimpleMovieCatalog">
        <!-- inject any dependencies required by this bean -->
    </bean>

    <bean id="movieRecommender" class="example.MovieRecommender"/>

</beans>

1.9.4 @Qualifier注解

@Primary当可以确定一个主要候选者时,是通过具有多个实例的类型使用自动装配的有效方式。当您需要更多控制选择过程时,可以使用Spring的@Qualifier注释。您可以将限定符值与特定参数相关联,缩小类型匹配集,以便为每个参数选择特定的bean。在最简单的情况下,这可以是一个简单的描述性值,如以下示例所示:

public class MovieRecommender {

    @Autowired
    @Qualifier("main")
    private MovieCatalog movieCatalog;

    // ...
}

您还可以@Qualifier在各个构造函数参数或方法参数上指定注释,如以下示例所示:

public class MovieRecommender {

    private MovieCatalog movieCatalog;

    private CustomerPreferenceDao customerPreferenceDao;

    @Autowired
    public void prepare(@Qualifier("main")MovieCatalog movieCatalog,
            CustomerPreferenceDao customerPreferenceDao) {
        this.movieCatalog = movieCatalog;
        this.customerPreferenceDao = customerPreferenceDao;
    }

    // ...
}

以下示例显示了相应的bean定义。

<?xml version="1.0" encoding="UTF-8"?>
<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.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">

    <context:annotation-config/>

    <bean class="example.SimpleMovieCatalog">
        <qualifier value="main"/> 

        <!-- inject any dependencies required by this bean -->
    </bean>

    <bean class="example.SimpleMovieCatalog">
        <qualifier value="action"/> 

        <!-- inject any dependencies required by this bean -->
    </bean>

    <bean id="movieRecommender" class="example.MovieRecommender"/>

</beans>
  • 具有main限定符值的bean与使用相同值限定的构造函数参数连接。
  • 具有action限定符值的bean与使用相同值限定的构造函数参数连接。

对于回退匹配,bean名称被视为默认限定符值。因此,可以用一个定义bean id的main代替嵌套限定符元素,导致相同的匹配结果。但是,尽管您可以使用此约定来按名称引用特定bean,但@Autowired基本上是关于具有可选语义限定符的类型驱动注入。这意味着即使使用bean名称回退,限定符值在类型匹配集中也总是具有缩小的语义。它们在语义上不表示对唯一bean的引用id。良好限定的值是main 或EMEA或persistent,表达独立于从所述豆的特定部件的特性id,在匿名bean定义的情况下可以自动生成,例如前面例子中的那个。

限定符也适用于类型集合,如前所述 - 例如,to Set。在这种情况下,根据声明的限定符,所有匹配的bean都作为集合注入。这意味着限定符不必是唯一的。相反,它们构成过滤标准。例如,您可以MovieCatalog使用相同的限定符值“action” 定义多个bean,所有这些bean都注入带Set注释的注释中@Qualifier(“action”)。

在类型匹配候选项中,根据目标bean名称选择限定符值,不需要@Qualifier在注入点处进行注释。如果没有其他解析指示符(例如限定符或主标记),则对于非唯一依赖性情况,Spring会将注入点名称(即字段名称或参数名称)与目标bean名称进行匹配,然后选择同名的候选人,如果有的话。

也就是说,如果您打算按名称表达注释驱动的注入@Autowired,即使它能够在类型匹配候选者中通过bean名称进行选择,也不要主要使用。相反,使用JSR-250 @Resource注释,该注释在语义上定义为通过其唯一名称标识特定目标组件,声明的类型与匹配过程无关。@Autowired具有相当不同的语义:在按类型选择候选bean之后,String 仅在那些类型选择的候选中考虑指定的限定符值(例如,将account限定符与标记有相同限定符标签的bean 匹配)。

对于自身定义为集合Map或数组类型的bean来说,这@Resource 是一个很好的解决方案,它通过唯一名称引用特定的集合或数组bean。也就是说,从4.3版本开始,只要元素类型信息保留在返回类型签名或集合继承层次结构中,您就可以Map通过Spring的@Autowired类型匹配算法匹配和数组类型 @Bean。在这种情况下,您可以使用限定符值在相同类型的集合中进行选择,如上一段所述。

从4.3开始,@Autowired还考虑了自引用注入(即,引用回到当前注入的bean)。请注意,自我注入是一种后备。对其他组件的常规依赖性始终具有优先权。从这个意义上说,自我引用并不参与常规的候选人选择,因此特别是不是主要的。相反,它们总是最低优先级。在实践中,您应该仅使用自引用作为最后的手段(例如,通过bean的事务代理调用同一实例上的其他方法)。考虑在这种情况下将受影响的方法分解为单独的委托bean。或者,您可以使用@Resource,它可以通过其唯一名称获取代理回到当前bean。

@Autowired适用于字段,构造函数和多参数方法,允许在参数级别缩小限定符注释。相比之下,@Resource 仅支持具有单个参数的字段和bean属性setter方法。因此,如果您的注入目标是构造函数或多参数方法,则应该使用限定符。

您可以创建自己的自定义限定符注释。为此,请定义注释并@Qualifier在定义中提供注释,如以下示例所示:

@Target({ElementType.FIELD, ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
@Qualifier
public @interface Genre {

    String value();
}

然后,您可以在自动装配的字段和参数上提供自定义限定符,如以下示例所示:

public class MovieRecommender {

    @Autowired
    @Genre("Action")
    private MovieCatalog actionCatalog;

    private MovieCatalog comedyCatalog;

    @Autowired
    public void setComedyCatalog(@Genre("Comedy") MovieCatalog comedyCatalog) {
        this.comedyCatalog = comedyCatalog;
    }

    // ...
}

接下来,您可以提供候选bean定义的信息。您可以将标记添加为 标记的子元素,然后指定type和 value匹配自定义限定符注释。该类型与注释的完全限定类名匹配。或者,为方便起见,如果不存在冲突名称的风险,您可以使用短类名称。以下示例演示了两种方法:

<?xml version="1.0" encoding="UTF-8"?>
<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.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">

    <context:annotation-config/>

    <bean class="example.SimpleMovieCatalog">
        <qualifier type="Genre" value="Action"/>
        <!-- inject any dependencies required by this bean -->
    </bean>

    <bean class="example.SimpleMovieCatalog">
        <qualifier type="example.Genre" value="Comedy"/>
        <!-- inject any dependencies required by this bean -->
    </bean>

    <bean id="movieRecommender" class="example.MovieRecommender"/>

</beans>

在类路径扫描和托管组件中,您可以看到基于注释的替代方法,即以XML格式提供限定符元数据。具体来说,请参阅使用注释提供限定符元数据。

在某些情况下,使用没有值的注释可能就足够了。当注解用于更通用的目的并且可以跨多种不同类型的依赖项应用时,这可能很有用。例如,您可以提供可在没有Internet连接时搜索的脱机目录。首先,定义简单注解,如以下示例所示:

@Target({ElementType.FIELD, ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
@Qualifier
public @interface Offline {

}

然后将注解添加到要自动装配的字段或属性中,如以下示例所示:

public class MovieRecommender {

    @Autowired
    @Offline 
    private MovieCatalog offlineCatalog;

    // ...
}

此行添加@Offline注释。
现在bean定义只需要一个限定符type,如下例所示:

<bean class="example.SimpleMovieCatalog">
    <qualifier type="Offline"/> 
    <!-- inject any dependencies required by this bean -->
</bean>

此元素指定限定符。

您还可以定义除简单value属性之外或代替简单属性接受命名属性的自定义限定符注释。如果随后在要自动装配的字段或参数上指定了多个属性值,则bean定义必须匹配所有此类属性值才能被视为自动装配候选。例如,请考虑以下注解定义:

@Target({ElementType.FIELD, ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
@Qualifier
public @interface MovieQualifier {

    String genre();

    Format format();
}

在这种情况下Format是一个枚举,定义如下:

public enum Format {
    VHS, DVD, BLURAY
}

要自动装配的字段使用自定义限定符进行注释,并包含两个属性的值:genre并且format,如以下示例所示:

public class MovieRecommender {

    @Autowired
    @MovieQualifier(format=Format.VHS, genre="Action")
    private MovieCatalog actionVhsCatalog;

    @Autowired
    @MovieQualifier(format=Format.VHS, genre="Comedy")
    private MovieCatalog comedyVhsCatalog;

    @Autowired
    @MovieQualifier(format=Format.DVD, genre="Action")
    private MovieCatalog actionDvdCatalog;

    @Autowired
    @MovieQualifier(format=Format.BLURAY, genre="Comedy")
    private MovieCatalog comedyBluRayCatalog;

    // ...
}

最后,bean定义应包含匹配的限定符值。此示例还演示了您可以使用bean元属性而不是 元素。如果可用,则元素及其属性优先,但如果不存在此类限定符,则自动装配机制将回退到标记内提供的值 ,如以下示例中的最后两个bean定义:

<?xml version="1.0" encoding="UTF-8"?>
<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.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">

    <context:annotation-config/>

    <bean class="example.SimpleMovieCatalog">
        <qualifier type="MovieQualifier">
            <attribute key="format" value="VHS"/>
            <attribute key="genre" value="Action"/>
        </qualifier>
        <!-- inject any dependencies required by this bean -->
    </bean>

    <bean class="example.SimpleMovieCatalog">
        <qualifier type="MovieQualifier">
            <attribute key="format" value="VHS"/>
            <attribute key="genre" value="Comedy"/>
        </qualifier>
        <!-- inject any dependencies required by this bean -->
    </bean>

    <bean class="example.SimpleMovieCatalog">
        <meta key="format" value="DVD"/>
        <meta key="genre" value="Action"/>
        <!-- inject any dependencies required by this bean -->
    </bean>

    <bean class="example.SimpleMovieCatalog">
        <meta key="format" value="BLURAY"/>
        <meta key="genre" value="Comedy"/>
        <!-- inject any dependencies required by this bean -->
    </bean>

</beans>

1.9.5 @Bean注解

使用泛型作为自动装配限定符
除了@Qualifier注释之外,您还可以使用Java泛型类型作为隐式的限定形式。例如,假设您具有以下配置:

@Configuration
public class MyConfiguration {

    @Bean
    public StringStore stringStore() {
        return new StringStore();
    }

    @Bean
    public IntegerStore integerStore() {
        return new IntegerStore();
    }
}

假设前面的bean实现了一个通用接口(即Store和, Store),您可以@Autowire将Store接口和泛型用作限定符,如下例所示:

@Autowired
private Store<String> s1; // <String> qualifier, injects the stringStore bean

@Autowired
private Store<Integer> s2; // <Integer> qualifier, injects the integerStore bean

通用限定符也适用于自动装配列表,Map实例和数组。以下示例自动装配通用List:

// Inject all Store beans as long as they have an <Integer> generic
// Store<String> beans will not appear in this list
@Autowired
private List<Store<Integer>> s;

1.9.6. 运用CustomAutowireConfigurer
CustomAutowireConfigurer 是一个BeanFactoryPostProcessor允许您注册自己的自定义限定符注释类型的,即使它们没有使用Spring的@Qualifier注释进行注释。以下示例显示如何使用CustomAutowireConfigurer:

<bean id="customAutowireConfigurer"
        class="org.springframework.beans.factory.annotation.CustomAutowireConfigurer">
    <property name="customQualifierTypes">
        <set>
            <value>example.CustomQualifier</value>
        </set>
    </property>
</bean>

通过以下方式AutowireCandidateResolver确定autowire候选人:

  • autowire-candidate每个bean定义的值

  • 元素default-autowire-candidates上可用的任何模式

  • @Qualifier注释的存在以及注册的任何自定义注释CustomAutowireConfigurer

当多个bean有资格作为autowire候选者时,“primary”的确定如下:如果候选者中只有一个bean定义具有primary 设置为的属性true,则选择它。

1.9.7 @Resource 注解

Spring还通过@Resource在字段或bean属性setter方法上使用JSR-250 注释来支持注入。这是Java EE 5和6中的常见模式(例如,在JSF 1.2托管bean或JAX-WS 2.0端点中)。Spring也支持Spring管理对象的这种模式。

@Resource采用名称属性。默认情况下,Spring将该值解释为要注入的bean名称。换句话说,它遵循按名称语义,如以下示例所示:

public class SimpleMovieLister {

    private MovieFinder movieFinder;

    @Resource(name="myMovieFinder") 
    public void setMovieFinder(MovieFinder movieFinder) {
        this.movieFinder = movieFinder;
    }
}

这条线注入了一个@Resource。

如果未明确指定名称,则默认名称是从字段名称或setter方法派生的。如果是字段,则采用字段名称。在setter方法的情况下,它采用bean属性名称。下面的例子将把bean movieFinder注入其setter方法:

public class SimpleMovieLister {

    private MovieFinder movieFinder;

    @Resource
    public void setMovieFinder(MovieFinder movieFinder) {
        this.movieFinder = movieFinder;
    }
}

提供注解的名称解析由一个bean的名称 ApplicationContext,其中的CommonAnnotationBeanPostProcessor知道。如果您SimpleJndiBeanFactory 明确配置Spring,则可以通过JNDI解析名称 。但是,我们建议您依赖于默认行为并使用Spring的JNDI查找功能来保留间接级别。

在专属情况下,@Resource不指定明确的名称,以及类似的使用@Autowired,@Resource发现的主要类型的比赛,而不是一个具体的bean并解决众所周知的解析依存关系:BeanFactory, ApplicationContext,ResourceLoader,ApplicationEventPublisher,和MessageSource 接口。

因此,在以下示例中,customerPreferenceDao字段首先查找名为customerPreferenceDao的bean,然后返回到该类型的主类型匹配 CustomerPreferenceDao:

public class MovieRecommender {

    @Resource
    private CustomerPreferenceDao customerPreferenceDao;

    @Resource
    private ApplicationContext context; 

    public MovieRecommender() {
    }

    // ...
}

context根据已知的可解析依赖类型注入该字段: ApplicationContext。

1.9.8 @PostConstruct和@PreDestroy注解

将CommonAnnotationBeanPostProcessor不仅承认了@Resource注解也是JSR-250的生命周期注解。在Spring 2.5中引入,对这些注释的支持提供了初始化回调和 销毁回调中描述的另一种替代 方法。如果 CommonAnnotationBeanPostProcessor在Spring中注册 ApplicationContext,则在生命周期的同一点调用带有这些注释之一的方法,作为相应的Spring生命周期接口方法或显式声明的回调方法。在以下示例中,缓存在初始化时预先填充并在销毁时清除:

public class CachingMovieLister {

    @PostConstruct
    public void populateMovieCache() {
        // populates the movie cache upon initialization...
    }

    @PreDestroy
    public void clearMovieCache() {
        // clears the movie cache upon destruction...
    }
}

有关组合各种生命周期机制的效果的详细信息,请参阅 组合生命周期机制。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值