在 WEB 应用中使用 Spring 的基本思路(原生)
2017-10-31 00:00
447 查看
1、Spring如何在web应用中使用
几点说明:1) 需要额外加入的jar包:
spring-web-4.0.0.RELEASE.jar
spring-webmvc-4.0.0.RELEASE.jar
2) Spring的配置文件,没有什么不同
3) 如何创建IOC容器?
① 非WEB应用在main方法中直接创建
② 应该在WEB应用被服务器加载时就创建IOC容器
在ServletContextListener#void contextInitialized(ServletContextEvent sce)方法中创建IOC容器
③ 在ServletContextListener#void contextInitialized(ServletContextEvent sce)方法中创建IOC容器后, 可以将其放在ServletContext(即application域)的一个属性中。
④ 实际上,Spring配置文件的名字和位置应该也是可以配置的!将其配置到当前WEB应用的初始化参数中较为合适。
2、关键文件编码
① SpringServletContextListener(ServletContextListener)(核心)package com.lty.spring.struts2.listener; import javax.servlet.ServletContext; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; import javax.servlet.annotation.WebListener; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * Application Lifecycle Listener implementation class SpringServletContextListener * */ @WebListener public class SpringServletContextListener implements ServletContextListener { /** * Default constructor. */ public SpringServletContextListener() { } /** * @see ServletContextListener#contextDestroyed(ServletContextEvent) */ public void contextDestroyed(ServletContextEvent arg0) { } /** * @see ServletContextListener#contextInitialized(ServletContextEvent) */ public void contextInitialized(ServletContextEvent event) { //获取Spring配置文件的名称(全路径) ServletContext servletContext = event.getServletContext(); String config = servletContext.getInitParameter("configLocation"); //1、创建IOC容器 ApplicationContext ctx = new ClassPathXmlApplicationContext(config); //2、将IOC容器放在ServletContext的一个属性中 servletContext.setAttribute("applicationContext", ctx); } }
② TestServlet(Servlet)
package com.lty.spring.struts2.servlet; import java.io.IOException; 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 org.springframework.context.ApplicationContext; import com.lty.spring.struts2.bean.Person; /** * Servlet implementation class TestServlet */ @WebServlet("/TestServlet") public class TestServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //1、从application域对象中获取IOC容器的引用 ServletContext servletContext = getServletContext(); ApplicationContext ctx = (ApplicationContext) servletContext.getAttribute("applicationContext"); //2、从IOC容器中得到需要的bean Person person = ctx.getBean(Person.class); person.hello(); } }
③ web.xml(核心)
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <!-- 配置Spring配置文件的名称和位置 --> <context-param> <param-name>configLocation</param-name> <param-value>applicationContext.xml</param-value> <!-- 默认类路径下 --> <!-- <param-value>classpath:com/lty/spring/struts2/servlet/beans.xml</param-value> --> </context-param> <!-- 启动IOC容器的ServletContextListener --> <listener> <listener-class>com.lty.spring.struts2.listener.SpringServletContextListener</listener-class> </listener> </web-app>
3、测试
参考资料
1、完整代码相关文章推荐
- Spring_34_在 WEB 应用中使用 Spring 的基本思路
- Spring在Web中使用的基本思路
- spring-在web应用中使用spring
- Spring 在 WEB 应用中使用
- 使用spring-loaded开源项目,实现java程序和web应用的热部署
- spring-在web应用中使用spring-test与junit整合
- Web应用中使用CXF不基于Spring的WebService发布
- 使用Docker来加速构建Android应用的基本部署思路解析
- 使用JSF, Spring, Hibernate构建一个实际的web应用(转载)
- maven下使用junit对spring进行单元测试_01基本应用
- 使用spring-loaded开源项目,实现java程序和web应用的热部署
- 使用struts+spring+hibernate 组装web应用..
- quartz在web项目中的应用(不使用spring)
- 使用spring-loaded开源项目,实现java程序和web应用的热部署
- Spring在Web应用中的事件与使用
- 使用spring-loaded开源项目,实现java程序和web应用的热部署
- 在web应用中使用Spring
- 7.2.2: spring的下载和安装---在web应用中使用spring
- 使用spring-loaded开源项目,实现java程序和web应用的热部署
- 使用TypeScript,AngularJs和Web API构建基本的CRUD Web 应用