<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