Spring MVC配置文件的三个常用配置详解
来源:互联网 发布:python celery原理 编辑:程序博客网 时间:2024/04/20 22:08
Spring MVC项目中通常会有二个配置文件,sprng-servlet.xml和applicationContext.xml二个配置文件,通常会出现以下几个配置
1. <context:annotation-config />
它的作用是隐式地向 Spring 容器注册 AutowiredAnnotationBeanPostProcessor、CommonAnnotationBeanPostProcessor、PersistenceAnnotationBeanPostProcessor、RequiredAnnotationBeanPostProcessor 这4个BeanPostProcessor。其作用是如果你想在程序中使用注解,就必须先注册该注解对应的类,如下图所示:
依赖的类注解CommonAnnotationBeanPostProcessor@Resource 、@PostConstruct、@PreDestroyPersistenceAnnotationBeanPostProcessor的Bean@PersistenceContextAutowiredAnnotationBeanPostProcessor Bean@AutowiredRequiredAnnotationBeanPostProcessor@Required当然也可以自己进行注册:
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor "/> <bean class="org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor"/>
2. <context:component-scan base-package="com.*" >
<context:component-scan/> 配置项不但启用了对类包进行扫描以实施注释驱动 Bean 定义的功能,同时还启用了注释驱动自动注入的功能(即还隐式地在内部注册了 AutowiredAnnotationBeanPostProcessor 和 CommonAnnotationBeanPostProcessor),因此当使用 <context:component-scan/> 后,就可以将 <context:annotation-config/> 移除了。
在这里有一个比较有意思的问题,就是扫描是否需要在二个配置文件都配置一遍,我做了这么几种测试:
(1)只在applicationContext.xml中配置如下
<context:component-scan base-package="com.login" />
启动正常,但是任何请求都不会被拦截,简而言之就是@Controller失效
(2)只在spring-servlet.xml中配置上述配置
启动正常,请求也正常,但是事物失效,也就是不能进行回滚
(3)在applicationContext.xml和spring-servlet.xml中都配置上述信息
启动正常,请求正常,也是事物失效,不能进行回滚
(4)在applicationContext.xml中配置如下
<context:component-scan base-package="com.login" />
在spring-servlet.xml中配置如下
<context:component-scan base-package="com.sohu.login.web" />
此时启动正常,请求正常,事物也正常了。
结论:在spring-servlet.xml中只需要扫描所有带@Controller注解的类,在applicationContext中可以扫描所有其他带有注解的类(也可以过滤掉带@Controller注解的类)。
3. <mvc:annotation-driven />
它会自动注册DefaultAnnotationHandlerMapping 与AnnotationMethodHandlerAdapter
转载自:行在途中
- Spring MVC配置文件的三个常用配置详解
- Spring MVC配置文件的三个常用配置详解
- Spring MVC配置文件的三个常用配置详解
- Spring MVC配置文件的三个常用配置详解
- Spring MVC配置文件的三个常用配置详解
- Spring MVC配置文件的三个常用配置详解
- Spring MVC配置文件的三个常用配置详解(annotation-config,annotation-config,annotation-driven)
- spring mvc 三个常用配置
- Spring的MVC配置详解
- spring-mvc.xml 配置文件详解
- Spring MVC的配置文件
- spring mvc常用配置
- 详解Spring MVC 4常用的注解
- 详解Spring MVC 常用的那些注解
- Spring MVC配置详解
- Spring MVC配置详解
- spring MVC配置详解
- spring MVC配置详解
- 1002. A+B for Polynomials (25)
- 六.Tengine的footer模块(ngx_http_footer_filter_module),主机信息调试
- 保罗·格雷厄姆创业讲堂六:没有留存率不要谈推广
- reemarker定义宏的用法
- Linux(Unix)时钟同步ntpd服务配置方法
- Spring MVC配置文件的三个常用配置详解
- OpenWrt网络结构
- hdu5072 容斥原理
- LeetCode 132 Palindrome Partitioning II
- IOS中Json解析的四种方法
- 常用10大算法汇总
- Android surfaceview详解
- 昌吉教育论文发表昌吉建筑论文发表昌吉在查论文发表计算机论文期刊出版社
- Master-Slave,Spring,Hibernate,故事曲折离奇,情结跌宕起伏