Spring学习-14:Spring整合web开发

本文介绍如何在Web应用中整合Spring框架与Servlet技术,通过引入spring-web-3.2.0.RELEASE.jar并配置ContextLoaderListener,实现Spring环境的初始化及避免重复加载配置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在Web应用中使用Spring,除了需要导入Spring基本开发jar包、commons-logging的jar包(详见Spring学习-02)之外,还需要导入Spring web开发jar包:spring-web-3.2.0.RELEASE.jar(在一般情况下,不引入这个包,开发也不会有什么问题。我们先不引入这个包,来看看不引会有什么问题。)

1、整合Spring和Servlet:

举个例子:

新建一个Servlet和一个Service:

package com.js.service;

public class UserService {
	public void sayHello(){
		System.out.println("Hello Spring web...");
	}
}

package com.js.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.js.service.UserService;

public class UserServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
		UserService userService = (UserService)applicationContext.getBean("userService");
		userService.sayHello();
	}

}

配置bean之后,运行项目,并访问Servlet,发现控制台输出正常。

正常整合Servlet和Spring是没有问题的,但是每次执行Servlet的时候都会加载Spring配置,加载Spring环境。

解决办法:

*在Servlet的init方法中加载Spring配置文件?

*当前这个Servlet可以使用,但是其他的Servlet就用不了了。还是需要重新加载

*正解:将加载的信息的内容放到ServletContext中,SevletContext是一个全局对象,并且是服务器启动的时候被创建的,只创建一次。在创建ServletContext的时候就加载Spring的环境。使用ServletContextListener(用于监听ServletContext对象的创建和销毁)监听器

那么这个时候,配置监听器不需要我们自己来做,因为文首提到的spring-web-3.2.0.RELEASE.jar,已经帮我们做好了!

引入该jar包。

在web.xml中配置该监听器:

<listener>
  	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <context-param>
  	<param-name>contextConfigLocation</param-name>
  	<param-value>classpath:applicationContext.xml</param-value>
  </context-param>
改写Servlet类:

package com.js.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.js.service.UserService;

public class UserServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		/*因为Spring容器已经交由web容器初始化和管理
		获得WebApplicationContext对象,需要依赖ServletContext对象 
		通常在Servlet中完成*/
		/*获取WebApplicationContext对象方法一:*/
		WebApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
		/*获取WebApplicationContext对象方法二:*/
//		WebApplicationContext applicationContext = (WebApplicationContext) getServletContext().getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
		UserService userService = (UserService)applicationContext.getBean("userService");
		userService.sayHello();
	}

}
运行程序,发现每次访问Servlet不会再加载配置文件。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值