當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
模拟Spring如何在WEB中运行
生活随笔
收集整理的這篇文章主要介紹了
模拟Spring如何在WEB中运行
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
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?specpublic?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?orApplication?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/javaeehttp://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
創建一個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容器中獲取需要的beanPerson?person=?(Person)?ctx.getBean("person");person.hello();}protected?void?doGet(HttpServletRequest?request,?HttpServletResponse?response)?throws?ServletException,?IOException?{doPost(request,response);} }轉載于:https://blog.51cto.com/malin/1842816
總結
以上是生活随笔為你收集整理的模拟Spring如何在WEB中运行的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: gulp安装说明
- 下一篇: SpringMVC 学习系列 (4) 之