幾點說明:
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>
一、完整代碼服務器