Spring注解方式配置说明
2015-04-18 21:34
393 查看
1、<context:annotation-config/>与<context:component-scan base-package=”XX.XX”/>
在基于主机方式配置Spring的配置文件中,你可能会见到<context:annotation-config/>这样一条配置,他的作用是式地向 Spring 容器注册AutowiredAnnotationBeanPostProcessor、CommonAnnotationBeanPostProcessor、PersistenceAnnotationBeanPostProcessor 以及 RequiredAnnotationBeanPostProcessor 这 4 个BeanPostProcessor。
注册这4个BeanPostProcessor的作用,就是为了你的系统能够识别相应的注解。
例如:
如果你想使用@Autowired注解,那么就必须事先在 Spring 容器中声明 AutowiredAnnotationBeanPostProcessor Bean。传统声明方式如下
<bean class="org.springframework.beans.factory.annotation. AutowiredAnnotationBeanPostProcessor "/>
如果想使用@ Resource 、@ PostConstruct、@ PreDestroy等注解就必须声明CommonAnnotationBeanPostProcessor
如果想使用@PersistenceContext注解,就必须声明PersistenceAnnotationBeanPostProcessor的Bean。
如果想使用 @Required的注解,就必须声明RequiredAnnotationBeanPostProcessor的Bean。同样,传统的声明方式如下:
<bean class="org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor"/>
一般来说,这些注解我们还是比较常用,尤其是Antowired的注解,在自动注入的时候更是经常使用,所以如果总是需要按照传统的方式一条一条配置显得有些繁琐和没有必要,
于是spring给我们提供<context:annotation-config/>的简化配置方式,自动帮你完成声明。
不过,呵呵,我们使用注解一般都会配置扫描包路径选项<context:component-scan base-package=”XX.XX”/>
该配置项其实也包含了自动注入上述processor的功能,因此当使用 <context:component-scan/> 后,就可以将 <context:annotation-config/> 移除了。
例如:
<!-- <context:annotation-config/> 可以去掉了-->
<!-- 采用注解方式自动扫描装配 -->
<context:component-scan base-package="com.aaa.action" />
<context:component-scan base-package="com.aaa.service" />
<context:component-scan base-package="com.aaa.dao" />
在基于主机方式配置Spring的配置文件中,你可能会见到<context:annotation-config/>这样一条配置,他的作用是式地向 Spring 容器注册AutowiredAnnotationBeanPostProcessor、CommonAnnotationBeanPostProcessor、PersistenceAnnotationBeanPostProcessor 以及 RequiredAnnotationBeanPostProcessor 这 4 个BeanPostProcessor。
注册这4个BeanPostProcessor的作用,就是为了你的系统能够识别相应的注解。
例如:
如果你想使用@Autowired注解,那么就必须事先在 Spring 容器中声明 AutowiredAnnotationBeanPostProcessor Bean。传统声明方式如下
<bean class="org.springframework.beans.factory.annotation. AutowiredAnnotationBeanPostProcessor "/>
如果想使用@ Resource 、@ PostConstruct、@ PreDestroy等注解就必须声明CommonAnnotationBeanPostProcessor
如果想使用@PersistenceContext注解,就必须声明PersistenceAnnotationBeanPostProcessor的Bean。
如果想使用 @Required的注解,就必须声明RequiredAnnotationBeanPostProcessor的Bean。同样,传统的声明方式如下:
<bean class="org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor"/>
一般来说,这些注解我们还是比较常用,尤其是Antowired的注解,在自动注入的时候更是经常使用,所以如果总是需要按照传统的方式一条一条配置显得有些繁琐和没有必要,
于是spring给我们提供<context:annotation-config/>的简化配置方式,自动帮你完成声明。
不过,呵呵,我们使用注解一般都会配置扫描包路径选项<context:component-scan base-package=”XX.XX”/>
该配置项其实也包含了自动注入上述processor的功能,因此当使用 <context:component-scan/> 后,就可以将 <context:annotation-config/> 移除了。
例如:
<!-- <context:annotation-config/> 可以去掉了-->
<!-- 采用注解方式自动扫描装配 -->
<context:component-scan base-package="com.aaa.action" />
<context:component-scan base-package="com.aaa.service" />
<context:component-scan base-package="com.aaa.dao" />
相关文章推荐
- 实例说明Spring实现AOP的2种方式(注解和XML配置)
- 详细配置说明||Spring 使用注解方式进行事务管理
- spring 注解方式配置Bean
- Spring 事务基于注解方式的配置
- Spring4的知识应用总结(四)——Bean的生命周期和注解方式配置
- 05 Spring Aop实例(AOP 如此简单)@Aspect、@Around 注解方式配置
- spring 注解方式配置Bean
- spring中配置Bean的xml方式和注解方式
- spring 注解方式配置Bean
- spring boot框架学习学前掌握之重要注解(2)-通过java的配置方式进行配置spring
- Spring配置入门案例(注解方式 )
- Spring(10)用注解方式不用配置文件方式
- Spring-注解的方式配置AOP-案例
- Spring+SpringMVC+Mybatis使用注解方式配置双数据源
- Spring的XML配置的bean调用注解方式的bean
- Spring整理11 -- 面对切面(AOP)1 -- 注解配置的方式实现AOP
- spring配置声明式事务--注解方式
- spring之aop编程——基于注解、xml配置文件方式
- Spring学习笔记(六)注解方式配置事物
- spring事务的传播属性和事务隔离级别及配置事务(注解方式)