1、Spring如何在web应用中使用
几点说明:
1) 需要额外加入的jar包:
spring-web-4.0.0.RELEASE.jar
spring-webmvc-4.0.0.RELEASE.jar
2) Spring的配置文件,没有什么不同
3) 如何创建IOC容器?
① 非WEB应用在main方法中直接创建
② 应该在WEB应用被服务器加载时就创建IOC容器
在ServletContextListener#void contextInitialized(ServletContextEvent sce)方法中创建IOC容器
③ 在ServletContextListener#void contextInitialized(ServletContextEvent sce)方法中创建IOC容器后, 可以将其放在ServletContext(即application域)的一个属性中。
④ 实际上,Spring配置文件的名字和位置应该也是可以配置的!将其配置到当前WEB应用的初始化参数中较为合适。
2、关键文件编码
① SpringServletContextListener(ServletContextListener)(核心)
package com.lty.spring.struts2.listener;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Application Lifecycle Listener implementation class SpringServletContextListener
*
*/
@WebListener
public class SpringServletContextListener implements ServletContextListener {
/**
* Default constructor.
*/
public SpringServletContextListener() {
}
/**
* @see ServletContextListener#contextDestroyed(ServletContextEvent)
*/
public void contextDestroyed(ServletContextEvent arg0) {
}
/**
* @see ServletContextListener#contextInitialized(ServletContextEvent)
*/
public void contextInitialized(ServletContextEvent event) {
//获取Spring配置文件的名称(全路径)
ServletContext servletContext = event.getServletContext();
String config = servletContext.getInitParameter("configLocation");
//1、创建IOC容器
ApplicationContext ctx = new ClassPathXmlApplicationContext(config);
//2、将IOC容器放在ServletContext的一个属性中
servletContext.setAttribute("applicationContext", ctx);
}
}
② TestServlet(Servlet)
package com.lty.spring.struts2.servlet;
import java.io.IOException;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.context.ApplicationContext;
import com.lty.spring.struts2.bean.Person;
/**
* Servlet implementation class TestServlet
*/
@WebServlet("/TestServlet")
public class TestServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1、从application域对象中获取IOC容器的引用
ServletContext servletContext = getServletContext();
ApplicationContext ctx = (ApplicationContext) servletContext.getAttribute("applicationContext");
//2、从IOC容器中得到需要的bean
Person person = ctx.getBean(Person.class);
person.hello();
}
}
③ web.xml(核心)
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<!-- 配置Spring配置文件的名称和位置 -->
<context-param>
<param-name>configLocation</param-name>
<param-value>applicationContext.xml</param-value>
<!-- 默认类路径下 -->
<!-- <param-value>classpath:com/lty/spring/struts2/servlet/beans.xml</param-value> -->
</context-param>
<!-- 启动IOC容器的ServletContextListener -->
<listener>
<listener-class>com.lty.spring.struts2.listener.SpringServletContextListener</listener-class>
</listener>
</web-app>
3、测试
参考资料
来源:oschina
链接:https://my.oschina.net/u/3144678/blog/1559010