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

来源:互联网 发布:mac优酷弹幕怎么设置 编辑:程序博客网 时间:2024/05/22 19:04

<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

阅读全文
0 0
原创粉丝点击