您好,登錄后才能下訂單哦!
Spring在web中配置和普通的Java程序中有所區別,總結一下主要表現在以下幾個方面:
①jar包不同,需要引入兩個web的jar包
②需要考慮IOC容器創建的時間
非 WEB 應用在 main 方法中直接創建
在web應用中為了保證spring的運行,所以必須在程序剛在容器中啟動時就必須創建IOC容器,這樣的時間人為不好把握,我們可以通過Listener來監聽程序的運行,保證在剛開始時就創建,具體采用的是
創建一個實現ServletContextListener接口的類,并在重寫的contextInitialized(ServletContextEvent sce)方法中創建IOC容器,創建了如何讓其他的組件來訪問可以通過把IOC放入servletContext的一個域屬性中,這樣其他組件就可以直接通過servlet.getAttribute()來取得。具體代碼如下:
package com.marry.spring.struts2.listeners; /** * Created by Administrator on 2016/8/26. */ import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import javax.servlet.ServletContext; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; import javax.servlet.annotation.WebListener; @WebListener() public class SpringServletContextListener implements ServletContextListener { // Public constructor is required by servlet spec public SpringServletContextListener() { } // ------------------------------------------------------- // ServletContextListener implementation // ------------------------------------------------------- public void contextInitialized(ServletContextEvent sce) { // 1獲取spring配置文件的名稱 ServletContext servletContext = sce.getServletContext(); String config = servletContext.getInitParameter("ConfigLocation"); //1.創建IOC容器 ApplicationContext ctx = new ClassPathXmlApplicationContext(config); // 2.將IOC容器放入servletContext一個屬性中 servletContext.setAttribute("applicationContext",ctx); } public void contextDestroyed(ServletContextEvent sce) { /* This method is invoked when the Servlet Context (the Web application) is undeployed or Application Server shuts down. */ } }
相應的需要在web.xml文件中配置
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> <!--配置spring配置文件的名稱和位置--> <context-param> <param-name>ConfigLocation</param-name> <param-value>applicationContext.xml</param-value> </context-param> <!--啟動IOC容器的servletContextListener--> <listener> <listener-class>com.marry.spring.struts2.listeners.SpringServletContextListener</listener-class> </listener> <servlet> <servlet-name>TestServlet</servlet-name> <servlet-class>com.marry.spring.struts2.servlets.testServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>TestServlet</servlet-name> <url-pattern>/TestServlet</url-pattern> </servlet-mapping> </web-app>
為檢驗成果,我們可以創建一個類并配置bean
package com.marry.spring.struts2.beans; /** * Created by Administrator on 2016/8/26. */ public class Person { private String username; public void setUsername(String username) { this.username = username; } public void hello(){ System.out.println("my name is:"+username); } }
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="person" class="com.marry.spring.struts2.beans.Person"> <property name="username" value="malin"/> </bean> </beans>
創建一個servlet進行測試
package com.marry.spring.struts2.servlets; import com.marry.spring.struts2.beans.Person; import org.springframework.context.ApplicationContext; 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 java.io.IOException; /** * Created by Administrator on 2016/8/26. */ @WebServlet(name = "testServlet") public class testServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 1.從application域對象中獲取IOC容器 ServletContext servletContext=getServletContext(); ApplicationContext ctx= (ApplicationContext) servletContext.getAttribute("applicationContext"); // 2.從IOC容器中獲取需要的bean Person person= (Person) ctx.getBean("person"); person.hello(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request,response); } }
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。