Spring 启动应用程序没有显示错误,但 tomcat 未初始化且 application.properties 未使用

Spring Boot Application displays no error, but tomcat not initialized and application.properties is unused

我使用Spring Initializr创建了一个新项目,并使用Intellij IDEA 2020.3.2版本ide打开了它。 maven 在 pom.xml 文件中安装所有依赖项后,我 运行 DemoApplication class 中的主要方法。我没有添加任何新的或不同于 Spring Initializr.

创建的默认项目

出现了两个问题:

1-控制台只输出了这3条日志,Tomcat没有初始化

2021-02-03 23:48:45.073 INFO 15872 --- [ main] com.example.DemoApplication: Starting DemoApplication using Java 1.8.0_281 on DESKTOP-M with PID 15872 (D:\demo\target\classes started by M in D:\demo)

2021-02-03 23:48:45.078 INFO 15872 --- [ main] com.example.DemoApplication : No active profile set,falling back to default profiles: default

2021-02-03 23:48:46.355 INFO 15872 --- [ main] com.example.DemoApplication: Started DemoApplication in 2.062 seconds (JVM running for 2.87)

Process finished with exit code 0

2- 我在 application.properties 文件中写入的任何内容都会变成灰色(未使用)

看了很多教程,照着做。他们的控制台输出总是有 3 个以上的语句,甚至是彩色的,而我的不是

我的pom.xml

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.2</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.1.1</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>com.oracle.database.jdbc</groupId>
            <artifactId>ojdbc8</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

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

</project>

DemoApplication.java

package com.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication {

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

}

如果你能帮助我弄清楚如何正确 运行 项目并启动服务器并 运行ning,我将不胜感激。

我应该再添加 运行 个选项吗?

外部图书馆:

当您 运行 DemoApplication 使用(独立 java)(IDE 运行 时,此问题可重现为 simple war initialisier ) 配置。

solution/correct 用法是在 (maven) spring-boot:run (IDE 运行) 配置中 运行 (项目)。

喜欢:

看起来您正在使用 has no support for Spring Boot.

的 IntelliJ IDEA Community Edition

这就是属性未突出显示且输出未着色的原因。

IntelliJ IDEA Ultimate 将自动使用 Spring Boot Run/Debug configuration type

作为解决方法,您可以 运行 通过 Maven spring-boot:run 您的代码。

我在使用您提供的 pox.xml 设置项目时看到了相同的行为。

“Include dependencies with”Provided”为我修复了它。但是我不得不像这样重新加载 Maven 依赖项几次:pom.xml(右键单击)-> Maven -> 重新加载项目。

我仍然建议您删除 spring-boot-starter-tomcat 中的 <scope>provided</scope>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<!-- Comment this out -->
<!-- <scope>provided</scope> -->
</dependency>

这是一个很好的答案 -

如果您仍然需要此项目 运行 在外部 Tomcat 容器中,因此想要将 spring-boot-spring-boot-starter 依赖项标记为“已提供”,您可以为该配置 -