Spring在Web项目中的三种启动加载的配置
2013-09-29 01:05
489 查看
在最近的项目中,使用到了spring相关的很多东西,有点把spring的配置给搞混了,从网上查到的资料以及整理了一下。
在Web项目中,启动spring容器的方式有三种,ContextLoaderListener; ContextLoaderServlet
;ContextLoaderPlugIn
1.在web.xml中配置ContextLoaderListener,如
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-context.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
可以通过<import resource ="classpath:spring/spring-xxx.xml "/>的方式把其他的配置抱进来。
2.在web.xml中配置ContextLoaderServlet,如
<servlet>
<servlet-name>context</servlet-name>
<servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
这种方式,spring3.0以后不再支持了
3.通过plugin配置,如
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation" value="/WEB-INF/applicationContext.xml"/>
</plug-in>
该方式适用于, spring与struts等整合,在Struts的配置文件struts-config.xml里面配置一个ContextLoaderPlugIn,用于spring的初始化工作。
在此建议使用用ContextLoaderListener。
此外,如果使用到了spring-mvc的话,在web.xml中配置DispatcherServlet,如下:
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
如果使用到了spring-security的话,在web.xml中配置FilterChain如下:
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
<init-param>
<param-name>contextAttribute</param-name>
<param-value>org.springframework.web.servlet.FrameworkServlet.CONTEXT.spring</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
在Web项目中,启动spring容器的方式有三种,ContextLoaderListener; ContextLoaderServlet
;ContextLoaderPlugIn
1.在web.xml中配置ContextLoaderListener,如
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-context.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
可以通过<import resource ="classpath:spring/spring-xxx.xml "/>的方式把其他的配置抱进来。
2.在web.xml中配置ContextLoaderServlet,如
<servlet>
<servlet-name>context</servlet-name>
<servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
这种方式,spring3.0以后不再支持了
3.通过plugin配置,如
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation" value="/WEB-INF/applicationContext.xml"/>
</plug-in>
该方式适用于, spring与struts等整合,在Struts的配置文件struts-config.xml里面配置一个ContextLoaderPlugIn,用于spring的初始化工作。
在此建议使用用ContextLoaderListener。
此外,如果使用到了spring-mvc的话,在web.xml中配置DispatcherServlet,如下:
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
如果使用到了spring-security的话,在web.xml中配置FilterChain如下:
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
<init-param>
<param-name>contextAttribute</param-name>
<param-value>org.springframework.web.servlet.FrameworkServlet.CONTEXT.spring</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
相关文章推荐
- Spring在Web项目中的三种启动加载的配置
- Spring Web项目spring配置文件随服务器启动时自动加载
- maven项目:spring配置里:连接远程oracle(虚拟机里),【虚拟机没开启(数据库连接不上)导致tomcat启动失败(web.xml配置Spring了监听器)】=(也不报具体错误,就是超时)
- Web项目中加载Spring配置的常用方法
- Web容器启动时加载Spring的配置
- web.xml文件加载顺序 一、 1 、启动一个 WEB 项目的时候, WEB 容器会去读取它的配置文件 web.xml ,读取 <listener> 和 <context-param>
- 通过web.xml文件自动启动spring容器加载对应的配置文件
- Tomcat启动项目时加载了两遍Spring配置文件
- Web项目中使用XML配置加载Spring及Spring MVC的组件
- 如果spring--bean是单例,那么web项目一启动就会加载到内存,如果bean是多例,项目会在刚刚使用的时候,就是走到这个请求地址的时候,类才会加载---spring 默认为单例
- web项目启动报spring监听加载错误问题解决
- web.xml中配置Spring容器随项目启动
- web.xml中定义的Spring的XML配置文件启动顺序
- SSM项目连接远程Linux服务器的mysql 启动tomcat卡在了 Initializing Spring root WebApplicationContext
- [IDEA][SpringBoot]web项目刚启动就结束
- web.xml中配置启动时加载的servlet,load-on-starup
- springboot 在idea多模块下 子模块的web项目用内置tomcat启动访问jsp报404
- 普通javaweb项目用jetty启动,pom中添加如下配置
- 解决eclipse创建spring boot项目加载不到application.properties配置文件的问题
- Java web项目启动加载顺序