最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Spring集成Web環(huán)境的實例詳解

 更新時間:2022年02月09日 10:12:00   作者:山林不向四季起誓·  
這篇文章主要介紹了Spring集成Web環(huán)境,本文通過實例圖文相結(jié)合給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下

Spring整合Web開發(fā)需要導(dǎo)入的坐標(biāo)

<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開發(fā)需要的依賴-->
        <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需要的坐標(biāo)-->
            <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>
<!--        下面是上下文應(yīng)用整和的坐標(biāo)-->
            <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監(jiān)聽器
② 使用WebApplicationContextUtils獲得應(yīng)用上下文

獲取ApplicationContext對象是從servletContext域中獲取的,還有就是使用WebApplicationContextUtils獲取app。 可以直接從Spring獲取app對象,省去了自己創(chuàng)建。還有就是以后要使用到多次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">
    <!--全局初始化參數(shù)-->
    <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下面有紅色波浪線的話,可能是復(fù)制的坐標(biāo),需要重寫寫一遍,還有就是是maven可能是3.6.3版本,idea和maven可能會出現(xiàn)沖突,所以要降低maven版本,改為3.6.1版本即可。

在部署tomcat的時候,可能回出現(xiàn)監(jiān)聽器的問題,如果是tomcat10,就需要降低tomcat版本,如果是tomcat8.5.5及其一下的版本,就需要做一下操作。

在這里插入圖片描述

在這里插入圖片描述

在這里插入圖片描述

就會出現(xiàn)lib包,再重新部署一下項目就可以了。

到此這篇關(guān)于Spring集成Web環(huán)境的文章就介紹到這了,更多相關(guān)Spring集成Web環(huán)境內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Java實現(xiàn)拓撲排序算法的示例代碼

    Java實現(xiàn)拓撲排序算法的示例代碼

    在圖論中,拓撲排序(Topological Sorting)是一個有向無環(huán)圖(DAG, Directed Acyclic Graph)的所有頂點的線性序列。本文將為大家講講拓撲排序算法的原理及實現(xiàn),需要的可以參考一下
    2022-07-07
  • SpringBoot生成License的實現(xiàn)示例

    SpringBoot生成License的實現(xiàn)示例

    License指的是版權(quán)許可證,那么對于SpringBoot項目,如何增加License呢?本文就來介紹一下,感興趣的可以了解一下
    2021-06-06
  • SpringBoot生產(chǎn)環(huán)境打包如何去除無用依賴

    SpringBoot生產(chǎn)環(huán)境打包如何去除無用依賴

    這篇文章主要介紹了SpringBoot生產(chǎn)環(huán)境打包如何去除無用依賴問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-09-09
  • Spring Cloud Gateway 緩存區(qū)異常問題及解決方案

    Spring Cloud Gateway 緩存區(qū)異常問題及解決方案

    最近在測試環(huán)境spring cloud gateway突然出現(xiàn)了異常,接下來通過本文給大家介紹Spring Cloud Gateway 緩存區(qū)異常問題解決方案,需要的朋友可以參考下
    2024-06-06
  • java解析Excel文件的方法實例詳解

    java解析Excel文件的方法實例詳解

    在日常工作中,我們常常會進行文件讀寫操作,除去我們最常用的純文本文件讀寫,更多時候我們需要對Excel中的數(shù)據(jù)進行讀取操作,下面這篇文章主要給大家介紹了關(guān)于java解析Excel文件的方法,需要的朋友可以參考下
    2022-06-06
  • Gauva使用ListenableFuture介紹說明

    Gauva使用ListenableFuture介紹說明

    并發(fā)是一個困難問題,但是通過強大和強大的抽象能夠顯著的簡化工作。為了簡化問題,Gauva使用ListenableFuture擴展了JDK的Future接口,這篇文章主要介紹了Gauva使用ListenableFuture
    2023-01-01
  • springboot如何集成Swagger2

    springboot如何集成Swagger2

    這篇文章主要介紹了springboot集成Swagger2的方法,幫助大家更好的理解和使用springboot框架,感興趣的朋友可以了解下
    2020-12-12
  • java遍歷讀取整個redis數(shù)據(jù)庫實例

    java遍歷讀取整個redis數(shù)據(jù)庫實例

    這篇文章主要介紹了java遍歷讀取整個redis數(shù)據(jù)庫實例,使用支持正則表達式的key搜索方法jedis.keys(“*”)實現(xiàn),需要的朋友可以參考下
    2014-05-05
  • Java 獲取當(dāng)前系統(tǒng)時間的三種方法

    Java 獲取當(dāng)前系統(tǒng)時間的三種方法

    這篇文章主要介紹了Java 獲取當(dāng)前系統(tǒng)時間的三種方法,幫助大家利用Java處理時間,感興趣的朋友可以了解下
    2020-10-10
  • Java 中String StringBuilder 與 StringBuffer詳解及用法實例

    Java 中String StringBuilder 與 StringBuffer詳解及用法實例

    這篇文章主要介紹了Java 中String StringBuilder 與 StringBuffer詳解及用法實例的相關(guān)資料,需要的朋友可以參考下
    2017-02-02

最新評論

宜章县| 巩留县| 新乐市| 三穗县| 威远县| 新津县| 萍乡市| 柳河县| 田林县| 铜鼓县| 长阳| 平遥县| 海南省| 疏附县| 普格县| 扶沟县| 嘉荫县| 和硕县| 贡觉县| 辽阳县| 普格县| 中江县| 武川县| 泰宁县| 乌鲁木齐县| 奇台县| 峨边| 临安市| 广东省| 亚东县| 夏邑县| 鄂尔多斯市| 江门市| 名山县| 墨脱县| 贵港市| 佛冈县| 江津市| 阳东县| 凌源市| 兰坪|