spring配置中《context:annotation-config》 和 《context:component-scan》区别

看到网上有很多大神的文章讲解,讲得很好很深,可是我没有用到过,理解不了,在这写一点我自己理解的区别。

 

如果想使用@AutoWired注解,需要在Spring配置文件中配置<context:annotation-config> 。

 

<context:annotation-config>做了什么呢?

 

他的作用是向 Spring 容器注册了以下4个BeanPostProcessor

AutowiredAnnotationBeanPostProcessor、                        @Autowired    

CommonAnnotationBeanPostProcessor、                         @ Resource 、@ PostConstruct、@ PreDestroy

PersistenceAnnotationBeanPostProcessor 、                    @PersistenceContext

RequiredAnnotationBeanPostProcessor、                         @Required

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

 

但是通常情况下,我们是不需要在写<context:annotation-config>的,因为使用Spring时一般都会使用<context:component-scan>把添加了@Service或者@Controller注解的类加到Spring容器中,那么

 

<context:component-scan>做了什么呢?

 <context:component-scan>做了两件事:

 

1. 把<context:annotation-config>做的事都做了(注册了4个BeanPostProcessor)

2. 扫描出需要的类到Spring容器中

 

 

把这两个注解的含义捋清楚之后,可以帮助理解和使用Spring,不懂得可以留言讨论。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值