context:annotation-config的作用

本文介绍了Spring中`context:annotation-config`和`context:component-scan`的作用。`context:annotation-config`用于自动注册BeanPostProcessor,使得系统能够识别并处理如`@Resource`、`@PostConstruct`等注解,实现自动注入。而`context:component-scan`则是在指定包下自动扫描并注册带有`@Component`、`@Service`、`@Repository`等注解的类,它不仅包含了`context:annotation-config`的功能,还能自动注册组件,因此使用`context:component-scan`后,通常不再需要`context:annotation-config`。
摘要由CSDN通过智能技术生成

在传统声明方式中
类似这样的注入IOC容器中

<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor "/> 

1.如果想使用@ Resource 、@ PostConstruct、@ PreDestroy等注解就必须声明CommonAnnotationBeanPostProcessor。

2.如果想使用@PersistenceContext注解,就必须声明PersistenceAnnotationBeanPostProcessor的Bean。

3.如果想使用@Autowired注解,那么就必须事先在 Spring 容器中声明 AutowiredAnnotationBeanPostProcessor Bean。

4.如果想使用 @Required的注解,就必须声明RequiredAnnotationBeanPostProcessor的Bean。

context:annotation-config

而使用context:annotation-config/ 就可以隐式地自动向Spring容器注册4个BeanPostProcessor:

AutowiredAnnotationBeanPostProcessor
CommonAnnotationBeanPostProcessor
PersistenceAnnotationBeanPostProcessor
RequiredAnnotationBeanPostProcessor 

这样就可以使用@ Resource 、@ PostConstruct、@ PreDestroy、@PersistenceContext

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

偷偷学习被我发现

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值