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();
posted @ 2022-04-20 14:36  计算机181胡佛  阅读(21)  评论(0编辑  收藏  举报