Spring MVC配置文件的三个常用配置详解
转自:http://www.cnblogs.com/benwu/articles/5162614.html
Spring MVC项目中通常会有二个配置文件,sprng-servlet.xml和applicationContext.xml二个配置文件,通常会出现以下几个配置
1. <context:annotation-config />
它的作用是隐式地向 Spring 容器注册 AutowiredAnnotationBeanPostProcessor、CommonAnnotationBeanPostProcessor、PersistenceAnnotationBeanPostProcessor、RequiredAnnotationBeanPostProcessor 这4个BeanPostProcessor。其作用是如果你想在程序中使用注解,就必须先注册该注解对应的类,如下图所示:
依赖的类 | 注解 |
CommonAnnotationBeanPostProcessor | @Resource 、@PostConstruct、@PreDestroy |
PersistenceAnnotationBeanPostProcessor的Bean | @PersistenceContext |
AutowiredAnnotationBeanPostProcessor Bean | @Autowired |
RequiredAnnotationBeanPostProcessor | @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" />
扫描到有@RestController、@Controller、@Compoment、@Repository等注解的类,则把这些类注册为Bean。
启动正常,但是任何请求都不会被拦截,简而言之就是@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注解的类)。
详解 http://blog.csdn.net/gladmustang/article/details/39999721
3. <mvc:annotation-driven />
它会自动注册RequestMappingHandlerMapping 与RequestMappingHandlerAdapter ( 3.1 以后DefaultAnnotationHandlerMapping 和AnnotationMethodHandlerAdapter已经可以不用了)
http://www.cnblogs.com/yangzhilong/p/3725849.html
http://stackoverflow.com/questions/19896870/why-is-my-spring-autowired-field-null
官方文档: http://docs.spring.io/spring/docs/4.2.2.BUILD-SNAPSHOT/spring-framework-reference/htmlsingle/#beans-autowired-annotation
区别: http://my.oschina.net/HeliosFly/blog/205343
###################################################################################################
spring中classpath和classpath*的配置区别
我们以spring中加载properties资源文件为例讲解,目录结构大致如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
src
├─main
│ ├─filters
│ │
│ ├─java
│ │ └─com
│ │ └─micmiu
│ │ ├─demoweb
│ │ │ │ ....
│ │ │ │
│ │ │ └─utils
│ │ │
│ │ └─modules
│ │
│ ├─resources
│ │ │ application.properties
│ │ │ applicationContext-shiro.xml
│ │ │ applicationContext.xml
│ │ │ hibernate.cfg.xml
│ │ │ log4j.properties
│ │ │ spring-mvc.xml
│ │ │ spring-view.xml
│ │
│ └─webapp
│ │
│ └─WEB-INF
│
└─test
├─java
│ └─com
│ └─micmiu
│ ├─demoweb
│ │ TestOther.java
│
└─resources
application.properties
|
同时 在该项目的lib中添加一个测试的micmiu-test.jar包,jar包中的文件结构如下:
1
2
3
4
5
6
7
8
9
10
11
|
micmiu-test.jar
│ application.properties
│
├─com
│ └─micmiu
│ └─test
│ application.properties
│ RunApp.class
│
└─META-INF
MANIFEST.MF
|
从准备的测试环境中我们可以看到在不同目录下的四个同名的application.properties资源文件。
[二]、测试代码:TestClassPath.java

1 package com.micmiu.demoweb;
2
3 import org.springframework.context.ApplicationContext;
4 import org.springframework.context.support.ClassPathXmlApplicationContext;
5
6 public class TestClassPath {
7 public static void main(String[] args) {
8 ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:/applicationContext.xml");
9 System.out.println(ctx.getClassLoader().getResource("").getPath());
10 }
11 }

