• SSM学习day04_MVC_Spring集成web环境


    一、 Spring与Web环境集成

    搭建环境

    1. 新建maven的Module,勾选Create from archetype,找到maven-archetype-webapp,勾选上并创建
    2. main下新建java和resources文件夹
    3. pom.xml中添加依赖文件,除了之前都有的,还要新加jakarta.servlet.jsp-api和jakarta.servlet-api
    4. 新建dao、service、web层,前两层与之前一样,web层中创建UserServlet
    5. web.xml中配置servlet
    6. 配置Tomcat,选中此项目,运行

    pom.xml

        <dependency>
          <groupId>jakarta.servlet.jsp</groupId>
          <artifactId>jakarta.servlet.jsp-api</artifactId>
          <version>3.0.0</version>
        </dependency>
        <dependency>
          <groupId>jakarta.servlet</groupId>
          <artifactId>jakarta.servlet-api</artifactId>
          <version>5.0.0</version>
        </dependency>
    

    web.xml

      <servlet>
        <servlet-name>UserServlet</servlet-name>
        <servlet-class>com.hf.web.UserServlet</servlet-class>
      </servlet>
      <servlet-mapping>
        <servlet-name>UserServlet</servlet-name>
        <url-pattern>/userServlet</url-pattern>
      </servlet-mapping>
    

    UserServlet.java

    public class UserServlet extends HttpServlet {
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
            UserService userService = app.getBean(UserService.class);
            userService.save();
        }
    }
    

    1.1 ApplicationContext应用上下文获取方式

    采用new ClasspathXmlApplicationContext方式获取Spring配置文件会导致每次从容器获得Bean都要编写这一句,配置文件被加载很多次且应用上下文对象被创建很多次。

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

    创建监听器类Listener.ContextLoaderListener

    
    public class ContextLoaderListener implements ServletContextListener {
        @Override
        public void contextInitialized(ServletContextEvent sce) {
    
            ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
    
            //将Spring的应用上下文对象存储到ServiceContext域中
            ServletContext servletContext = sce.getServletContext();
            servletContext.setAttribute("app",app);
            System.out.println("Spring容器创建完毕");
    
        }
    
        @Override
        public void contextDestroyed(ServletContextEvent sce) {
    
        }
    }
    
    

    修改UserServlet.java

            //ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
            ServletContext servletContext = req.getServletContext();
            ApplicationContext app = (ApplicationContext) servletContext.getAttribute("app");
            UserService userService = app.getBean(UserService.class);
            userService.save();
    
    

    优化##

    1. 对于读取配置文件进行解耦合,将这一步写到web.xml文件中,利用全局初始化参数
      web.xml
    <!--  全局初始化参数-->
      <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>applicationContext.xml</param-value>
      </context-param>
    
    

    修改ContextLoaderListener.java中的初始化函数

        @Override
        public void contextInitialized(ServletContextEvent sce) {
            ServletContext servletContext = sce.getServletContext();
            //读取web.xml中的全局参数
    
            String contextConfigLocation = servletContext.getInitParameter("contextConfigLocation");
    
            ApplicationContext app = new ClassPathXmlApplicationContext(contextConfigLocation);
    
            //将Spring的应用上下文对象存储到ServiceContext域中
            servletContext.setAttribute("app",app);
            System.out.println("Spring容器创建完毕");
    
        }
    
    
    1. 工具类通过名称获取上下文,改为通过工具类
      创建工具类WebApplicationContextUtils.java
    public class WebApplicationContextUtils {
    
        public static ApplicationContext getWebApplicationContext(ServletContext servletContext){
            return (ApplicationContext) servletContext.getAttribute("app");
        }
    }
    

    修改UserServlet.java

    //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();
    
  • 相关阅读:
    [Javascript] Use a custom sort function on an Array in Javascript
    [Unit Testing] Fundamentals of Testing in Javascript
    [WASM] Create a New Rust/Webpack Project using the rust-webpack Template
    [Adobe Analytics] Segments types
    win7系统远程连接其它计算机,并且向远程机传输文件
    移动应用数据统计分析平台汇总
    设计模式(策略模式)
    程序员与卓别林
    我的Android进阶之旅------>HTTP 返回状态值详解
    OSX: 真的吗?Mac OS X重大漏洞 改时钟获系统最高权限
  • 原文地址:https://www.cnblogs.com/ebym/p/16165014.html
Copyright © 2020-2023  润新知