第六节_spring集成web环境

Spring集成web环境

1、前期准备工作

  • 我先准备下web的环境(Servlet)

  • 导入Servlet依赖

  • <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.1.0</version>
    </dependency>
    
  • 创建web

@WebServlet("/UserServlet")
public class UserServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 加载我们的配置文件
        ApplicationContext app = new AnnotationConfigApplicationContext(SpringConfigurataion.class);
        // 获取service层对象
        UserService userService = app.getBean(UserService.class);
        // 调用他的save方法
        userService.save();
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}
  • 运行效果
  • image-20220322084002235

2、ApplicationContext应用上下文获取方式

2.1、原始方法的弊端

  • 我们刚刚创建了web环境下的servlet充当controller层,但是原始环境下,我们想要使用我们在spring容器中创建的对象,依旧需要加载配置文件和getBean()获取到我们想要的对象类型

  • 前端发送过来的请求不止一个,如果有多个请求发送过来,那么我们对于配置类的加载就需要多次加载,异常的麻烦

  • 我们依旧加载了ApplicationContext app = new AnnotationConfigApplicationContext(SpringConfigurataion.class);

  • 和使用getBean()获取我们的UserService的对象

  • @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 加载我们的配置文件
        ApplicationContext app = new AnnotationConfigApplicationContext(SpringConfigurataion.class);
        // 获取service层对象
        UserService userService = app.getBean(UserService.class);
        // 调用他的save方法
        userService.save();
    }
    

2.2、解决办法–图解

image-20220322084457565

3、自定义Context-Linstener(解决2.2中问题的实践环节)

3.1、创建我们的监听器类–监听ServletContext

  • 需要实现ServletContextListener类,重写他的两个方法
public class ContexLoadListener implements ServletContextListener {
    
    @Override
    public void contextInitialized(ServletContextEvent servletContextEvent) {

    }

    @Override
    public void contextDestroyed(ServletContextEvent servletContextEvent) {

    }
}

3.2、方法简述

  • contextInitialized:ServletContex被创建的时候调用,我们可以在这个方法内部创建我们的Spring应用上下文的对象(相当于加载我们的配置类或者配置文件)
  • contextDestroyed:ServletContex对象销毁的时候调用,不讲,暂时没用

3.3、方法实践

  • 在ServletContext对象创建的时候加载我们的配置类
  • 创建完毕之后获取到ServletContex对象,并将我们Spring应用上下文的对象存储到其中(app存到ServletContex中)
public class ContexLoadListener implements ServletContextListener {

    @Override
    public void contextInitialized(ServletContextEvent servletContextEvent) {
        // 初始化我们程序的配置,加载配置文件
        ApplicationContext app = new AnnotationConfigApplicationContext(SpringConfigurataion.class);
        // 对象创建完毕,为了能让其它层拿到,将其存储在域中(最大域:ServletContex域)
        // 获取这个对象
        ServletContext context = servletContextEvent.getServletContext();
        //将其存储进域中
        context.setAttribute("app",app);
    }

    @Override
    public void contextDestroyed(ServletContextEvent servletContextEvent) {

    }
}

3.4、配置到Tomcat服务器当中

  • 打开我们的web.xml中

  • 创建监听器类的全限定名放入其中

  • <listener>
        <!-- 将我们配置的监听器类的全限定名放在这里 -->
        <listener-class>com.waves.Listener.ContexLoadListener</listener-class>
    </listener>
    

3.5、重新配置web层并重新设置我们的方法

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // 获取我们的ServletContex对象
    ServletContext servletContext = request.getServletContext();
    // 获取我们的上下文对象
    ApplicationContext  app = (ApplicationContext) servletContext.getAttribute("app");

    // 获取service层对象
    UserService userService = app.getBean(UserService.class);
    // 调用他的save方法
    userService.save();
}

image-20220322091626130

3.4、代码优化(ApplicationContext app = (ApplicationContext) servletContext.getAttribute(“app”))

  • 这行代码是耦合死的,如何解决?

  • 创建一个工具类,这个工具类返回一个ApplicationContext对象,这样我们在web层启动配置类的时候就不需要记住那个**“app”**的前缀了

  • 创建一个WebServletContextUtil

  • public class WebServletContextUtil {
        // 设置一个静态方法,该方法返回一个ApplicationContext对象
        public static Object getApplicationContext(ServletContext context) {
            return context.getAttribute("app");
        }
    }
    
  • 重新改造下我们的web层代码

  • @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 获取我们的ServletContex对象
        ServletContext servletContext = request.getServletContext();
        // 调用我们的工具类--获取上下文对象
        ApplicationContext app = (ApplicationContext) WebServletContextUtil.getApplicationContext(servletContext);
        // 获取service层对象
        UserService userService = app.getBean(UserService.class);
        // 调用他的save方法
        userService.save();
    }
    

4、经典白学-使用Spring为我们封装的工具来获取Spring应用上下文对象

image-20220322101138947

4.1、使用Spring提供的上下文工具

image-20220322101201873

  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值