spring boot application.yml 完全被忽略(存在于目标/类下)

问题描述 投票:0回答:1

运行 Spring Boot 应用程序如下: 我的端口

8080
正在使用中,因此我正在尝试更改它。另外,我故意给kafka错误的bootstrap服务器,看看是否生效。

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

但请注意日志(仅粘贴部分内容),引导服务器属性仍然是默认值 9092,服务器/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);
    }

}
java spring-boot maven spring-kafka
1个回答
0
投票

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

server:
  port: 9098
© www.soinside.com 2019 - 2024. All rights reserved.