模拟Spring如何在WEB中运行
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了模拟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 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); } }
本文出自 “小小小小白” 博客,请务必保留此出处http://malin.blog.51cto.com/10779111/1842816
以上是关于模拟Spring如何在WEB中运行的主要内容,如果未能解决你的问题,请参考以下文章
如何使用 Spring 在单元测试中模拟远程 REST API?
spring练习,在Eclipse搭建的Spring开发环境中,使用set注入方式,实现对象的依赖关系,通过ClassPathXmlApplicationContext实体类获取Bean对象(代码片段