`

Spring配置项之<context:annotation-config/>

阅读更多
Spring 2.1 添加了一个新的 context 的 Schema 命名空间,该命名空间对注释驱动、属性文件引入、加载期织入等功能提供了便捷的配置。我们知道注释本身是不会做任何事情的,它仅提供元数据信息。要使元数据信息真正起作用,必须让负责处理这些元数据的处理器工作起来。

而我们前面所介绍的AutowiredAnnotationBeanPostProcessor和CommonAnnotationBeanPostProcessor就是处理这些注释元数据的处理器。但是直接在 Spring 配置文件中定义这些 Bean 显得比较笨拙。Spring 为我们提供了一种方便的注册这些BeanPostProcessor的方式,这就是 <context:annotation-config/>。请看下面的配置:

调整 beans.xml 配置文件

<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xmlns:context="http://www.springframework.org/schema/context"
     xsi:schemaLocation="http://www.springframework.org/schema/beans
 http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
 http://www.springframework.org/schema/context
 http://www.springframework.org/schema/context/spring-context-2.5.xsd">

    <context:annotation-config/>

    <bean id="boss" class="com.baobaotao.Boss"/>
    <bean id="office" class="com.baobaotao.Office">
        <property name="officeNo" value="001"/>
    </bean>
    <bean id="car" class="com.baobaotao.Car" scope="singleton">
        <property name="brand" value=" 红旗 CA72"/>
        <property name="price" value="2000"/>
    </bean>
</beans>



<context:annotationconfig/> 将隐式地向 Spring 容器注册AutowiredAnnotationBeanPostProcessor、CommonAnnotationBeanPostProcessor、PersistenceAnnotationBeanPostProcessor以及equiredAnnotationBeanPostProcessor这 4 个 BeanPostProcessor。

在配置文件中使用 context 命名空间之前,必须在 <beans> 元素中声明 context 命名空间。

参考:http://hi.baidu.com/blue1255/item/ded2dc155c88b20ad0d66d85
分享到:
评论

相关推荐

    拦截器与冲突解决

    NULL 博文链接:https://xj84.iteye.com/blog/2065681

    (代码)SpringMVC第12讲:<mvc:annotation-driven/>

    SpringMVC第12讲:&lt;mvc:annotation-driven/&gt;

    springboot 基础简易实例, maven项目

    &lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt; &lt;version&gt;2.1.4.RELEASE&lt;/version&gt; &lt;relativePath/&gt; &lt;!-- lookup parent from repository --&gt; &lt;/parent&gt; &lt;groupId&gt;com.example&lt;/groupId&gt; &lt;artifactId&gt;...

    SpringMVC源码总结(三)mvc:annotation-driven和mvc:message-converters简单介绍

    NULL 博文链接:https://lgbolgger.iteye.com/blog/2105151

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

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

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

    springweb3.0MVC注解(附实例)

    &lt;display-name&gt;Spring Annotation MVC Sample&lt;/display-name&gt; &lt;!-- Spring 服务层的配置文件 --&gt; &lt;context-param&gt; &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt; &lt;param-value&gt;classpath:applicationContext...

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

    基于MyEclipse搭建maven+springmvc整合图文教程(含源码0

    &lt;mvc:annotation-driven /&gt; &lt;context:component-scan base-package="Controller" /&gt; &lt;bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"&gt; &lt;property name="prefix" value="/...

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

    spring 定时任务

    本例中一共使用了两种 spring 的定时任务,一种是使用 xml 配置的定时任务,一种是使用 annotation 配置的定时任务...&lt;task:annotation-driven/&gt; 在定时任务的方法上配置 @Scheduled(cron="0 */10 * * * *") 就可以了。

    SSH整合&&SSH集成开发

    &lt;tx:annotation-driven transaction-manager="transactionManager" /&gt; &lt;!-- 保证POJO中标注@Required的属性被注入 --&gt; &lt;bean class="org.springframework.beans.factory.annotation....

    SpringMVC+Hibernate实例

    &lt;mvc:annotation-driven/&gt; &lt;!--视图解析--&gt; &lt;bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"&gt; &lt;property name="viewClass" value="org.springframework....

    SpringMVC源码总结(二)mvc:mvc:annotation-driven背后的那些事

    NULL 博文链接:https://yihuawuye1.iteye.com/blog/2105063

    license.txt

    &lt;mvc:annotation-driven&gt; &lt;/mvc:annotation-driven&gt; 中配置Json格式乱码。代码如下: &lt;!--json格式乱码处理--&gt; &lt;mvc:message-converters register-defaults="true"&gt; &lt;bean class="org.springframework....

Global site tag (gtag.js) - Google Analytics