【问题标题】:How to load environment specific configurations and properties with Spring boot using Maven Profiles and Settings Example如何使用 Maven 配置文件和设置示例通过 Spring Boot 加载环境特定的配置和属性
【发布时间】:2018-10-24 13:18:39
【问题描述】:

根据 pom.xml 中的配置文件选择从 settings.xml 中读取 mongodb 用户名和密码。 我不知道该怎么做。谁能举个例子。

我关注了参考链接

https://examples.javacodegeeks.com/enterprise-java/spring/loading-environment-specific-configurations-properties-spring-using-maven-profiles-xml-settings-file-example/

pom.xml

<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">//here showing error


<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<packaging>jar</packaging>
<version>1.0</version>
<name>Spring Maven Properties Example Code</name>

<properties>

    <!-- Generic properties -->
    <java.version>1.8</java.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <resource.directory>src/main/resources</resource.directory>

</properties>
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.9.RELEASE</version>

</parent>
<dependencies>

    <dependency>
        <groupId>ch.qos.logback</groupId>
        <artifactId>logback-classic</artifactId>
        <version>1.1.11</version>
        <exclusions>
            <exclusion>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-api</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.postgresql</groupId>
        <artifactId>postgresql</artifactId>
    </dependency>
    <dependency>
        <groupId>org.apache.maven.shared</groupId>
        <artifactId>maven-filtering</artifactId>
        <version>1.3</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-mongodb</artifactId>
    </dependency>
    <!-- Test Artifacts -->


</dependencies>

<profiles>
    <profile>
        <id>dev</id>
        <!-- Dev Env. Properties -->
        <properties>
            <profile.name>${profile.name}</profile.name>
            <!-- Database Properties -->
            <db.driverClass>${db.driverClass}</db.driverClass>
            <db.connectionURL>${db.connectionURL}</db.connectionURL>
            <db.username>${db.username}</db.username>
            <db.password>${db.password}</db.password>

        </properties>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
    </profile>
    <profile>
        <id>test</id>
        <!-- Test Env. Properties -->
        <properties>
            <profile.name>${profile.name}</profile.name>
            <!-- Database Properties -->
            <db.driverClass>${db.driverClass}</db.driverClass>
            <db.connectionURL>${db.connectionURL}</db.connectionURL>
            <db.username>${db.username}</db.username>
            <db.password>${db.password}</db.password>

        </properties>
    </profile>
    <profile>
        <id>prod</id>
        <!-- Prod Env. Properties -->
        <properties>
            <profile.name>${profile.name}</profile.name>
            <!-- Database Properties -->
            <db.driverClass>${db.driverClass}</db.driverClass>
            <db.connectionURL>${db.connectionURL}</db.connectionURL>
            <db.username>${db.username}</db.username>
            <db.password>${db.password}</db.password>

        </properties>
    </profile>
</profiles>

<build>

    <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.2</version>
            <configuration>
                <source>${java.version}</source>
                <target>${java.version}</target>
            </configuration>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-resources-plugin</artifactId>
            <version>2.6</version>
            <configuration>
                <!-- specify UTF-8, ISO-8859-1 or any other file encoding -->
                <encoding>${project.build.sourceEncoding}</encoding>
            </configuration>
        </plugin>
    </plugins>

    <resources>
        <!-- Placeholders that are found from the files located in the configured 
            resource directories are replaced with the property values found from the 
            profile specific configuration file. -->
        <resource>
            <directory>${resource.directory}</directory>
            <filtering>true</filtering>
        </resource>
    </resources>

</build>

application.properites

db.connectionURL=${db.connectionURL}
db.username=${db.username}
db.password=${db.password}

我将 settings.xml 文件放在当前项目目录中。 设置.xml

<profiles>
        <profile>
            <id>dev</id>
            <!-- Dev Env. Properties -->
            <properties>
                <profile.name>dev</profile.name>
                <!-- Database Properties -->
                <db.driverClass>com.mysql.jdbc.Driver</db.driverClass>
<db.connectionURL>jdbc:postgresql://localhost:5432/springbootdb</db.connectionURL>
<db.username>postgres</db.username>
<db.password>postgres</db.password>

            </properties>
        </profile>
        </profiles>

主应用程序

public class DemoApplication {

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

我在这里测试 postgres 天气它是否工作但在 pom.xml 文件中显示错误

<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"> 

错误是

Project build error: 
Resolving expression: '${db.password}': Detected the following recursive expression cycle in 'db.password': [db.password]

我想测试 postgres 和 monngodb 的场景。

帮帮我..

【问题讨论】:

  • 我不确定我们能否为您提供比您链接的示例更明确的示例。到目前为止,您能向我们展示您的代码吗?
  • @RemyG 虽然我正在使用 spring boot 执行上面链接中提供的相同示例。它在 pom.xml 中显示,如 Project build error: Resolving expression: '${db.connectionURL}': Detected the following recursive expression cycle in 'db.connectionURL': [db.connectionURL]
  • 请展示你的源代码。
  • 如果您使用的是 spring boot,请使用 spring boot 配置文件,但 NOT maven 配置文件..
  • 更新了源代码,你能给我建议天气它是否有效

标签: java maven spring-boot


【解决方案1】:

如果您使用的是 Spring Boot,Maven 过滤的语法会略有不同。您应该使用@propery@ 而不是${property}。所以你应该改变你的 application.properties 如下:

db.connectionURL=@db.connectionURL@
db.username=@db.username@
db.password=@db.password@

This blog 提供更多详细信息

【讨论】:

  • 到目前为止,我探索了如果我使用上面的博客在 pom.xml 中定义属性,例如 username ,稍后再进行有人更改了用户名,所以我必须再次更新 pom.xml 中的用户名并再次构建它。如果我使用 settings.xml 文件,则无需更改任何 pom 文件。
  • 你是对的,如果你在settings.xml中设置了属性,你就不用改pom.xml了。但是,我的建议只是修改您的 application.properties 以遵循 Spring Boot 语法。我认为这就是你所要做的。
猜你喜欢
  • 2015-11-22
  • 1970-01-01
  • 2014-09-07
  • 1970-01-01
  • 2012-08-06
  • 2015-02-18
  • 2018-05-18
  • 2015-11-19
  • 1970-01-01
相关资源
最近更新 更多