Spring Boot - Using ${} placeholders in Property Files

[Updated: Jul 19, 2017, Created: Jul 19, 2017]

In the last example, we saw maven resource filtering in boot application. Spring also provides it's own variable substitution in property files. We just need to use ${someProp} in property file and start the application having 'someProp' in system properties or as main class (or jar) argument '--someProp=theValue'.

This feature allows us to use 'short' command line arguments.

Example

src/main/resources/application.properties

app.title=Boot ${app} @project.artifactId@

Note that, in above example, we are also using maven variables @..@ (last tutorial).

@SpringBootConfiguration
public class ExampleMain {
    @Bean
    MyBean myBean() {
        return new MyBean();
    }

    public static void main(String[] args) throws InterruptedException {
        SpringApplication bootApp = new SpringApplication(ExampleMain.class);
        bootApp.setBannerMode(Banner.Mode.OFF);
        bootApp.setLogStartupInfo(false);
        ConfigurableApplicationContext context = bootApp.run(args);
        MyBean myBean = context.getBean(MyBean.class);
        myBean.doSomething();
    }

    private static class MyBean {

        @Value("${app.title}")
        private String appTitle;

        public void doSomething() {
            System.out.printf("App title : %s%n", appTitle);
        }
    }
}
mvn spring-boot:run -Dapp=Example

Output

D:\example-projects\spring-boot\place-holders-in-properties>mvn spring-boot:run -Dapp=Example
App title : Boot Example place-holders-in-properties

Example Project

Dependencies and Technologies Used :

  • spring-boot-starter 1.5.4.RELEASE: Core starter, including auto-configuration support, logging and YAML.
    Corresponding Spring version: 4.3.9.RELEASE
  • JDK 1.8
  • Maven 3.3.9

Using Placeholders in Property Files Select All Download
  • place-holders-in-properties
    • src
      • main
        • java
          • com
            • logicbig
              • example
        • resources

See Also