`

Spring <context:annotation-config/>

 
阅读更多

 在基于主机方式配置Spring的配置文件中,你可能会见到<context:annotation-config/>这样一条配置,他的作用是式地向 Spring 容器注册

AutowiredAnnotationBeanPostProcessorCommonAnnotationBeanPostProcessor

PersistenceAnnotationBeanPostProcessor 以及 RequiredAnnotationBeanPostProcessor  4 BeanPostProcessor

注册这4 BeanPostProcessor的作用,就是为了你的系统能够识别相应的注解。

例如:

如果你想使用@Autowired注解,那么就必须事先在 Spring 容器中声明 AutowiredAnnotationBeanPostProcessor Bean。传统声明方式如下

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

如果想使用@ Resource @ PostConstruct@ PreDestroy等注解就必须声明CommonAnnotationBeanPostProcessor

如果想使用@PersistenceContext注解,就必须声明PersistenceAnnotationBeanPostProcessorBean

如果想使用 @Required的注解,就必须声明RequiredAnnotationBeanPostProcessorBean。同样,传统的声明方式如下:

<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/> 移除了。

分享到:
评论

相关推荐

    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的注解功能,...

    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 ...

    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中配置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-...

    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=...

    spring applicationContext 配置文件

    &lt;bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" p:order="0"&gt; &lt;property name="interceptors"&gt; &lt;list&gt; &lt;ref bean="doubleSubmitInterceptor"/&gt; &lt;/list&gt; ...

    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...

    维生药业小项目 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+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...

    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整合框架

    &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt; &lt;/listener&gt; &lt;context-param&gt; &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt; &lt;param-value&gt;/WEB-INF/...

    轻量级高性能 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 aop 实现源代码--xml and annotation(带lib包)

    &lt;bean id="messageSender" class="org.springframework.aop.framework.ProxyFactoryBean"&gt; 12. &lt;property name="proxyInterfaces" value="com.savage.aop.MessageSender"/&gt; 13. &lt;property name="target" ref=...

    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

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

    &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

    DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"&gt; &lt;configuration&gt; &lt;typeAliases&gt; &lt;typeAlias alias="User" type=...

    java微信公众号MVC开发框架

    &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt; &lt;/listener&gt; &lt;servlet&gt; &lt;servlet-name&gt;weixin&lt;/servlet-name&gt; &lt;servlet-class&gt;...

    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...

Global site tag (gtag.js) - Google Analytics