Spring IOC <context:annotation-config>与<context:component-scan/>

<context:annotation-config>

使用<context:annotation-config> 会隐式地注册几个post-processors
包括
1. AutowiredAnnotationBeanPostProcessor,
2. CommonAnnotationBeanPostProcessor,
3. PersistenceAnnotationBeanPostProcessor,
4. RequiredAnnotationBeanPostProcessor
这样我们就使用@Required@Autowired注解了

<context:component-scan/>

会扫描带有@Component, @Controller, @Service, @Repositry 等注解的类。

注意:如果你使用SpringMVC, 配置<mvc:annotation-driven />要和<context:component-scan/>写在同一个文件中才能生效。因为每个配置文件Spring都会新建一个DefaultListableBeanFactory来装注册的实例类。

两者联系

使用<context:component-scan/>就会默认开启<context:annotation-config>的功能,所以,配置了<context:component-scan/>就无需配置<context:annotation-config>

Spring实例化Bean两种办法:

1.在XML中用<bean/>标签实例化Bean.
2.使用 <context:component-scan/>标签自动扫描注册实例化Bean

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值