|
对于最新稳定版本,请使用Spring Framework 7.0.1! |
基于注释的容器配置
Spring 为基于注释的配置提供了全面的支持,运行于
通过对相关类进行注释,将元数据纳入组件类本身,
方法,或字段声明。如同所述示例:该AutowiredAnnotationBeanPostProcessor,
Spring用途豆后处理器结合注释以构建核心IOC
容器对特定注释的感知。
例如,@Autowired注释功能与自动接线协作者中描述的相同,但
控制更细致,适用范围更广。此外,Spring还提供
对 JSR-250 注释的支持,例如@PostConstruct和@PreDestroy,以及
支持 JSR-330(Java 依赖注入)注释,包含于雅加达.inject例如包@Inject和@Named.关于这些注释的详细信息
相关章节可见。
|
注释注入在外部属性注入之前进行。因此,外部 配置(e.g. XML指定的豆属性)实际上覆盖了注释 对于通过混合方法接线的属性。 |
技术上,你可以将后处理器注册为独立的Beans定义,但它们
隐式注册于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/>element 隐式注册以下后处理器:
|
|