栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

【Spring笔记】五、Spring集成web环境

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【Spring笔记】五、Spring集成web环境

Spring集成web环境

ApplicationContext 问题

应用上下文对象(Spring容器)是通过new ClassPathXmlApplicationContext(“Spring配置文件”)方式获取的,但是每次从容器中获得取Bean时都要编写new ClassPathXmlApplicationContext,这样会导致配置文件加载多次,应用上下文对象ApplicationContext创建多次

解决方法

在Web项目中,可以使用ServletContextListener监听器监听Web应用的启动,可以在Web启动时,就加载Spring的配置文件,创建应用上下文对象ApplicationContext,再将其存到servlet最大的域对象ServletContext中,这样就可以在任意位置从域中获取Spring容器对象了

实现步骤
    创建Web应用启动监听器,实现ServletContextListener监听器接口
public class ContextLoaderListener implements ServletContextListener {
    @Override
    public void contextInitialized(ServletContextEvent sce) {
        ClassPathXmlApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
//        将Spring的应用上下文对象存到servlet最大的域对象ServletContext中
        ServletContext servletContext = sce.getServletContext();
        servletContext.setAttribute("app",app);
        System.out.println("Spring容器创建完毕");
    }
    在web.xml中配置监听器
    
        com.heima.listener.ContextLoaderListener
      
    创建Servlet程序(在web.xml中配置servlet),获取Spring应用上下文对象,获取bean对象
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ServletContext servletContext = this.getServletContext();
        ApplicationContext app = (ApplicationContext) servletContext.getAttribute("app");
        UserService userService = app.getBean(UserService.class);
        userService.save();
    }
解耦合优化

在web.xml中配置ServletContext初始化参数,配置Spring配置文件的文件名

    
        contextConfigLocation
        applicationContext.xml
    

在Listener监听器中创建Spring应用上下文对象时,读取并赋予xml中Spring配置文件的地址

public class ContextLoaderListener implements ServletContextListener {
    @Override
    public void contextInitialized(ServletContextEvent sce) {
        ServletContext servletContext = sce.getServletContext();
//        读取web.xml中的全局参数
        String contextConfigLocation = servletContext.getInitParameter("contextConfigLocation");
        ClassPathXmlApplicationContext app = new ClassPathXmlApplicationContext(contextConfigLocation);
//        将Spring的应用上下文对象存到servlet最大的域对象ServletContext中
        servletContext.setAttribute("app",app);
        System.out.println("Spring容器创建完毕");
    }
终!Spring提供获取应用上下文对象的工具类

上面的分析不用手动实现,Spring提供了一个监听器ContextLoaderListener(需导入spring-web依赖)就是对上述功能的封装,该监听器内部加载Spring配置文件,创建应用上下文对象,并存储到ServletContext域中,提供了一个工具类WebApplicationContextUtils以获取Spring应用上下文对象

步骤:

    pom.xml中导入spring-web的jar包依赖web.xml中配置ContextLoaderListener监听器使用WebApplicationContextUtils获取Spring应用上下文对象ApplicationContext

1、pom.xml中导入spring-web的jar包依赖


  org.springframework
  spring-web
  5.0.5.RELEASE

2、web.xml中配置ContextLoaderListener监听器


    org.springframework.web.context.ContextLoaderListener
  

​ web.xml中配置Spring配置文件的文件名

    
        contextConfigLocation
        classpath:applicationContext.xml
    

3、使用WebApplicationContextUtils获取Spring应用上下文对象ApplicationContext

 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ServletContext servletContext = this.getServletContext();
        ApplicationContext app = WebApplicationContextUtils.getWebApplicationContext(servletContext);
        UserService userService = app.getBean(UserService.class);
        userService.save();
    }

如需更多知识点请前往我的Spring学习笔记专栏

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/769952.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号