spring应用于web项目中
目标:
在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项目中的更多相关文章
- Spring Scope:Web项目中如何安全使用有状态的Bean对象?
Web系统是最常见的Java应用系统之一,现在流行的Web项目多使用ssm或ssh框架,使用spring进行bean的管理,这为我们编写web项目带来了很多方便,通常,我们的controler层使用注 ...
- Axis2在Web项目中整合Spring
一.说明: 上一篇说了Axis2与Web项目的整合(详情 :Axis2与Web项目整合)过程,如果说在Web项目中使用了Spring框架,那么又改如何进行Axis2相关的配置操作呢? 二.Axis2 ...
- Spring在Web项目中的三种启动加载的配置
在最近的项目中,使用到了spring相关的很多东西,有点把spring的配置给搞混了,从网上查到的资料以及整理了一下. 在Web项目中,启动spring容器的方式有三种,ContextLoaderLi ...
- web项目中获取spring的bean对象
Spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架,如何在程序中不通过注解的形式(@Resource.@Autowired)获取Spring配置的bean呢? Bean工厂(c ...
- 重新学习Spring一--Spring在web项目中的启动过程
1 Spring 在web项目中的启动过程 Spring简介 Spring 最简单的功能就是创建对象和管理这些对象间的依赖关系,实现高内聚.低耦合.(高内聚:相关性很强的代码组成,既单一责任原则:低耦 ...
- 在普通WEB项目中使用Spring
Spring是一个对象容器,帮助我们管理项目中的对象,那么在web项目中哪些对象应该交给Spring管理呢? 项目中涉及的对象 我们回顾一下WEB项目中涉及的对象 Servlet Request ...
- web项目中加入struts2、spring的支持,并整合两者
Web项目中加入struts2 的支持 在lib下加入strut2的jar包 2. 在web.xml中添加配置 <filter> <filter-name>struts2< ...
- Java Web学习系列——Maven Web项目中集成使用Spring、MyBatis实现对MySQL的数据访问
本篇内容还是建立在上一篇Java Web学习系列——Maven Web项目中集成使用Spring基础之上,对之前的Maven Web项目进行升级改造,实现对MySQL的数据访问. 添加依赖Jar包 这 ...
- web项目中 集合Spring&使用junit4测试Spring
web项目中 集合Spring 问题: 如果将 ApplicationContext applicationContext = new ClassPathXmlApplicationContext(& ...
随机推荐
- 转载:javascript 拖拽排序,简洁示例备忘
转载自:http://blog.csdn.net/wang4978/article/details/6721157 <html> <head> <title>拖动行 ...
- ABBYY如何把PDF转换Excel
我们都知道2007以上版本的Office文档,是可以直接将文档转存为PDF格式文档的.那么反过来,PDF文档可以转换成其他格式的文档吗?这是大家都比较好奇的话题.如果可以以其他格式进行保存,就可以极大 ...
- Cannot unwrap to requested type [javax.sql.DataSource]
遇上这个bug我的情况是这样,hibernate4以后,spring3.1不再有hibernateDaoSupport,在dao层不能继承HibernateDaoSupport, 只能显式声明Sess ...
- CSS控制print打印样式
来源:http://blog.csdn.net/pangni/article/details/6224533 一.添加打印样式 1. 为屏幕显示和打印分别准备一个css文件,如下所示: 用于屏幕显 ...
- 图片加载js类库
Picturefill Picturefill.WP插件利用picturefill.js脚本展示Responsive图片,即根据视口宽度选择尺寸合适的图片加载,节省带宽,提高网站载入速度.例如用户用手 ...
- Why we made vorlon.js and how to use it to debug your JavaScript remotely
Vorlon.js is powered by node.JS, socket.io, and late-night coffee. I would like to share with you wh ...
- 利用Java Service Wrapper将java项目添加到windows服务中
1.web项目,即tomcat/resin添加至window系统服务,步骤如下:第一步:找到tomcat的bin目录,如:D:\apache-tomcat-8.0.26\bin第二步:打开cmd,cd ...
- SSH_框架整合6--修改Edit员工信息
SSH_框架整合6--修改Edit员工信息 1 加上修改Edit键 (1)emp-list.jsp <td> <a href="emp-input?id=${id }&qu ...
- php提高效率
1. foreach效率更高,尽量用foreach代替while和for循环. 2. 循环内部不要声明变量,尤其是对象这样的变量. 3. 在多重嵌套循环中,如有可能,应当将最长的循环放在内层,最短循环 ...
- ARM各种版本号知识以及型号的发展(三星为例)
1.ARM型号的发展历史 2.单片机.工业上一般使用RTOS(实时操作系统),Linux.Android用在影音娱乐等对实时性要求没那么高的场合: 3.ARM内核版本号和Soc版本号是由ARM确定的, ...