基于注解的容器配置
Spring 提供对基于注解的配置的全面支持,通过在相关类、方法或字段声明上使用注解,操作组件类本身的元数据。如示例:AutowiredAnnotationBeanPostProcessor中所述,Spring 将 BeanPostProcessors 与注解结合使用,使核心 IOC 容器能够感知特定的注解。
例如,@Autowired 注解提供了与自动装配协作器中描述的相同功能,但具有更细粒度的控制和更广泛的适用性。此外,Spring 还支持 JSR-250 注解,例如 @PostConstruct 和 @PreDestroy,以及支持 jakarta.inject 包中包含的 JSR-330(Java 依赖注入)注解,例如 @Inject 和 @Named。有关这些注解的详细信息可在相关部分中找到。
|
注解注入在外部属性注入之前执行。因此,当通过混合方式进行装配时,外部配置(例如,XML 指定的 bean 属性)会有效地覆盖属性的注解。 |
从技术上讲,您可以将后处理器注册为单独的 bean 定义,但它们已隐式注册在 AnnotationConfigApplicationContext 中。
在基于 XML 的 Spring 设置中,您可以包含以下配置标签以启用与基于注解的配置的混合和匹配
<?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
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<context:annotation-config/>
</beans>
<context:annotation-config/> 元素隐式注册以下后处理器
|
|