目标:

在webapp启动的时候取到spring的applicationContext对象,并把applicationContext对象存到servletContext里面,在需要的时候直接从servletcontext里面拿出来用

步骤:

1、加入spring jar包

2、建一个bean:

package com.hy.bean;
/**
*
* @author Administrator
*
*/
public class Person {
private String name; public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public void hello() {
System.out.println("my name is " + name);
} }

3、建立spring配置文件:

<?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 name="person" class="com.hy.bean.Person">
<property name="name" value="wanghai">
</property>
</bean>
</beans>

4、写一个listener

package com.hy.listener;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class SpringContextListener implements ServletContextListener{ @Override
public void contextDestroyed(ServletContextEvent arg0) { } @Override
public void contextInitialized(ServletContextEvent contextEvent) {
ServletContext context = contextEvent.getServletContext();
String config = context.getInitParameter("contextLocation");
ApplicationContext app = new ClassPathXmlApplicationContext(config);
context.setAttribute("ApplicationContext", app);
} }

注意:获取servletcontext ,获取初始化参数(spring配置文件的位置),得到一个applicationContext,存入servletcontext中

5、将listener配置到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_3_0.xsd"
version="3.0">
<context-param>
<param-name>contextLocation</param-name>
<param-value>applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>com.hy.listener.SpringContextListener</listener-class>
</listener>
</web-app>

注意:这里讲spring配置文件的位置写到了初始化参数里面。

6、写一个测试:

package com.hy.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.hy.bean.Person; /**
* Servlet implementation class TestServlet
*/
@WebServlet("/TestServlet")
public class TestServlet extends HttpServlet {
private static final long serialVersionUID = 1L; /**
* @see HttpServlet#HttpServlet()
*/
public TestServlet() {
super();
// TODO Auto-generated constructor stub
} /**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletContext context = getServletContext();
ApplicationContext app = (ApplicationContext) context.getAttribute("ApplicationContext");
Person person = app.getBean(Person.class);
person.hello();
} /**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
} }

注意:这里用了注解配置servlet,注解配置最低支持tomcat7  jdk6  xml3.0

代码连接:http://pan.baidu.com/s/1ch5YdK

spring应用于web项目中的更多相关文章

  1. Spring Scope:Web项目中如何安全使用有状态的Bean对象?

    Web系统是最常见的Java应用系统之一,现在流行的Web项目多使用ssm或ssh框架,使用spring进行bean的管理,这为我们编写web项目带来了很多方便,通常,我们的controler层使用注 ...

  2. Axis2在Web项目中整合Spring

    一.说明: 上一篇说了Axis2与Web项目的整合(详情 :Axis2与Web项目整合)过程,如果说在Web项目中使用了Spring框架,那么又改如何进行Axis2相关的配置操作呢? 二.Axis2 ...

  3. Spring在Web项目中的三种启动加载的配置

    在最近的项目中,使用到了spring相关的很多东西,有点把spring的配置给搞混了,从网上查到的资料以及整理了一下. 在Web项目中,启动spring容器的方式有三种,ContextLoaderLi ...

  4. web项目中获取spring的bean对象

    Spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架,如何在程序中不通过注解的形式(@Resource.@Autowired)获取Spring配置的bean呢? Bean工厂(c ...

  5. 重新学习Spring一--Spring在web项目中的启动过程

    1 Spring 在web项目中的启动过程 Spring简介 Spring 最简单的功能就是创建对象和管理这些对象间的依赖关系,实现高内聚.低耦合.(高内聚:相关性很强的代码组成,既单一责任原则:低耦 ...

  6. 在普通WEB项目中使用Spring

    Spring是一个对象容器,帮助我们管理项目中的对象,那么在web项目中哪些对象应该交给Spring管理呢? 项目中涉及的对象 ​ 我们回顾一下WEB项目中涉及的对象 Servlet Request ...

  7. web项目中加入struts2、spring的支持,并整合两者

    Web项目中加入struts2 的支持 在lib下加入strut2的jar包 2. 在web.xml中添加配置 <filter> <filter-name>struts2< ...

  8. Java Web学习系列——Maven Web项目中集成使用Spring、MyBatis实现对MySQL的数据访问

    本篇内容还是建立在上一篇Java Web学习系列——Maven Web项目中集成使用Spring基础之上,对之前的Maven Web项目进行升级改造,实现对MySQL的数据访问. 添加依赖Jar包 这 ...

  9. web项目中 集合Spring&使用junit4测试Spring

    web项目中 集合Spring 问题: 如果将 ApplicationContext applicationContext = new ClassPathXmlApplicationContext(& ...

随机推荐

  1. easyui datagrid使用(好)

    加载相关js和css,因为easyui依赖jquery,所有加载easyui前要先加载jquery,否则为提示找不到datagrid <!-- 加载jquery --> <scrip ...

  2. SQL server 2008 Express Edition实现自动备份和自动删除备份

    1.查看SQL 版本: select @@VERSION --可以看到 Express Edition 精简免费版 Microsoft SQL Server 2008 R2 (SP2) - 10.50 ...

  3. 使用SQL字符串反转函数REVERSE巧妙实现lastindexof功能

    要实现一个简单的业务: 使用SQL脚本获取字符串'large/020700/61970b0101.jpg' 中的'61970b0101.jpg'部分. 先想到的是C#中的lastindexof,但是S ...

  4. 【转】关于PHP的header("P3P: CP=CURa……")

    对于IE来说(默认安全级别下),iframe.img.link等标签都是只发送session cookie(又叫 第一方cookie),拦截本地cookie发送(又叫第三方cookie).当这些标签跨 ...

  5. Intellij IDEA 的使用(创建项目、导入项目、同时部署多个项目、JRebel)等常见eclipse、myeclipse换idea必看

    第一篇:Intellij IDEA 的使用 1.黑色主题 中文乱码修改 2.WEB项目的部署 以及自动编译 3.多项目的同时部署 4.相关插件提高工作效率 1.JRebel插件 实现热部署 2.Tas ...

  6. 通过Profiles查看create语句的执行时间消耗 (转)

    一,查看profiles的状态值   1,查看profiles是否已经打开了,默认是不打开的.   mysql> show profiles;   Empty set (0.02 sec) my ...

  7. $(function(){})和$(document).ready(function(){})

    document.ready和onload的区别——JavaScript文档加载完成事件 页面加载完成有两种事件 一是ready,表示文档结构已经加载完成(不包含图片等非文字媒体文件) 二是onloa ...

  8. 【shell】 for循环

    #!/bin/bash for i in 1,3,4,6 ##有几个数就循环几次 do echo $i done for m in $( cat /root/num.log) ## /root/num ...

  9. 剑指offer系列57---整数中1出现的次数

    [题目]求出1~n的整数中1出现的次数.(10进制) package com.exe11.offer; /** * [题目]求出1~n的整数中1出现的次数. * @author WGS * */ pu ...

  10. MySQL数据库MyISAM和InnoDB存储引擎的比较

    MySQL有多种存储引擎,MyISAM和InnoDB是其中常用的两种.这里介绍关于这两种引擎的一些基本概念(非深入介绍). MyISAM是MySQL的默认存储引擎,基于传统的ISAM类型,支持全文搜索 ...