指定spring配置文件的位置 ContextLoaderListener DispatcherServlet
2017-03-10 09:54
483 查看
默认MVC配置文件
在web.xml文件中配置:
指定Spring来处理请求的Servlet,默认查找mvc配置文件的地址是:/WEB-INF/${servletname}-servlet.xml,示例中默认查找的mvc配置文件是:/WEB-INF/annomvc-servlet.xml。
要修改mvc配置文件的位置,需要在配置DispatcherServlet时指定mvc配置文件的位置,只需要在配置DispatcherServlet时指定<init-param>标签。
其他配置文件
这里的其他配置文件,指的是对datasource的配置、persistence层的配置、service层的配置信息等。要加载其他配置文件,需要在web.xml配置文件中加入一个ContextLoaderListener监听器来配置。ContextLoaderListener只监听初始化除mvc相关配置之外的bean。
要修改除mvc配置文件这之外的其他bean的配置文件位置,只需要在web.xml中加入<context-param>标签。
ContextLoaderListener&ServletDispatcher
Spring中有两种上下文环境“Application Context”和“Web Application Context”,它们分别对应ContextLoaderListener和ServletDispatcher,且者可以用来配置bean的注入、装配、AOP。
1. ContextLoaderListener
ContextLoaderListener通过读取contextConfigLocatiion参数来读取配置参数,一般来说它配置的是Spring项目的中间层。对应到Spring的自动装配机制<context:component-scan>就是以下几种注解的装配。
(1) DAO : such as @Repository bean
(2) Entity : such as @Entity bean
(3)Service: such as @Service bean
2. ServletDispatcher
它配置的是Web层组件的注入、装配和AOP。
(1)Controller
(2)ViewResolvers
(3)LocaleResolvers
(4)ThemeResolvers
在web.xml文件中配置:
<!-- front controller --> <servlet> <servlet-name>annomvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>annomvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>
指定Spring来处理请求的Servlet,默认查找mvc配置文件的地址是:/WEB-INF/${servletname}-servlet.xml,示例中默认查找的mvc配置文件是:/WEB-INF/annomvc-servlet.xml。
<!-- front controller --> <servlet> <servlet-name>annomvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:config/annomvc-servlet.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>annomvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>
要修改mvc配置文件的位置,需要在配置DispatcherServlet时指定mvc配置文件的位置,只需要在配置DispatcherServlet时指定<init-param>标签。
其他配置文件
这里的其他配置文件,指的是对datasource的配置、persistence层的配置、service层的配置信息等。要加载其他配置文件,需要在web.xml配置文件中加入一个ContextLoaderListener监听器来配置。ContextLoaderListener只监听初始化除mvc相关配置之外的bean。
<listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>若没有指定其他参数,默认查找的配置文件位置是:/WEB-INF/applicationContext.xml。
<!-- context load listener --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value> classpath:config/service-context.xml classpath:config/persistence-context.xml classpath:config/datasource-context.xml </param-value> </context-param>
要修改除mvc配置文件这之外的其他bean的配置文件位置,只需要在web.xml中加入<context-param>标签。
ContextLoaderListener&ServletDispatcher
Spring中有两种上下文环境“Application Context”和“Web Application Context”,它们分别对应ContextLoaderListener和ServletDispatcher,且者可以用来配置bean的注入、装配、AOP。
1. ContextLoaderListener
ContextLoaderListener通过读取contextConfigLocatiion参数来读取配置参数,一般来说它配置的是Spring项目的中间层。对应到Spring的自动装配机制<context:component-scan>就是以下几种注解的装配。
(1) DAO : such as @Repository bean
(2) Entity : such as @Entity bean
(3)Service: such as @Service bean
2. ServletDispatcher
它配置的是Web层组件的注入、装配和AOP。
(1)Controller
(2)ViewResolvers
(3)LocaleResolvers
(4)ThemeResolvers
相关文章推荐
- 在搭建Spring框架时,web.xml文件的配置之DispatcherServlet&ContextLoaderListener
- Spring ContextLoaderListener And DispatcherServlet Concepts
- SPRING IN ACTION 第4版笔记-第七章Advanced Spring MVC-002- 在xml中引用Java配置文件,声明DispatcherServlet、ContextLoaderListener
- Spring ContextLoaderListener And DispatcherServlet Concepts
- 160329(一)、在web.xml文件里配置org.springframework.web.context.ContextLoaderListener
- web.xml文件中配置spring的ContextLoaderListener后报错
- contextConfigLocation指定spring配置文件位置
- spring有三种启动方式,使用ContextLoaderServlet,ContextLoaderListener和ContextLoaderPlugIn.
- 如何将配置spring文件指定名字,指定位置
- 【SpringMVC之配置文件(一)】spring 里面的ApplicationContext.xml 和 xxx-servlet.xml分别的作用是什么?
- Spring 配置问题还是代码问题?Error configuring application listener of class org.springframework.web.context.ContextLoaderListener
- 搭建spring框架的时候,web.xml中的spring相关配置,可以不用配置ContextLoaderListener(即只配DispatcherServlet)吗?
- web.xml配置详解 org.springframework.web.context.ContextLoaderListener
- spring通过在web.xml 中配置ContextLoaderListener 来加载co...
- [key]严重: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener(Spring配置异常)
- Spring配置web.xml中<listener>标签(ContextLoaderListener类解析)解析
- ContextLoaderListener 与 ServletDispatcher
- web.xml配置文件中ContextLoaderListener的作用
- 在tomcat中启动的文件-ServletContextListener实现全局配置装载入内存
- Spring: DispacherServlet和ContextLoaderListener中的WebApplicationContext的关系