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
- Spring_34_在 WEB 应用中使用 Spring 的基本思路
- 【Spring】在WEB应用中使用Spring
- Spring 在 WEB 应用中使用
- 在web应用中使用Spring
- Spring在web应用中使用?
- Spring_35_在 WEB 应用中使用 Spring
- 在WEB应用中使用spring
- Web应用的组件化(一)——基本思路
- 如何在Java Web应用中获取Spring的ApplicationContext
- Spring在web应用中获得Bean的方法
- Spring在web应用中获得Bean的方法
- spring中事务的管理基于xml和注解,以及spring在web中应用
- spring在web.xml中首次使用的配置
- [JAVA]在Web应用中ThreadLocal的使用
- 在web应用程序中使用Spring
- 在WEB应用中使用MVC模式
- 在WEB应用中使用MVC模式
- 在web应用中使用日志
- 基于贝叶斯方法的垃圾邮件过滤工具
- C 语言高效编程与代码优化
- 全局事务和本地事务
- C++引用变量的使用
- 深入探讨:宏、内联函数与普通函数的区别
- Spring_34_在 WEB 应用中使用 Spring 的基本思路
- 堆和栈的区别(转过无数次的文章)
- 回调函数和普通函数的区别
- 122. Best Time to Buy and Sell Stock II | 股票利润最大化
- J2EE—JAF和JavaMail
- 【codeforces 765C】Table Tennis Game 2
- NIO
- vim与gcc
- LINUX内核经典面试题