spring应用于web项目中

时间:2021-05-16 23:05:40

目标:

在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