spring配置文件:applicationContext.xml 中两种不同的properties文件加载配置:
第一种配置:classpath:
输出:
第二种配置: classpath*:
由此日志信息可知:
- 同名资源存在时,classpath: 只从第一个符合条件的classpath中加载资源,而classpath*: 会从所有的classpath中加载符合条件的资源
- classpath*:需要遍历所有的classpath,效率肯定比不上classpath,因此在项目设计的初期就尽量规划好资源文件所在的路径,避免使用classpath*来加载
Spring MVC配置文件的三个常用配置详解的更多相关文章
- Http请求中Content-Type讲解以及在Spring MVC注解中produce和consumes配置详解
原文地址: https://blog.csdn.net/shinebar/article/details/54408020 引言: 在Http请求中,我们每天都在使用Content-type来指定不 ...
- 【转】logback logback.xml常用配置详解(三) <filter>
原创文章,转载请指明出处:http://aub.iteye.com/blog/1110008, 尊重他人即尊重自己 详细整理了logback常用配置, 不是官网手册的翻译版,而是使用总结,旨在更快更透 ...
- logback logback.xml常用配置详解(三)
logback logback.xml常用配置详解 <filter> <filter>: 过滤器,执行一个过滤器会有返回个枚举值,即DENY,NEUTRAL,ACCEPT其中之 ...
- logback logback.xml常用配置详解(一)<configuration> and <logger>
logback logback.xml常用配置详解(一)<configuration> and <logger> 博客分类: Log java loglogback 原创文章 ...
- web.xml常用配置详解
web.xml常用配置详解 context-param 指定 ServletContext(上下文) 配置文件路径,基本配置一般是Spring配置文件,或者是spring-security的配置文件. ...
- 【转】logback logback.xml常用配置详解(二)<appender>
原创文章,转载请指明出处:http://aub.iteye.com/blog/1101260, 尊重他人即尊重自己 详细整理了logback常用配置, 不是官网手册的翻译版,而是使用总结,旨在更快更透 ...
- 【转】logback logback.xml常用配置详解(一)<configuration> and <logger>
原创文章,转载请指明出处:http://aub.iteye.com/blog/1101260, 尊重他人即尊重自己 详细整理了logback常用配置, 不是官网手册的翻译版,而是使用总结,旨在更快更透 ...
- logback常用配置详解及logback简介
logback 简介(一) Ceki Gülcü在Java日志领域世界知名.他创造了Log4J ,这个最早的Java日志框架即便在JRE内置日志功能的竞争下仍然非常流行.随后他又着手实现SLF4J 这 ...
- Tomcat记录-tomcat常用配置详解和优化方法(转载)
常用配置详解 1 目录结构 /bin:脚本文件目录. /common/lib:存放所有web项目都可以访问的公共jar包(使用Common类加载器加载). /conf:存放配置文件,最重要的是serv ...
随机推荐
- linux mint —— 图片一张
概述 Linux Mint是一種基於Ubuntu開發出的Linux操作系统.由Linux Mint Team团队于2006年开始发行.Linux Mint 的目标是为家庭用户和企业客户提供一个免费.高 ...
- $SublimeText2常用快捷键
1.删除一行:ctrl + shift + K2.替换:ctrl + H3.设置书签:Ctrl+F2设置书签F2 下一个书签Shift+F2上一个书签4.查找:ctrl + F 查找F3 查找下一个s ...
- HASH、HASH函数、HASH算法的通俗理解
之前经常遇到hash函数或者经常用到hash函数,但是hash到底是什么?或者hash函数到底是什么?却很少去考虑.最近同学去面试被问到这个问题,自己看文章也看到hash的问题.遂较为细致的追究了一番 ...
- 028_MapReduce中的计数器Counter的使用
一.分析运行wordcount程序屏幕上打印信息 ##运行wordcount单词频率统计程序,基于输出输出路径. [hadoop@hadoop-master hadoop-1.2.1]$ hadoop ...
- python中的值传递和引用传递
Python中的变量是没有类型的,我们可以把它看做一个(*void)类型的指针,变量是可以指向任何对象的,而对象才是有类型的. Python中的对象有可变对象(number,string,tuple等 ...
- linux基础四----samba&&nginx
一 samba 1环境配置: a.确保linux下防火墙关闭比或开放共享目录权限 iPtables -F b.确保setlinux关闭:setenforce 0 c.配置iP 2安装软件包:yum i ...
- Docker容器技术-基础命令
一.基础命令 1.运行一个镜像 [root@bogon ~]# docker run debian echo "Hello World" Unable to find image ...
- CSS3手风琴菜单 可同时折叠多个菜单
在线演示 本地下载
- library-type:fr-unstanded vs fisrt-stand vs second-stanrd
建库时是否是链特异性建库. Tophat2: --library-type The default is unstranded (fr-unstranded). If either fr-firsts ...
- 在与SQLServer建立连接时出现与网络相关的或特定于实例的错误
标题: 连接到服务器 ------------------------------ 无法连接到 (local). ------------------------------ 其他信息: 在与 SQL ...