spring整合web

在service层开启的时候创建spring容器,在listener初始化的时候获取,然后将spring容器存储到servletContext中

创建listener,重写里面的初始化方法contextInitialized

package com.java.listener; 
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
import javax.servlet.http.HttpSessionBindingEvent;

public class ApplicationContextInitListener implements ServletContextListener {
    public void contextInitialized(ServletContextEvent sce) {
      /* This method is called when the servlet context is
         initialized(when the Web application is deployed). 
         You can initialize servlet context related data here.
      */
        //读取配置文件
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        //将容器存储到servletContext域中
        sce.getServletContext().setAttribute("applicationContext", applicationContext);
    }
}

配置web.xml文件

<listener>
    <!--配置listener-->
    <listener-class>com.java.listener.ApplicationContextInitListener</listener-class>
</listener>

servlet获取servletContext

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    //获取servletContext
    ServletContext servletContext=request.getServletContext();
    //servletContext获取spring
    ApplicationContext applicationContext = (ApplicationContext) servletContext.getAttribute("applicationContext");
    //获取bean
    Sysuser bean = applicationContext.getBean(Sysuser.class);
    response.setContentType("text/html;charset=utf-8");
    response.getWriter().write(bean.toString());
}

改善代码

上面通过自定义listener存取spring容器,servletContext里面的参数名和参数都是直接出现在代码中,一步步放进去,并且定义的名字也是自定义的,且servlet取的servletContext的参数名需要跟listener的参数名一致,容易出错,改善代码:通过修改配置文件实现存放读取spring容器:

在web.xml配置文件中:容器参数名和值放在xml文件中配置

<!--定义配置文件全局参数-->
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
    <!--配置spring提供好的读取配置文件的listener-->
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

通过spring提供好的WebApplicationContextUtils类中的getWebApplicationContext方法获ApplicationContext

WebApplicationContext getWebApplicationContext(ServletContext sc)

上面的思想【只是思想,并非源码】为:

通过读取配置文件中的参数名,在servletContext中存进对应的值

public void contextInitialized(ServletContextEvent sce) {
        ServletContext servletContext = sce.getServletContext();
        String contextConfigLocation = servletContext.getInitParameter("contextConfigLocation");
        //读取配置文件
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext(contextConfigLocation.substring(contextConfigLocation.indexOf("classpath:")));
        //将容器存储到servletContext域中
        servletContext.setAttribute(contextConfigLocation, applicationContext);
    }

在工具类中定义一个方法,传入当前servletContext,通过xml文件中的参数名取得对应的值

public class WebApplicationUtils {
    public static ApplicationContext getWebApplication(ServletContext sc) {
        String contextConfigLocation = sc.getInitParameter("contextConfigLocation");
        ApplicationContext attribute = (ApplicationContext) sc.getAttribute(contextConfigLocation.substring(contextConfigLocation.indexOf("classpath:")));
        return attribute;
    }
}
posted @ 2023-02-28 22:01  Liku007  阅读(17)  评论(0编辑  收藏  举报