对于最新稳定版本,请使用Spring Framework 7.0.1spring-doc.cadn.net.cn

基于注释的容器配置

Spring 为基于注释的配置提供了全面的支持,运行于 通过对相关类进行注释,将元数据纳入组件类本身, 方法,或字段声明。如同所述示例:该AutowiredAnnotationBeanPostProcessor, Spring用途豆后处理器结合注释以构建核心IOC 容器对特定注释的感知。spring-doc.cadn.net.cn

例如,@Autowired注释功能与自动接线协作者中描述的相同,但 控制更细致,适用范围更广。此外,Spring还提供 对 JSR-250 注释的支持,例如@PostConstruct@PreDestroy,以及 支持 JSR-330(Java 依赖注入)注释,包含于雅加达.inject例如包@Inject@Named.关于这些注释的详细信息 相关章节可见。spring-doc.cadn.net.cn

注释注入在外部属性注入之前进行。因此,外部 配置(e.g. XML指定的豆属性)实际上覆盖了注释 对于通过混合方法接线的属性。spring-doc.cadn.net.cn

技术上,你可以将后处理器注册为独立的Beans定义,但它们 隐式注册于AnnotationConfigApplicationContext已经。spring-doc.cadn.net.cn

在基于XML的Spring设置中,你可以包含以下配置标签以启用 与基于注释的配置混合搭配:spring-doc.cadn.net.cn

<?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 隐式注册以下后处理器:spring-doc.cadn.net.cn

<context:annotation-config/>只查找豆子的注释 定义的应用上下文。这意味着,如果你设<context:annotation-config/>WebApplicationContext对于调度器服务, 它只检查@Autowired豆子在你的手柄里,而不是你的服务。更多信息请参见The DispatcherServletspring-doc.cadn.net.cn