简体   繁体   中英

SpringBoot : possible to have dynmamic values/variables from application.properties into resources

For a SpringBoot app :

I have resources in src/main/resources : it can be JSON, XML... doesn't matter

Some resources are dynamic, for example :

  {
     "url": "${propA.url}"
  }

This propA.url is defined in application.properties :

propA.url=http://localhost

Is it possible when building the project to finally having the real value after mvn package :

  {
     "url": "http://localhost"
  }

After compilation, it does not work.

The properties-maven-plugin can do this. In pom.xml :

        ...
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>properties-maven-plugin</artifactId>
            <version>1.1.0</version>
            <executions>
                <execution>
                    <phase>initialize</phase>
                    <goals>
                        <goal>read-project-properties</goal>
                    </goals>
                    <configuration>
                        <files>
                            <file>src/main/resources/application.properties</file>
                        </files>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        ...

Then the maven-resources-plugin will by default replace, for example, src/main/resources/app.json file if put like this:

{
    "url": "@propA.url@"
}

Executing:

mvn package && less -FX target/classes/app.json

Will give you (if using the application.properties you mention above):

{
    "url": "http://localhost"
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM