Spring Boot 配置文件无法正常工作

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

你好,我有一个 Maven Spring Boot Rest 项目。我想向其中添加开发和生产配置,但它不起作用。

在 /src/main/resources 下我创建了 application.yaml、application-dev.yaml 和 application-prod.yaml

# application.yaml
spring:
  profiles:
    active: prod
# application-dev.yaml
server:
  port: 8080
spring:
  datasource:
    url: jdbc:h2:mem:mydb
    username: sa
    password: password
    driverClassName: org.h2.Driver
# application-prod.yaml
server:
  port: 8099
spring:
  datasource:
    url: jdbc:postgresql://localhost:5432/WsDb
    username: postgres
    password: postgres
    driverClassName: org.postgresql.Driver

在 pom.xml 我有以下内容:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>3.2.0</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
...
</dependencies>
<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>
<profiles>
    <profile>
        <id>dev</id>
        <properties>
            <spring.profiles.active>dev</spring.profiles.active>
        </properties>
        <dependencies>
            <dependency>
                <groupId>com.h2database</groupId>
                <artifactId>h2</artifactId>
            </dependency>
        </dependencies>
    </profile>
    <profile>
        <id>prod</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <properties>
            <spring.profiles.active>prod</spring.profiles.active>
        </properties>
        <dependencies>
            <dependency>
                <groupId>org.postgresql</groupId>
                <artifactId>postgresql</artifactId>
                <version>${postgres.version}</version>
            </dependency>
        </dependencies>
    </profile>
</profiles>

当我使用命令

mvn clean package spring-boot:run -Dspring.profiles.active = prod
在 Intellij Idea 中使用调试配置运行应用程序时,它显示:

测试阶段输出

The following 1 profile is active: "prod"   
Bootstrapping Spring Data JPA repositories in DEFAULT mode.  
Finished Spring Data repository scanning in 164 ms. Found 2 JPA repository interfaces.  
Replacing 'dataSource' DataSource bean with embedded version   
Starting embedded database: url='jdbc:h2:mem:4348112a-775e-4b78-a5b7-e5bcfdf21761  
HHH000204: Processing PersistenceUnitInfo [name: default]  

运行阶段输出

No active profile set, falling back to 1 default profile: "default"
Bootstrapping Spring Data JPA repositories in DEFAULT mode.
Finished Spring Data repository scanning in 116 ms. Found 2 JPA repository interfaces.
Tomcat initialized with port 8080 (http) 
Starting service [Tomcat]   
spring maven profile boot
1个回答
0
投票

据我所知,命令行参数

-Dspring.profiles.active=prod
没有任何影响。 spring-boot 插件文档描述了如何激活配置文件。

在您的项目中,

prod
配置文件仅由于maven配置文件
spring.profiles.active
中的属性
prod
而被激活。这引诱我上错了渡轮。

在 spring 文档中还有一个如何使用 Maven 配置文件激活 spring 配置文件的示例。

© www.soinside.com 2019 - 2024. All rights reserved.