Spring -- 集成web环境

1. 集成web环境

准备工作:

开始之前先进行基础环境搭建


1.1 ApplicationContext应用 上下文获取方式

在这里插入图片描述


1.1.1 创建监听器

在这里插入图片描述

ContextLoaderListener.java

package com.tian.listener;


import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

public class ContextLoaderListener implements ServletContextListener {

    /**
     * 使用ServletContextListener监听Web应用的启动,
     * 我们可以在Web应用启动时,就加载Spring的配置文件,
     * 创建应用上下文对象ApplicationContext,
     * 在将其存储到最大的域servletContext域中,
     * 这样就可以在任意位置从域中获得应用上下文ApplicationContext对象了。
     */
    public void contextInitialized(ServletContextEvent servletContextEvent) {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        //将Spring的应用上下文对象存储到ServletContext域中
        servletContextEvent.getServletContext().setAttribute("context", context);
        System.out.println("spring容器创建完毕....");
    }

    public void contextDestroyed(ServletContextEvent servletContextEvent) {

    }
}

1.1.2 配置监听器

在这里插入图片描述
web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
    <servlet>
        <servlet-name>UserServlet</servlet-name>
        <servlet-class>com.tian.web.UserServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>UserServlet</servlet-name>
        <url-pattern>/userServlet</url-pattern>
    </servlet-mapping>

    <!--    配置监听器-->
    <listener>
        <listener-class>com.tian.listener.ContextLoaderListener</listener-class>
    </listener>
</web-app>

1.1.3 重新get请求 更换获取Spring上下文的方式

之前的方式:

在这里插入图片描述

现在的方式:从servletContext域中获取context

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) {
        ServletContext servletContext = this.getServletContext();
        ApplicationContext context = (ApplicationContext) servletContext.getAttribute("context");
        UserServiceImpl bean = context.getBean(UserServiceImpl.class);
        bean.save();
    }

1.1.4 启动服务器测试

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述


1.2 Spring提供获取应用上下文的工具

Spring提供了一个监听器ContextLoaderListener,该监听器内部加载Spring配置文件,创建应用上下文对象,并存储到ServletContext域中,提供了一个客户端工具WebApplicationContextUtils供使用者获得应用上下文对象。(可以把我们之前创建的监听器删了,现在用Spring提供的监听器)
在这里插入图片描述


1.2.1 导入Spring集成web的坐标

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>5.3.7</version>
        </dependency>

1.2.2 配置ContextLoaderListener监听器

在这里插入图片描述

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
    <servlet>
        <servlet-name>UserServlet</servlet-name>
        <servlet-class>com.tian.web.UserServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>UserServlet</servlet-name>
        <url-pattern>/userServlet</url-pattern>
    </servlet-mapping>

    <!--    全局参数-->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>
    <!--    配置Spring监听器-->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
</web-app>

在这里插入图片描述
在这里插入图片描述


1.2.3 开始测试

现在的UserServlet代码:

package com.tian.web;


import com.tian.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class UserServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //        获取ApplicationContext
        ServletContext servletContext = this.getServletContext();
        //        获取Spring Ioc容器的上下文
        ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(servletContext);
        //        从Ioc容器获取bean
        UserService userService = context.getBean(UserService.class);
        // 调用bean的方法
        userService.save();
    }
}

启动服务器:

在这里插入图片描述
在这里插入图片描述



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

CodeJiao

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值