`

关于Spring中的<context:annotation-config/>配置

阅读更多

使用@Autowired注解,必须事先在Spring容器中声明AutowiredAnnotationBeanPostProcessor的Bean:

<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor "/>

  使用 @Required注解,就必须声明RequiredAnnotationBeanPostProcessor的Bean:

<bean class="org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor"/>

  类似地,使用@Resource、@PostConstruct、@PreDestroy等注解就必须声明 CommonAnnotationBeanPostProcessor;使用@PersistenceContext注解,就必须声明 PersistenceAnnotationBeanPostProcessor的Bean。
  这样的声明未免太不优雅,而Spring为我们提供了一种极为方便注册这些BeanPostProcessor的方式,即使用<context:annotation- config/>隐式地向 Spring容器注册AutowiredAnnotationBeanPostProcessor、RequiredAnnotationBeanPostProcessor、CommonAnnotationBeanPostProcessor以及PersistenceAnnotationBeanPostProcessor这4个BeanPostProcessor。如下:

<context:annotation-config/> 

  另,在我们使用注解时一般都会配置扫描包路径选项:

<context:component-scan base-package="pack.pack"/>

  该配置项其实也包含了自动注入上述processor的功能,因此当使用<context:component-scan/>后,即可将<context:annotation-config/>省去。

 

原文出处:http://www.cnblogs.com/iuranus/archive/2012/07/19/2599084.html

分享到:
评论

相关推荐

    Spring3中配置DBCP,C3P0,Proxool,Bonecp数据源

    &lt;context:annotation-config /&gt; &lt;context:component-scan base-package="com.mvc" /&gt; &lt;mvc:annotation-driven /&gt; &lt;mvc:resources mapping="/resources/**" location="/resources/" /&gt; &lt;mvc:default-servlet-...

    Spring AOP配置源码

    &lt;/beans&gt;&lt;context:annotation-config/&gt; &lt;context:component-scan base-package="com.spring.*"/&gt; 两行为开启spring的注解配置 &lt;aop:aspect id="aspect" ref="logIntercepter"&gt; 引入具体的AOP操作类 &lt;aop:pointcut ...

    spring applicationContext 配置文件

    &lt;description&gt;Spring公共配置文件&lt;/description&gt; &lt;!-- mes 的數據庫 --&gt; &lt;bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"&gt; &lt;property name="driverClass" ...

    struts2.3+hibernate3.6+spring3.1整合的纯xml配置的小项目

    &lt;context:annotation-config&gt;&lt;/context:annotation-config&gt; &lt;context:component-scan base-package="org.whvcse"&gt;&lt;/context:component-scan&gt; &lt;tx:annotation-driven transaction-manager="txManager" /&gt; &lt;!-- ...

    spring3.2+strut2+hibernate4

    &lt;context:annotation-config /&gt; &lt;!-- &lt;bean id="sessionFactory"--&gt; &lt;!-- class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"&gt;--&gt; &lt;!-- &lt;property name="configLocation"--&gt; &lt;!-- value=...

    springmvc-ibatis

    &lt;context:annotation-config/&gt; &lt;!-- 扫描包 --&gt; &lt;context:component-scan base-package="com.org"/&gt; &lt;bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver...

    SpringMVC+Hibernate全注解整合

    &lt;context:annotation-config/&gt; &lt;context:component-scan base-package="com.org.*" /&gt; &lt;bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"&gt; &lt;property...

    维生药业小项目 SSH简单学习项目

    &lt;context:annotation-config/&gt; &lt;bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"&gt; &lt;property name="driverClass" value="com.mysql.jdbc.Driver"&gt;&lt;/property&gt; &lt;property name="jdbc...

    SpringMVC-SSH全注解

    &lt;context:annotation-config/&gt; &lt;!-- 扫描包 --&gt; &lt;context:component-scan base-package="com.org.core"/&gt; &lt;bean id="jspViewResolver" class="org.springframework.web.servlet.view....

    OA项目SSH整合框架

    2,在struts-config.xml中配置controller(Spring Reference 15.4.1.1. DelegatingRequestProcessor) &lt;!-- 集成Spring --&gt; &lt;controller&gt; &lt;set-property property="processorClass" value="org.spring...

    spring_MVC源码

    10. &lt;context:annotation-config /&gt; 11. &lt;!-- 把标记了@Controller注解的类转换为bean --&gt; 12. &lt;context:component-scan base-package="com.mvc.controller" /&gt; 13. &lt;!-- 启动Spring MVC的注解功能,...

    轻量级高性能 RPC 框架 HRPC.zip

     &lt;context:annotation-config/&gt;  &lt;context:property-placeholder location="classpath:system.properties"/&gt;  &lt;!--客户端配置--&gt;  &lt;bean id="rpcClient" class="com.yingjun.rpc.client.RPCClient"&gt;  ...

    Spring Context测试

    学习spring组件扫描(Component Scanning)的代码 ...&lt;context:annotation-config /&gt; &lt;context:component-scan base-package="com.test"&gt;&lt;/context:component-scan&gt; 2.在需要装配的类的上面全部加上@Component

    spring mvc aop

    spring mvc aop &lt;context:annotation-config /&gt;

    spring

    context:annotation-config:作用是可以使用@ Autowired,@ Inject等来省略xml配置文件中的属性属性,简化配置; context:component-scan:作用是可以使用@ Component,@ Controller,@ Service等等来省略xml...

    spring aop 实现源代码--xml and annotation(带lib包)

    在Spring1.2或之前的版本中,实现AOP的传统方式就是通过实现Spring的AOP API来定义Advice,并设置代理对象。Spring根据Adivce加入到业务流程的时机的不同,提供了四种不同的Advice:Before Advice、After Advice、...

    客户关系管理系统框架搭建(二)

    &lt;tx:annotation-driven transaction-manager="txManager"/&gt; * 创建dao层共同的接口,放置在cn.itcast.crm.dao下 public interface ICommonDao&lt;T&gt; { public void save(T entity); } * ...

    springmybatis

    查询出列表,也就是返回list, 在我们这个例子中也就是 List&lt;User&gt; , 这种方式返回数据,需要在User.xml 里面配置返回的类型 resultMap, 注意不是 resultType, 而这个resultMap 所对应的应该是我们自己配置的 ...

    java微信公众号MVC开发框架

    spring配置文件中唯一需要配置的bean是WeixinConfigurer类,&lt;context:component-scan base-package="com.github.jweixin.jwx.weixin.service" /&gt;是可选配置,但里面封装了微信接口服务类,建议一定要配置进spring...

Global site tag (gtag.js) - Google Analytics