maven spring Boot application.yml完全忽略(存在于target/classes下)

qyzbxkaa  于 2023-11-17  发布在  Maven
关注(0)|答案(1)|浏览(181)

运行spring Boot 应用程序如下:我的端口8080正在使用中,所以我正在尝试更改它。此外,我故意为Kafka提供错误的引导服务器,以查看它是否生效。
mvn spring-boot:run -Dspring.config.location=file:/src/main/resources/application.yml,classpath:application.yml
我验证了application.yml存在于预期位置下,如下所示

~/Documents/springboot/codedecode25:=> cat target/classes/application.yml
spring:
  kafka:
    producer:
      bootstrap-servers: localhost:9093
      key-serializer: org.apache.kafka.common.serialization.StringSerializer
      value-serializer: org.apache.kafka.common.serialization.StringSerializer
  server:
    port: 9098

字符串
但请注意日志(仅粘贴部分日志),bootstrap服务器属性仍为默认值9092,server/tomcat从8080开始,应用程序无法启动。

***************************
APPLICATION FAILED TO START
***************************

Description:

Web server failed to start. Port 8080 was already in use.


在启动期间(粘贴部分日志以显示它从8080开始,并且Kafka服务器端口仍然是默认的)

edecode25)
2023-10-28 19:12:25.957  INFO 51238 --- [           main] c.c.KafkaDemo.KafkaDemoApplication       : No active profile set, falling back to 1 default profile: "default"
2023-10-28 19:12:26.808  INFO 51238 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2023-10-28 19:12:26.820  INFO 51238 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2023-10-28 19:12:26.820  INFO 51238 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.65]
2023-10-28 19:12:26.907  INFO 51238 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2023-10-28 19:12:26.907  INFO 51238 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 908 ms
2023-10-28 19:12:27.380  INFO 51238 --- [           main] o.a.k.clients.consumer.ConsumerConfig    : ConsumerConfig values:
    allow.auto.create.topics = true
    auto.commit.interval.ms = 5000
    auto.offset.reset = latest
    bootstrap.servers = [localhost:9092]
    check.crcs = true


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.7.2</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.codedecode</groupId>
    <artifactId>KafkaDemo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>KafkaDemo</name>
    <description>Demo project for Kafka implementation</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.kafka</groupId>
            <artifactId>spring-kafka</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.kafka</groupId>
            <artifactId>spring-kafka-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

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

        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <targetPath>${project.build.outputDirectory}</targetPath>
                <includes>
                    <include>application.yml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
        </resources>
    </build>

</project>


还有,当我使用intellij时,application.yml甚至不存在。我是不是在我的spring Boot 应用程序中使用了一些错误的注解?

@SpringBootApplication
public class KafkaDemoApplication {

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

}

v6ylcynt

v6ylcynt1#

server.port属性不属于spring。请尝试将其移动到application.yml文件的根目录。

server:
  port: 9098

字符串

相关问题