「Spring环境搭建」applicationContext.xml和springMVC.xml
本篇博客介绍适合使用xml配置Spring环境和SpringMVC环境。
① 首先我们我们创建一个普通的java web工程(包含WEB-INF/web.xml),然后在pom文件中相关的依赖
UTF-8
5.0.2.RELEASE
2.11.4
org.springframework
spring-core
${spring.version}
org.springframework
spring-beans
${spring.version}
org.springframework
spring-context
${spring.version}
org.springframework
spring-context-support
${spring.version}
org.springframework
spring-expression
${spring.version}
org.springframework
spring-web
${spring.version}
org.springframework
spring-webmvc
${spring.version}
org.springframework
spring-aop
${spring.version}
org.springframework
spring-aspects
${spring.version}
org.springframework
spring-tx
${spring.version}
javax.servlet
javax.servlet-api
4.0.1
provided
com.fasterxml.jackson.core
jackson-core
${jackson.version}
com.fasterxml.jackson.core
jackson-databind
${jackson.version}
com.fasterxml.jackson.core
jackson-annotations
${jackson.version}
org.apache.maven.plugins
maven-compiler-plugin
2.5
1.8
1.8
UTF-8
② Spring 配置文件的所有命名空间
<?xml version="1.0" encoding="UTF-8"?>
注:引入相应的命名控件有些是需要引入依赖的。
如果觉得命名空间中的版本号不对,也可以修改版本号,或者把版本号去掉,都不影响使用。
如果觉得不需要这么多的命名空间,也可以删除不需要的,如果要删除,需要删除两部分的,一个是xmls里面的,还有一个就是xsi:schemalocation中的。
web.xml配置
Archetype Created Web Application
contextConfigLocation
classpath*:applicationContext.xml
org.springframework.web.context.ContextLoaderListener
SpringMVC
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath*:springMVC.xml
1
SpringMVC
*.action
classpath是指 WEB-INF文件夹下的classes目录。
① classpath 和 classpath* 区别
classpath:只会到你的class路径中查找找文件;
classpath*:不仅包含class路径,还包括jar文件中(class路径)进行查找。
② 当有多个配置文件加载时,可采用下面代码来配置:
contextConfigLocation
classpath*:conf/spring/applicationContext_core*.xml,
classpath*:conf/spring/applicationContext_dict*.xml,
classpath*:conf/spring/applicationContext_hibernate.xml,
......
或者
contextConfigLocation
classpath*:**/applicationContext-*.xml
**/表示任意目录。
**/applicationContext-*.xml表示任意目录下的以"applicationContext-"开头的XML文件。
Spring配置文件最好以"applicationContext-"开头,且最好把所有Spring配置文件都放在一个统一的目录下,也可以分模块创建。
③ contextConfigLocation在spring中的作用
Spring 提供ServletContextListener 的一个实现类ContextLoaderListener ,该类可以作为listener 使用,加载配置文件的流程:
1)org.springframework.web.context.ContextLoaderListener#contextInitialized
2)org.springframework.web.context.ContextLoader#configureAndRefreshWebApplicationContext
如果没有contextConfigLocation 指定配置文件,则Spring 自动查找applicationContext.xml 配置文件。如果有contextConfigLocation,则利用该参数确定的配置文件。该参数指定的一个字符串,Spring 的ContextLoaderListener 负责将该字符串分解成多个配置文件,逗号","、空格" "及分号";"都可作为字符串的分割符。如果既没有applicationContext.xml 文件,也没有使用contextConfigLocation参数确定配置文件,或者contextConfigLocation确定的配置文件不存在。都将导致Spring 无法加载配置文件或无法正常创建ApplicationContext 实例。
④ org.springframework.web.servlet.DispatcherServlet配置的url-patternDispatcherServlet作用:拦截请求(浏览器的访问),然后根据自己定义的规则,去匹配处理器(Controller)来处理。
url-pattern一般有三种配置方式:
- 绝对路径 /UserServlet
- 多级目录 /aaa/bbb/UserServlet
- 通配符匹配 *.action *.do (只要请求的后置是.action 或.do都会执行该servlet)
有两种格式的建议不写:
/ :这样写的话,DispatcherServlet会将向静态资源的获取请求,如.css、.js、.jpg、.png等资源的获取请求,当作一个普通的Controller请求。中央调度器会调用处理器映射器为其查找相应的处理器,这是找不到的,会报404。/* :*表示所有,即静态资源、动态页面(jsp页面,servlet)、其他请求 都会被拦截。把静态资源、jsp页面的获取请求当作Controller请求去找处理器,然后找不到,也会报404。
当DispatcherServlet 的url-pattern配置成/ 访问不到静态资源的解决方案:
1)在web.xml中加以下代码:
default
*.png
default
*.jpg
default
*.css
default
*.js
2)在springmvc.xml中加以下代码:
3)使用MVC的resources解决
在springmvc.xml中加以下代码:
⑤ 如果不是web项目,但是又想加载applicationContext.xml,怎么办呢
Spring提供了一个ClassPathXmlApplicationContext用来读取applicationContext.xml文件,来创建spring容器
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:spring/applicationContext.xml");//得到spring容器
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
springMVC.xml
<?xml version="1.0" encoding="UTF-8"?>
application/json;charset=UTF-8