Automatic Property Expansion with Spring Boot

Spread the love

1. Overview

In this article, we’ll explore the property expansion mechanism provided by Spring through Maven Build method.

2. Configuration

In resource files, we can use ${…} placeholder as variables which are replaced by system or maven properties during build time.

By default, maven resource filtering is not enabled. If we extend our Spring Boot project from spring-boot-starter-parent the resource filtering is enabled by default. In that case, @..@ delimiter is used instead of ${}, that is to avoid conflict with the spring-style placeholder ${}.

If we don’t extend spring-boot-starter-parent  but instead, importspring-boot-dependencies then we have to enable maven resource filtering ourselves.

For Maven projects using the spring-boot-starter-parent, there is no need for extra configurations to make use of property expansions:

<parent>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-parent</artifactId>
	<version>2.1.7.RELEASE</version>
	<relativePath/> <!-- lookup parent from repository -->
</parent>

Now we can expand our project’s properties using @…@ placeholders. Here is an example of how we may save the project’s version taken from Maven, into our properties:

project-name=@project.name@
app-title=@app.title@
spring-version=@spring.version@

We can only use these expansions within configuration files matching these patterns:

  • **/application*.yml
  • **/application*.yaml
  • **/application*.properties

In the absence of the spring-boot-starter-parent parent, we’ll need to configure this filtering and expansion manually. We’ll need to include resources element into the <build> section of our pom.xml file:

<resources>
    <resource>
        <directory>${basedir}/src/main/resources</directory>
        <filtering>true</filtering>
        <includes>
            <include>**/application*.yml</include>
            <include>**/application*.yaml</include>
            <include>**/application*.properties</include>
         </includes>
    </resource>
</resources> 

And in the <plugins>:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-resources-plugin</artifactId>
    <version>2.7</version>
    <configuration>
        <delimiters>
            <delimiter>@</delimiter>
        </delimiters>
        <useDefaultDelimiters>false</useDefaultDelimiters>
    </configuration>
</plugin>

Now, the mail class will be configured like this

@SpringBootApplication
public class PropertyExpansionSpringApplication {

    @Bean
    CustomBean customBean() {
        return new CustomBean();
    }

    public static void main(String[] args) throws InterruptedException {
        SpringApplication bootApp = new SpringApplication(PropertyExpansionSpringApplication.class);
        bootApp.setBannerMode(Banner.Mode.OFF);
        ConfigurableApplicationContext context = bootApp.run(args);
        CustomBean customBean = context.getBean(CustomBean.class);
        customBean.doSomething();
    }

    private static class CustomBean {

        @Value("${project-name}")
        private String projectName;

        @Value("${spring-version}")
        private String springVersion;

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

        public void doSomething() {
            System.out.printf("Project name: %s%n"
                            + "Spring version: %s%n"
                            + "App title: %s%n",
                    projectName, springVersion, appTitle);
        }
    }

}

Output :

Project name: Spring-Boot-Tutorial
Spring version: 5.1.9.RELEASE
App title: Application Title

3. Conclusion

In this quick tutorial, we saw how to automatically expand Spring Boot properties using Maven

Github Repository

As always, Please find the code in Github: Click Here

Leave a Reply