几点说明:
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应用的初始化参数中较为合适。java
① SpringServletContextListener(ServletContextListener)(核心)git
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"); //一、建立IOC容器 ApplicationContext ctx = new ClassPathXmlApplicationContext(config); //二、将IOC容器放在ServletContext的一个属性中 servletContext.setAttribute("applicationContext", ctx); } }
② TestServlet(Servlet)web
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 { //一、从application域对象中获取IOC容器的引用 ServletContext servletContext = getServletContext(); ApplicationContext ctx = (ApplicationContext) servletContext.getAttribute("applicationContext"); //二、从IOC容器中获得须要的bean Person person = ctx.getBean(Person.class); person.hello(); } }
③ web.xml(核心)spring
<?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>
一、完整代码服务器