|
对于最新的稳定版本,请使用 Spring Framework 7.0.6! |
基于注解的容器配置
通过注解配置提供了XML配置的替代方案,该方案依赖于字节码元数据来连接组件,而不是XML声明。开发人员不再使用XML来描述bean连接,而是通过在相关类、方法或字段声明上使用注解,将配置移至组件类本身。如示例: AutowiredAnnotationBeanPostProcessor中所述,将BeanPostProcessor与注解结合使用是扩展Spring IoC容器的常见方法。例如,@Autowired注解提供了与自动连线协作对象中描述的功能相同的能力,但具有更细粒度的控制和更广泛的应用范围。此外,Spring还支持JSR-250注解,如@PostConstruct和@PreDestroy,以及支持包含在jakarta.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
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/> 元素会隐式注册以下后处理器:
|
|