spring <context:annotation-config /> 配置作用

来源:互联网 发布:数据仓库 数据加工 编辑:程序博客网 时间:2024/06/12 14:02

spring <context:annotation-config /> 配置作用

1、从字面意思来理解就是 “注解配置” ,使用<context:annotation-config/>元素可以自动声明“2”中的注解

2、替代了原来繁琐的配置,繁琐的配置如下:
配置<bean class="org.springframework.beans.factory.annotation. AutowiredAnnotationBeanPostProcessor "/> 才能使用@Autowired注解
配置<bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor"/>才能使用@Resource 、@PostConstruct、@PreDestroy注解
配置<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/>才能使用@PersistenceContext注解
配置<bean class="org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor"/>才能使用@Required注解

3、但由于 <context:component-scan base-package=”com.package”/> 也包含了自动注入“2”中bean的功能,所以<context:annotation-config/>可以省略;同时存在的时候,前者会被忽略。
<context:component-scan>除了具有<context:annotation-config>的功能之外,<context:component-scan>还可以在指定的package下扫描以及注册javabean 。 详细请参考context:component-scan