javascript
模拟Spring如何在WEB中运行
Spring在web中配置和普通的Java程序中有所區(qū)別,總結(jié)一下主要表現(xiàn)在以下幾個(gè)方面:
①jar包不同,需要引入兩個(gè)web的jar包
②需要考慮IOC容器創(chuàng)建的時(shí)間
非 WEB 應(yīng)用在 main 方法中直接創(chuàng)建
在web應(yīng)用中為了保證spring的運(yùn)行,所以必須在程序剛在容器中啟動(dòng)時(shí)就必須創(chuàng)建IOC容器,這樣的時(shí)間人為不好把握,我們可以通過Listener來監(jiān)聽程序的運(yùn)行,保證在剛開始時(shí)就創(chuàng)建,具體采用的是
創(chuàng)建一個(gè)實(shí)現(xiàn)ServletContextListener接口的類,并在重寫的contextInitialized(ServletContextEvent sce)方法中創(chuàng)建IOC容器,創(chuàng)建了如何讓其他的組件來訪問可以通過把IOC放入servletContext的一個(gè)域?qū)傩灾?#xff0c;這樣其他組件就可以直接通過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.創(chuàng)建IOC容器ApplicationContext?ctx?=?new?ClassPathXmlApplicationContext(config); //????????2.將IOC容器放入servletContext一個(gè)屬性中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.*/} }相應(yīng)的需要在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><!--啟動(dòng)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>為檢驗(yàn)成果,我們可以創(chuàng)建一個(gè)類并配置bean
創(chuàng)建一個(gè)servlet進(jìn)行測試
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域?qū)ο笾蝎@取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);} }轉(zhuǎn)載于:https://blog.51cto.com/malin/1842816
總結(jié)
以上是生活随笔為你收集整理的模拟Spring如何在WEB中运行的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: gulp安装说明
- 下一篇: SQLServer出现 '其他会话正在使