Spring 为基于注解的配置提供全面支持,可在 组件类本身中的元数据,通过对相关类使用注释, 方法或字段声明。如示例:AutowiredAnnotationBeanPostProcessor 中所述, Spring 与注解结合使用,使核心 IOC 容器感知特定注释。BeanPostProcessorsSpring中文文档

例如,@Autowired注释提供的功能与自动连线协作者中所述的功能相同,但 具有更细粒度的控制和更广泛的适用性。此外,Spring 还提供 支持 JSR-250 注解,例如 和 ,以及 支持软件包中包含的 JSR-330(Dependency Injection for Java)注解,例如 和 。有关这些批注的详细信息 可以在相关部分找到。@PostConstruct@PreDestroyjakarta.inject@Inject@NamedSpring中文文档

注释注入在外部属性注入之前执行。因此,外部 配置(e.g. XML指定的 Bean 属性)有效地覆盖了注释 对于通过混合方法连接的属性。Spring中文文档

注释注入在外部属性注入之前执行。因此,外部 配置(e.g. XML指定的 Bean 属性)有效地覆盖了注释 对于通过混合方法连接的属性。Spring中文文档

从技术上讲,您可以将后处理器注册为单独的 Bean 定义,但它们 隐式注册在 ALREADY 中。AnnotationConfigApplicationContextSpring中文文档

在基于 XML 的 Spring 设置中,您可以包含以下配置标记来启用 与基于注释的配置混合匹配: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/>Spring中文文档

<context:annotation-config/>只在相同的 bean 上寻找注释 在其中定义它的应用程序上下文。这意味着,如果你输入 a for a , 它只检查控制器中的 Bean,而不检查服务中的 Bean。有关更多信息,请参见 DispatcherServlet<context:annotation-config/>WebApplicationContextDispatcherServlet@AutowiredSpring中文文档

<context:annotation-config/>只在相同的 bean 上寻找注释 在其中定义它的应用程序上下文。这意味着,如果你输入 a for a , 它只检查控制器中的 Bean,而不检查服务中的 Bean。有关更多信息,请参见 DispatcherServlet<context:annotation-config/>WebApplicationContextDispatcherServlet@AutowiredSpring中文文档