在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不会再加载配置文件。