首頁  >  文章  >  Java  >  idea怎麼使用外置tomcat配置springboot

idea怎麼使用外置tomcat配置springboot

WBOY
WBOY轉載
2023-05-12 11:10:13784瀏覽
  •  建立一個maven專案

  • 匯入springboot依賴,注意底下註解部分

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.znsd.springboot</groupId>
    <artifactId>springboot-jsp</artifactId>
    <version>1.0-SNAPSHOT</version>
    
    
    <!-- 一定要声明war包 -->
    <packaging>war</packaging>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.12.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- 去除springboot默认tomcat依赖,让其在生成war包时无效, -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <!--在编译和测试有效,生成war包时无效-->
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

idea怎麼使用外置tomcat配置springboot

idea怎麼使用外置tomcat配置springboot

#完成下圖操作儲存即可

idea怎麼使用外置tomcat配置springboot

#設定tomcat啟動項目

idea怎麼使用外置tomcat配置springboot

idea怎麼使用外置tomcat配置springboot

idea怎麼使用外置tomcat配置springboot

idea怎麼使用外置tomcat配置springboot

##設定視圖解析器

idea怎麼使用外置tomcat配置springboot

建立一個springboot主程式

@SpringBootApplication
public class SpringBootMain {
    public static void main(String[] args) {
        SpringApplication.run(SpringBootMain.class,args);
    }
}

必須寫一個SpringBootServletInitializer的子類,並且呼叫configure方法裡面的固定寫法

public class ServletInitializer extends SpringBootServletInitializer {
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        //传入SpringBoot的主程序,
        return application.sources(SpringBootMain.class);
    }
}

然後啟動tomcat,控制台輸出了spring就啟動成功了

idea怎麼使用外置tomcat配置springboot

以上是idea怎麼使用外置tomcat配置springboot的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:yisu.com。如有侵權,請聯絡admin@php.cn刪除