Spring系列11 Spring底层提供获取应用上下文工具解析(Spring监听器使用)

Spring底层提供获取应用上下文工具解析

Spring在给客户访问提供应用上下文工具,这里分析具体操作




前言

提示 :这里的代码均为模拟Spring处理的代码

应用上下文对象是通过new ClasspathXmIApplicationContext(spring配置文件)方式获取的,但是每次从容器中获得Bean时都要编写new ClasspathXmIApplicationContext(spring配置文件),这样的弊端是配置文件加载多次,应用上下文对象创建多次。


提示:以下是本篇文章正文内容,下面案例可供参考

一、设置监听器

示例:这里将Spring容器放到了wep最大的域servletContext中就可以不要一直创建容器了
然后在监听器中监听系统启动,再将创建spring容器。
在Web项目中,可以使用ServletContextListener监听Web应用的启动,我们可以在Web应用启动时,就加载Sprling的配置文件,创建应用上下文对象ApplicationContext,在将其存储到最大的域servletContext域中,这样就可以在任意位置从域中获得应用上下文ApplicationContext对象了。
这里的ServletContextListener是监听器父类其中有初始方法contextInitialized和销毁方法contextDestroyed,并在初始方法中放入创建的应用上下文对象,之后在servlet中就可以直接用request.getServletcontext方法获取应用上下文对象了

public class Contextloaderlistener implements ServletContextListener {
    public void contextInitialized(ServletContextEvent servletContextEvent) {
    	<!--为了读取web.xml中的全局配置参数使用ServletContext中的方法getInitParameter,再将标签中的字符串写入参数-->
        ServletContext servletContext = servletContextEvent.getServletContext();
        <!--为了将Spring配置文件和servlet解耦合添加了contextconfiglocation-->
        String contextconfiglocation = servletContext.getInitParameter("contextconfiglocation");
        ApplicationContext app = new ClassPathXmlApplicationContext(contextconfiglocation);
        servletContext.setAttribute("app",app);
        System.out.println("app running");
    }
    public void contextDestroyed(ServletContextEvent servletContextEvent) {

    }
}

并将Applicationcontext封装到了web.xml里,并声明了监听器

	<!--这个标签是webxml中的全局配置参数,与Spring配置文件结合可以帮助与servlet解耦合-->
    <context-param>
        <param-name>contextconfiglocation</param-name>
        <param-value>applicationContext.xml</param-value>
    </context-param>
    <!--声明监听器-->
    <listener>
        <listener-class>com.sxit.lisenter.Contextloaderlistener</listener-class>
    </listener>

二、设置工具类WebApplicationContextUtils

1.工具类

代码如下(示例):

import org.springframework.context.ApplicationContext;

import javax.servlet.ServletContext;

public class WebApplicationContextUtils {

    public static ApplicationContext getWebApplicationContext(ServletContext servletContext){
            return (ApplicationContext) servletContext.getAttribute("app");
    }
}

2.客户端类

代码如下(示例):

public class Userservlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
        ServletContext servletContext = this.getServletContext();
        //ApplicationContext app = (ApplicationContext) servletContext.getAttribute("app");
        ApplicationContext app = WebApplicationContextUtils.getWebApplicationContext(servletContext);
        Userservice userservice = app.getBean(Userservice.class);
        userservice.save();
    }
}

其中注释的为原 没有Spring工具时的代码,之后Spring进行了优化


总结

Spring底层提供获取应用上下文工具解析,希望有帮助
这个Contextloaderlistener是封装道理Spring-web中, 所以必须得导入相应的坐标
在applicationContext.xml坐标中需要指定Spring配置文件的位置(加上classpath:)

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值