在 WEB 应用中使用 Spring(框架)
2017-11-02 00:00
302 查看
一、在web环境下使用Spring步骤
关键几点说明
1、需要额外加入jar包spring-web-4.0.0.RELEASE.jar spring-webmvc-4.0.0.RELEASE.jar
2、Spring的配置文件,没有什么不同
3、需要在web.xml中加入如下配置
<!-- 配置Spring配置文件的名称和位置 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <!-- 启动IOC容器的ServletContextListener --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
注:
1、contextConfigLocation名称不可更改,否则将报错;
2、若不想配置Spring配置文件的名称和位置,则框架将使用默认配置,即先将
<context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param>
注释掉,然后将applicationContext.xml(不可更名)配置文件移至/WEB-INF下。
二、代码说明
1、jsp测试<%@page import="org.springframework.web.context.WebApplicationContext"%> <%@page import="org.springframework.web.context.support.WebApplicationContextUtils"%> <%@page import="org.springframework.context.ApplicationContext"%> <%@page import="com.lty.spring.struts2.bean.Person"%> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <% //1、从application域对象中获取IOC容器的实例 WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(application); //2、从IOC容器中获取bean Person person = ctx.getBean(Person.class); //3、使用bean person.hello(); %> </body> </html>
三、源码说明
主要包括ServletContextListener的初始化方法实例化IOC容器以及将该容器对象放入application域对象的一个属性中。主类:ContextLoaderListenerpublic WebApplicationContext initWebApplicationContext(ServletContext servletContext) { if (servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE) != null) { throw new IllegalStateException( "Cannot initialize context because there is already a root application context present - " + "check whether you have multiple ContextLoader* definitions in your web.xml!"); } Log logger = LogFactory.getLog(ContextLoader.class); servletContext.log("Initializing Spring root WebApplicationContext"); if (logger.isInfoEnabled()) { logger.info("Root WebApplicationContext: initialization started"); } long startTime = System.currentTimeMillis(); try { // Store context in local instance variable, to guarantee that // it is available on ServletContext shutdown. if (this.context == null) { this.context = createWebApplicationContext(servletContext); } if (this.context instanceof ConfigurableWebApplicationContext) { ConfigurableWebApplicationContext cwac = (ConfigurableWebApplicationContext) this.context; if (!cwac.isActive()) { // The context has not yet been refreshed -> provide services such as // setting the parent context, setting the application context id, etc if (cwac.getParent() == null) { // The context instance was injected without an explicit parent -> // determine parent for root web application context, if any. ApplicationContext parent = loadParentContext(servletContext); cwac.setParent(parent); } configureAndRefreshWebApplicationContext(cwac, servletContext); } } servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.context); ClassLoader ccl = Thread.currentThread().getContextClassLoader(); if (ccl == ContextLoader.class.getClassLoader()) { currentContext = this.context; } else if (ccl != null) { currentContextPerThread.put(ccl, this.context); } if (logger.isDebugEnabled()) { logger.debug("Published root WebApplicationContext as ServletContext attribute with name [" + WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE + "]"); } if (logger.isInfoEnabled()) { long elapsedTime = System.currentTimeMillis() - startTime; logger.info("Root WebApplicationContext: initialization completed in " + elapsedTime + " ms"); } return this.context; } catch (RuntimeException ex) { logger.error("Context initialization failed", ex); servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, ex); throw ex; } catch (Error err) { logger.error("Context initialization failed", err); servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, err); throw err; } }
参考资料
1、web.xml 通过contextConfigLocation配置spring 的方式相关文章推荐
- Spring - Java/J2EE Application Framework 应用框架 第 16 章 通过Spring使用远程访问和web服务
- 使用 Spring 2 Portlet MVC 框架构建 Portlet 应用
- 使用struts+spring+hibernate 组装web应用(zz)
- 使用SpringMVC框架生成XLS和PDF格式的WEB应用视图
- Spring在Web应用中的事件与使用
- 使用SpringMVC框架生成XLS和PDF格式的WEB应用视图
- 在使用hibernate 和spring 构架的框架中如果出现Initialization of bean failed; nested exception is java.lang.NoSuchMethodError: org.objectweb.asm
- Web应用中使用CXF不基于Spring的WebService发布
- struts2+hibernate3+spring3(ssh2)框架下的web应用-SBPMS系统
- 《Java高手真经_应用框架卷_Java_Web核心框架》所用软件、使用版本、下载地址列表:
- 国产InitPHP框架系列 - InitPHP框架搭建高可用WEB应用04:服务层Service使用
- 使用JSF, Spring, Hibernate构建一个实际的web应用(转载)
- Portal-Basic Java Web 应用开发框架:应用篇(十一) —— 整合 Spring
- Portal-Basic Web 应用开发框架:应用篇(二) —— Action 使用
- 使用struts+spring+hibernate 组装web应用..
- 使用分层的 Selenium 框架进行复杂 Web 应用的自动测试
- Portal-Basic Java Web 应用开发框架:应用篇(十一) —— 整合 Spring
- 使用 Spring 2 Portlet MVC 框架构建 Portlet 应用
- 如何让 Spring 应用使用web 容器的JNDI 资源
- 使用Acegi作为基于Spring框架的WEB应用的安全框架