Spring集成Web环境

Spring集成web环境

1.首先创建一个最简单的maven项目,不要用原型
2.在项目结构中,对模块添加web选项,选择好web-inf和xml部署的位置
https://blog.csdn.net/qq_37112085/article/details/108303613?utm_term=maven%E9%A1%B9%E7%9B%AE%E5%A6%82%E4%BD%95%E6%B7%BB%E5%8A%A0webapp&utm_medium=distribute.pc_aggpage_search_result.none-task-blog-2~all~sobaiduweb~default-1-108303613&spm=3001.4430
3.webapp中添加index.jsp文件
4.pom.xml中添加依赖

pom.xml中添加依赖


       javax.servlet
       javax.servlet-api
       3.0.1
       provided


        javax.servlet.jsp
        javax.servlet.jsp-api
        2.2.1
        provided

5.编辑配置好tomat

避免多次加载配置文件(读取上下文对象ApplicationContext)

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

自定义ContextLoaderListener获取应用上下文
web.xml


    
        com.itheima.listener.ContextLoaderListener
    

    
    
        contextConfigLocation
        applicationContext.xml
    
    

UserServlet

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 = WebApplicationContextUtil.getWebApplicationContext(servletContext);
        UserService userService = app.getBean(UserService.class);
        userService.save();
    }
}

WebApplicationContextUtil

/*
解除ContextLoaderListener中
servletContext.setAttribute("app",app)
和UserServlet
ApplicationContext app = (ApplicationContext) servletContext.getAttribute("app");
耦合问题
*/
/*
将其中servletContext传过来
单独创建工具类解决,修改UserServlet代码
 */
public class WebApplicationContextUtil {
    public  static ApplicationContext getWebApplicationContext(ServletContext servletContext){
        return (ApplicationContext) servletContext.getAttribute("app");
    }
}

ContextLoaderListener

public class ContextLoaderListener implements ServletContextListener {
    public void contextInitialized(ServletContextEvent servletContextEvent) {
        ServletContext servletContext = servletContextEvent.getServletContext();
        //读取web.xml中的全局参数
       String contextConfigLocation = servletContext.getInitParameter("contextConfigLocation");
        ApplicationContext app=new ClassPathXmlApplicationContext(contextConfigLocation);
        //将Spring的应用上下文对象存储到ServletContext域中
        servletContext.setAttribute("app",app);
        System.out.println("spring容器创建完毕");
    }
    public void contextDestroyed(ServletContextEvent servletContextEvent) {}
}

Spring提供获取应用上下文工具
Spring提供了一个监听器ContextLoaderListener就是对上述功能的封装
使用步骤

  • 在web.xml中配置ContextLoaderListener监听器(导入spring-web坐标)
    导入坐标过程中网络中段导致一直无法正常添加依赖解决方法
    http://www.bubuko.com/infodetail-3145893.html

            org.springframework
            spring-web
            5.0.5.RELEASE

  • 使用WebApplicationContextUtils获取应用上下文对象ApplicationContext
    web.xml文件中先配置

        org.springframework.web.context.ContextLoaderListener

 

        contextConfigLocation
        classpath:applicationContext.xml

UserServlet中直接使用

public class UserServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ServletContext servletContext=this.getServletContext();
        WebApplicationContext app = WebApplicationContextUtils.getWebApplicationContext(servletContext);
        UserService userService = app.getBean(UserService.class);
        userService.save();
    }
}

你可能感兴趣的:(Spring集成Web环境)