Spring_34_在 WEB 应用中使用 Spring 的基本思路

来源:互联网 发布:js修改parameter 编辑:程序博客网 时间:2024/04/29 13:22

**重点:
核心是利用:
在web应用启动阶段,传入参数(spring配置文件的名称和路径),在监听器中获取这个参数,并用这个参数来创建ioc容器,并将它设置到servletcontext中的一个属性,要使用的时候,根据特定的属性名称取出applicationContext,当然,原理是这样子,但是spring集成有这样的listener,这也是为什么web.xml中总是要配置初始化参数和监听器的原因,就是为了在web应用启动阶段初始化ioc容器.**

他人笔记:

1. Spring 如何在 WEB 应用中使用 ?1). 需要额外加入的 jar 包:spring-web-4.0.0.RELEASE.jarspring-webmvc-4.0.0.RELEASE.jar2). Spring 的配置文件, 没有什么不同3). 如何创建 IOC 容器 ? ①. 非 WEB 应用在 main 方法中直接创建②. 应该在 WEB 应用被服务器加载时就创建 IOC 容器: 在 ServletContextListener#contextInitialized(ServletContextEvent sce) 方法中创建 IOC 容器.③. 在 WEB 应用的其他组件中如何来访问 IOC 容器呢 ?在 ServletContextListener#contextInitialized(ServletContextEvent sce) 方法中创建 IOC 容器后, 可以把其放在ServletContext(即 application 域)的一个属性中. ④. 实际上, Spring 配置文件的名字和位置应该也是可配置的! 将其配置到当前 WEB 应用的初始化参数中较为合适. 2. Spring 如何整合 Struts2 ?

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">  <!-- 1,配置spring配置文件所在的位置 -->  <context-param>    <param-name>configpath</param-name>    <param-value>applicationcontext.xml</param-value>  </context-param></web-app>

applicationcontext.xml

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xmlns:context="http://www.springframework.org/schema/context"    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"><bean id="person" class="com.hgh.spring.pojo.Person"></bean></beans>

MyListener

package com.hgh.spring.web;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 MyListener * */@WebListenerpublic class MyListener implements ServletContextListener {    /**     * Default constructor.      */    public MyListener() {        // TODO Auto-generated constructor stub    }    /**     * @see ServletContextListener#contextInitialized(ServletContextEvent)     */    public void contextInitialized(ServletContextEvent arg0) {        // TODO Auto-generated method stub        //1. 获取 Spring 配置文件的名称.         ServletContext servletContext = arg0.getServletContext();        String springConfigPath = servletContext.getInitParameter("configpath");        //1. 创建 IOC 容器        ApplicationContext ac = new ClassPathXmlApplicationContext(springConfigPath);        //2. 把 IOC 容器放在 ServletContext 的一个属性中.         servletContext.setAttribute("ApplicationContext", ac);    }    /**     * @see ServletContextListener#contextDestroyed(ServletContextEvent)     */    public void contextDestroyed(ServletContextEvent arg0) {        // TODO Auto-generated method stub    }}

TestServlet

package com.hgh.spring.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.hgh.spring.pojo.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 {        // TODO Auto-generated method stub        ServletContext servletContext = getServletContext();        ApplicationContext ac = (ApplicationContext) servletContext.getAttribute("ApplicationContext");        Person person = (Person) ac.getBean("person");        person.getPerson();    }}

index.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"    pageEncoding="ISO-8859-1"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>Insert title here</title></head><body><a href="TestServlet">TestServlet</a></body></html>
0 0