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/>
<artifactId>spring-boot-starter-parent</artifactId> <version>2.1.4.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>...
NULL 博文链接:https://lgbolgger.iteye.com/blog/2105151
<description>Spring公共配置文件</description> <!-- mes 的數據庫 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> <property name="driverClass" ...
</beans><context:annotation-config/> <context:component-scan base-package="com.spring.*"/> 两行为开启spring的注解配置 <aop:aspect id="aspect" ref="logIntercepter"> 引入具体的AOP操作类 <aop:pointcut ...
<context:annotation-config /> <context:component-scan base-package="com.mvc" /> <mvc:annotation-driven /> <mvc:resources mapping="/resources/**" location="/resources/" /> <mvc:default-servlet-...
<display-name>Spring Annotation MVC Sample</display-name> <!-- Spring 服务层的配置文件 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext...
<context:annotation-config></context:annotation-config> <context:component-scan base-package="org.whvcse"></context:component-scan> <tx:annotation-driven transaction-manager="txManager" /> <!-- ...
<context:annotation-config /> <!-- <bean id="sessionFactory"--> <!-- class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">--> <!-- <property name="configLocation"--> <!-- value=...
<context:annotation-config/> <!-- 扫描包 --> <context:component-scan base-package="com.org"/> <bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver...
<mvc:annotation-driven /> <context:component-scan base-package="Controller" /> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/...
<context:annotation-config/> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="com.mysql.jdbc.Driver"></property> <property name="jdbc...
<context:annotation-config/> <context:component-scan base-package="com.org.*" /> <bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property...
本例中一共使用了两种 spring 的定时任务,一种是使用 xml 配置的定时任务,一种是使用 annotation 配置的定时任务...<task:annotation-driven/> 在定时任务的方法上配置 @Scheduled(cron="0 */10 * * * *") 就可以了。
<tx:annotation-driven transaction-manager="transactionManager" /> <!-- 保证POJO中标注@Required的属性被注入 --> <bean class="org.springframework.beans.factory.annotation....
<mvc:annotation-driven/> <!--视图解析--> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework....
NULL 博文链接:https://yihuawuye1.iteye.com/blog/2105063
<mvc:annotation-driven> </mvc:annotation-driven> 中配置Json格式乱码。代码如下: <!--json格式乱码处理--> <mvc:message-converters register-defaults="true"> <bean class="org.springframework....