spring应用于web项目中

Posted hy7873

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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

 

以上是关于spring应用于web项目中的主要内容,如果未能解决你的问题,请参考以下文章

spring只适用于web项目么

最近项目中代码管理学习

最近项目中代码管理学习

spring应用于web项目中

unity项目中代码中用到的类型总结

敏捷项目中代码质量提升实践