0
点赞
收藏
分享

微信扫一扫

spring集成web环境(9)

何以至千里 2022-03-11 阅读 53

一. 搭建环境

在这里插入图片描述

在UserDaoImpl中随便写点东西:

@Repository
public class UserDaoImpl implements UserDao {
    public void save(){
        System.out.println("saving.....");
    }
}

然后在maven中配置spring和下面的坐标:
在这里插入图片描述
在UserServlet中写上:

public class UserServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ApplicationContext app = new AnnotationConfigApplicationContext(SpringConfig.class);
        UserDao userDao = app.getBean(UserDao.class);
        userDao.save();
    }
}

其他的类就按照常规进行配置,并在web.xml中配置上servlet:

<web-app>
  <display-name>Archetype Created Web Application</display-name>
  <servlet>
    <servlet-name>UserServlet</servlet-name>
    <servlet-class>com.guazicola.web.UserServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>UserServlet</servlet-name>
    <url-pattern>/userServlet</url-pattern>
  </servlet-mapping>
</web-app>

配置上tomcat:
在这里插入图片描述
然后点击运行,就可以出来网页:

在这里插入图片描述
在后面加上userServlet域名:
在这里插入图片描述
后台就可以看到我们在userDao中定义的东西:
在这里插入图片描述

二. ContextListener监听器

ApplicationContext在这里是直接写在了UserServlet中。如果很多方法中都需要ApplicationContext,那他会被创建多次。这里用监听器的方法,在服务器开始运行的时候就讲ApplicationContext创建的对象放在sevlet四大作用域之一的application域中,要用的时候直接提取已经建好的对象。
定义一个ContextListener类:
在这里插入图片描述
写上:

public class ContextListener implements ServletContextListener {
    @Override
//    启动服务器时执行
    public void contextInitialized(ServletContextEvent servletContextEvent) {
        ApplicationContext app = new AnnotationConfigApplicationContext(SpringConfig.class);
//        得到application域
        ServletContext servletContext = servletContextEvent.getServletContext();
//        将app放到域中
        servletContext.setAttribute("app",app);
    }

    @Override
//    关闭服务器时执行
    public void contextDestroyed(ServletContextEvent servletContextEvent) {

    }
}

在web.xml中配置监听器:

<listener>
  <listener-class>com.guazicola.listener.ContextListener</listener-class>
</listener>

在userServlet中获取app:

public class UserServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        得到application域
        ServletContext servletContext = this.getServletContext();
//        取出域中的app
        ApplicationContext app = (ApplicationContext) servletContext.getAttribute("app");
        UserDao userDao = app.getBean(UserDao.class);
        userDao.save();
    }
}

运行程序,可以得到和之前一样的结果

三. 将两个写死的名字解耦合

1. applicationContext.xml

前面在提取spring配置文件的时候,我们用的是:

ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");

现在我们将applicationContext.xml配置在web.xml中:

<!--  全局初始化参数-->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>applicationContext.xml</param-value>
  </context-param>

然后在监听器中读取:

public class ContextListener implements ServletContextListener {
    @Override
//    启动服务器时执行
    public void contextInitialized(ServletContextEvent servletContextEvent) {
//        得到application域
        ServletContext servletContext = servletContextEvent.getServletContext();
//        读取web.xml中的全局参数
        String contextConfigLocation = servletContext.getInitParameter("contextConfigLocation");
//        读取spring配置文件
        ApplicationContext app = new ClassPathXmlApplicationContext(contextConfigLocation);
//        将app放到域中
        servletContext.setAttribute("app",app);
    }

    @Override
//    关闭服务器时执行
    public void contextDestroyed(ServletContextEvent servletContextEvent) {

    }
}

2. app

在监听器的代码中,app也是写死了的。
这里我们将取app的操作托管给一个工具类:WebApplicationContextUtils.java
在这里插入图片描述
写上代码:

public class WebApplicationContextUtils {
    public static ApplicationContext getApplicationContext(ServletContext servletContext){
        return (ApplicationContext) servletContext.getAttribute("app");
    }
}

这里实现的逻辑是:在监听器中,我们往application域放入app,随后在userServlet中取app,取app的方法是调用该工具类,这个工具类去提取app,然后返回给userServlet。

userServlet的代码也要进行相应的修改:

public class UserServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ServletContext servletContext = this.getServletContext();
//        调用工具类,提取app
        ApplicationContext app = WebApplicationContextUtils.getApplicationContext(servletContext);
        UserDao userDao = app.getBean(UserDao.class);
        userDao.save();
    }
}

四. spring框架中的监听器

之前监听器的那些配置,spring已经给我们封装好了,可以直接用现成的:
首先传入坐标:
在这里插入图片描述
同样去web.xml中配置监听器和全局初始化参数:

<!--  全局初始化参数-->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
  </context-param>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

在userServlet中调用spring框架的WebApplicationContextUtils:

public class UserServlet extends HttpServlet {
  @Override
  protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
      ServletContext servletContext = this.getServletContext();
//        调用工具类,提取app
      ApplicationContext app = WebApplicationContextUtils.getWebApplicationContext(servletContext);
      UserDao userDao = app.getBean(UserDao.class);
      userDao.save();
  }
}
举报

相关推荐

0 条评论