模拟Spring如何在WEB中运行
Spring在web中配置和普通的Java程序中有所区别,总结一下主要表现在以下几个方面:
①jar包不同,需要引入两个web的jar包
②需要考虑IOC容器创建的时间
非 WEB 应用在 main 方法中直接创建
在web应用中为了保证spring的运行,所以必须在程序刚在容器中启动时就必须创建IOC容器,这样的时间人为不好把握,我们可以通过Listener来监听程序的运行,保证在刚开始时就创建,具体采用的是
创建一个实现ServletContextListener接口的类,并在重写的contextInitialized(ServletContextEvent sce)方法中创建IOC容器,创建了如何让其他的组件来访问可以通过把IOC放入servletContext的一个域属性中,这样其他组件就可以直接通过servlet.getAttribute()来取得。具体代码如下:
packagecom.marry.spring.struts2.listeners;/***CreatedbyAdministratoron2016/8/26.*/importorg.springframework.context.ApplicationContext;importorg.springframework.context.support.ClassPathXmlApplicationContext;importjavax.servlet.ServletContext;importjavax.servlet.ServletContextEvent;importjavax.servlet.ServletContextListener;importjavax.servlet.annotation.WebListener;@WebListener()publicclassSpringServletContextListenerimplementsServletContextListener{//PublicconstructorisrequiredbyservletspecpublicSpringServletContextListener(){}//-------------------------------------------------------//ServletContextListenerimplementation//-------------------------------------------------------publicvoidcontextInitialized(ServletContextEventsce){//1获取spring配置文件的名称ServletContextservletContext=sce.getServletContext();Stringconfig=servletContext.getInitParameter("ConfigLocation");//1.创建IOC容器ApplicationContextctx=newClassPathXmlApplicationContext(config);//2.将IOC容器放入servletContext一个属性中servletContext.setAttribute("applicationContext",ctx);}publicvoidcontextDestroyed(ServletContextEventsce){/*ThismethodisinvokedwhentheServletContext(theWebapplication)isundeployedorApplicationServershutsdown.*/}}
相应的需要在web.xml文件中配置
<?xmlversion="1.0"encoding="UTF-8"?><web-appxmlns="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
packagecom.marry.spring.struts2.beans;/***CreatedbyAdministratoron2016/8/26.*/publicclassPerson{privateStringusername;publicvoidsetUsername(Stringusername){this.username=username;}publicvoidhello(){System.out.println("mynameis:"+username);}}
<?xmlversion="1.0"encoding="UTF-8"?><beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd"><beanid="person"class="com.marry.spring.struts2.beans.Person"><propertyname="username"value="malin"/></bean></beans>
创建一个servlet进行测试
packagecom.marry.spring.struts2.servlets;importcom.marry.spring.struts2.beans.Person;importorg.springframework.context.ApplicationContext;importjavax.servlet.ServletContext;importjavax.servlet.ServletException;importjavax.servlet.annotation.WebServlet;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;importjava.io.IOException;/***CreatedbyAdministratoron2016/8/26.*/@WebServlet(name="testServlet")publicclasstestServletextendsHttpServlet{protectedvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{//1.从application域对象中获取IOC容器ServletContextservletContext=getServletContext();ApplicationContextctx=(ApplicationContext)servletContext.getAttribute("applicationContext");//2.从IOC容器中获取需要的beanPersonperson=(Person)ctx.getBean("person");person.hello();}protectedvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{doPost(request,response);}}
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。