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

Spring(一):利用全局初始化参数解耦ApplicationContext与监听器

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

Spring(一):利用全局初始化参数解耦ApplicationContext与监听器

前期我们已经创建了监听器,为解决在每次调用Servlet时重复创建决配置文件的问题。

 监听器代码如下

public class ContextLoaderListener implements ServletContextListener {


    public void contextInitialized(ServletContextEvent sce) {
        ApplicationContext app = new ClassPathXmlApplicationContext("ApplicationContext.xml");
        //将spring存入servletcontext域
        ServletContext servletContext = sce.getServletContext();
        servletContext.setAttribute("app",app);
        System.out.println("Spring容器创建完毕.......");
    }

    public void contextDestroyed(ServletContextEvent sce) {

    }
}

web层代码如下

public class UserServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        ApplicationContext app = new ClassPathXmlApplicationContext("ApplicationContext.xml");


        ServletContext servletContext = req.getServletContext();
        ApplicationContext app = (ApplicationContext) servletContext.getAttribute("app");

        UserService userService = app.getBean(UserService.class);
        userService.save();
    }
}

 但ApplicationContext未完成与监听器的解耦,于是我们在xml中引入全局初始化参数


1.修改web.xml

  
    contextConfigLocation
    ApplicationContext.xml
  

2.修改监听器

public class ContextLoaderListener implements ServletContextListener {


    public void contextInitialized(ServletContextEvent sce) {

        ServletContext servletContext = sce.getServletContext();
        //读取web.xml中的全局参数
        String contextConfigLocation = servletContext.getInitParameter("contextConfigLocation");
        //将已有的ApplicationContext传入new中,实现解耦
        ApplicationContext app = new ClassPathXmlApplicationContext(contextConfigLocation);
        //将spring存入servletcontext域

        servletContext.setAttribute("app",app);
        System.out.println("Spring容器创建完毕.......");
    }

    public void contextDestroyed(ServletContextEvent sce) {

    }
}

如此实现了ApplicationContext与监听器的解耦


进一步,为解决每一个服务需要记住attribute名字的问题,我们对代码进行优化,建立了一个工具类完成ApplicationContext的调取,并在Servlet直接调取这个工具类的方法

1.创建工具类如下

public class WebApplicationContextUtils {

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

}

2.Servlet引用如下

public class UserServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        ApplicationContext app = new ClassPathXmlApplicationContext("ApplicationContext.xml");


        ServletContext servletContext = req.getServletContext();
//        ApplicationContext app = (ApplicationContext) servletContext.getAttribute("app");
        ApplicationContext app = WebApplicationContextUtils.getWebApplicationContext(servletContext);
        UserService userService = app.getBean(UserService.class);
        userService.save();
    }
}

以上优化无需手动书写,Spring已经提供了监听器对上述功能进行封装(写到这我才知道Spring有了这个功能,淦!)

只需三步

1.在pom.xml中加载spring-web

    
      org.springframework
      spring-web
      5.0.5.RELEASE
    

2.在web.xml中配置ContextLoaderListener

  
    contextConfigLocation
    classpath:ApplicationContext.xml
  
  
    org.springframework.web.context.ContextLoaderListener
  

3.使用WebApplicationContextUtils获得应用上下文

public class UserServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        ApplicationContext app = new ClassPathXmlApplicationContext("ApplicationContext.xml");


        ServletContext servletContext = req.getServletContext();
//        ApplicationContext app = (ApplicationContext) servletContext.getAttribute("app");
//        ApplicationContext app = WebApplicationContextUtils.getWebApplicationContext(servletContext);

        WebApplicationContext app = WebApplicationContextUtils.getWebApplicationContext(servletContext);
        UserService userService = app.getBean(UserService.class);
        userService.save();
    }
}

此文为黑马SSM视频教学笔记,建议配合使用。

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

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

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