Spring配置项<context:annotation-config>

本文详细解释了&lt;context:annotation-config&gt;在Spring框架中的作用,包括注册多个BeanPostProcessor,如AutowiredAnnotationBeanPostProcessor,以支持@Autowired、@Resource等注解的使用。同时对比了&lt;context:component-scan&gt;的使用场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<context:annotation-config>,的作用是向Spring容器中注册

AutowiredAnnotationBeanPostProcessor

CommonAnnotationBeanPostProcessor

PersistenceAnnotationBeanPostProcessor

RequiredAnnotationBeanPostProcessor

这4个BeanPostProcessor.注册这4个BeanPostProcessor的作用,就是为了系统能够识别相应的注解。

 

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

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

3.使用@PersistenceContext注解,就必须声明PersistenceAnnotationBeanPostProcessor的Bean。

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

 

 传统方式替代<context:annotation-config>

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

  

 

最后,与<context:annotation-config>比较像的一个注解<context:component-scan/> 

<context:component-scan/> 除了@Autowired等一些注解会识别到,还可以扫描@Component等注解,可以用注解生成bean.使用<context:component-scan/> 就可以不使用<context:annotation-config>

 

推荐一篇博客https://www.cnblogs.com/leiOOlei/p/3713989.html

 

转载于:https://www.cnblogs.com/lliuqh/p/10336821.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值