Spring集成Web环境的实例详解

 更新时间:2022年02月09日 10:12:00   作者:山林不向四季起誓·  
这篇文章主要介绍了Spring集成Web环境,本文通过实例图文相结合给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

Spring整合Web开发需要导入的坐标

<properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
    </properties>
<build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <release>11</release>
                </configuration>
            </plugin>
        </plugins>
    </build>
<dependencies>
<!--        下面是web开发需要的依赖-->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>3.1.0</version>
        </dependency>

<!--        下面是spring依赖-->
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.2.4.RELEASE</version>
<!--        下面两个是spring整和Web需要的坐标-->
            <artifactId>javax.servlet-api</artifactId>
            <scope>provided</scope>
            <groupId>Maven_Repository.javax.servlet.jsp</groupId>
            <artifactId>javax.servlet.jsp-api</artifactId>
            <version>2.3.1</version>
<!--        下面是上下文应用整和的坐标-->
            <artifactId>spring-web</artifactId>
            <version>5.0.5.RELEASE</version>
            <groupId>Maven_Repository.com.h2database</groupId>
            <artifactId>h2</artifactId>
            <version>1.3.158</version>
    </dependencies>

① 配置ContextLoaderListener监听器
② 使用WebApplicationContextUtils获得应用上下文

获取ApplicationContext对象是从servletContext域中获取的,还有就是使用WebApplicationContextUtils获取app。 可以直接从Spring获取app对象,省去了自己创建。还有就是以后要使用到多次app对象,所以就是省去了new出多了app对象。

dao层代码

package com.itheima.dao.impl;

import com.itheima.dao.UserDao;
public class UserDaoImpl implements UserDao {
    public void save() {
        System.out.println("save is running");
    }
}

service层代码

package com.itheima.service.impl;

import com.itheima.dao.UserDao;
import com.itheima.service.UserService;
public class UserServiceImpl implements UserService {
    private UserDao userDao;
    
    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }
    public void save() {
        userDao.save();
}

web层

package com.itheima.web;

import com.itheima.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/servlet")
public class UserServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
        ServletContext servletContext = this.getServletContext();
//         ApplicationContext app = (ApplicationContext) servletContext.getAttribute("app");
//        ApplicationContext app = WebApplicationContextUtils.getWebApplicationContext(servletContext);
        ApplicationContext app = WebApplicationContextUtils.getWebApplicationContext(servletContext);
        UserService userService = (UserService) app.getBean("userService");
        userService.save();
    }
}

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="userDao" class="com.itheima.dao.impl.UserDaoImpl"></bean>
    <bean id="userService" class="com.itheima.service.impl.UserServiceImpl">
        <property name="userDao" ref="userDao"></property>
    </bean>
</beans>

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">
    <!--全局初始化参数-->
    <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>
</web-app>

问题:

在配置maven时候,project下面有红色波浪线的话,可能是复制的坐标,需要重写写一遍,还有就是是maven可能是3.6.3版本,idea和maven可能会出现冲突,所以要降低maven版本,改为3.6.1版本即可。

在部署tomcat的时候,可能回出现监听器的问题,如果是tomcat10,就需要降低tomcat版本,如果是tomcat8.5.5及其一下的版本,就需要做一下操作。

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

就会出现lib包,再重新部署一下项目就可以了。

到此这篇关于Spring集成Web环境的文章就介绍到这了,更多相关Spring集成Web环境内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Java实现拓扑排序算法的示例代码

    Java实现拓扑排序算法的示例代码

    在图论中,拓扑排序(Topological Sorting)是一个有向无环图(DAG, Directed Acyclic Graph)的所有顶点的线性序列。本文将为大家讲讲拓扑排序算法的原理及实现,需要的可以参考一下
    2022-07-07
  • SpringBoot生成License的实现示例

    SpringBoot生成License的实现示例

    License指的是版权许可证,那么对于SpringBoot项目,如何增加License呢?本文就来介绍一下,感兴趣的可以了解一下
    2021-06-06
  • SpringBoot生产环境打包如何去除无用依赖

    SpringBoot生产环境打包如何去除无用依赖

    这篇文章主要介绍了SpringBoot生产环境打包如何去除无用依赖问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-09-09
  • Spring Cloud Gateway 缓存区异常问题及解决方案

    Spring Cloud Gateway 缓存区异常问题及解决方案

    最近在测试环境spring cloud gateway突然出现了异常,接下来通过本文给大家介绍Spring Cloud Gateway 缓存区异常问题解决方案,需要的朋友可以参考下
    2024-06-06
  • java解析Excel文件的方法实例详解

    java解析Excel文件的方法实例详解

    在日常工作中,我们常常会进行文件读写操作,除去我们最常用的纯文本文件读写,更多时候我们需要对Excel中的数据进行读取操作,下面这篇文章主要给大家介绍了关于java解析Excel文件的方法,需要的朋友可以参考下
    2022-06-06
  • Gauva使用ListenableFuture介绍说明

    Gauva使用ListenableFuture介绍说明

    并发是一个困难问题,但是通过强大和强大的抽象能够显著的简化工作。为了简化问题,Gauva使用ListenableFuture扩展了JDK的Future接口,这篇文章主要介绍了Gauva使用ListenableFuture
    2023-01-01
  • springboot如何集成Swagger2

    springboot如何集成Swagger2

    这篇文章主要介绍了springboot集成Swagger2的方法,帮助大家更好的理解和使用springboot框架,感兴趣的朋友可以了解下
    2020-12-12
  • java遍历读取整个redis数据库实例

    java遍历读取整个redis数据库实例

    这篇文章主要介绍了java遍历读取整个redis数据库实例,使用支持正则表达式的key搜索方法jedis.keys(“*”)实现,需要的朋友可以参考下
    2014-05-05
  • Java 获取当前系统时间的三种方法

    Java 获取当前系统时间的三种方法

    这篇文章主要介绍了Java 获取当前系统时间的三种方法,帮助大家利用Java处理时间,感兴趣的朋友可以了解下
    2020-10-10
  • Java 中String StringBuilder 与 StringBuffer详解及用法实例

    Java 中String StringBuilder 与 StringBuffer详解及用法实例

    这篇文章主要介绍了Java 中String StringBuilder 与 StringBuffer详解及用法实例的相关资料,需要的朋友可以参考下
    2017-02-02

最新评论