30 Jan 2018

Creating and working with custom properties in Spring Boot

java 0 Comment

In spring Boot applications we can define application.properites file. And in this file we can specify any properties and after that we can access these properties throughout the application.

Spring Boot provided some commonly used application properties. You can look for them here. Spring Common application properties

If we want to define our own custom properties to use in our application, we can define them.

Defining custom property by Java configuration:

  • To define custom properties we need to create one POJO with the annotation @ConfigurationProperties.
  • We can annotate our POJO with the @Component annotation then we no need to create @Bean for our property class explicitly in configuration classes.
  • We can define some prefix for all our properties like @ConfigurationProperties(“machine.config”)

Example property POJO class:

  • To enable the processing of our properties, we need to annotate any of our @Configuration class with @EnableConfigurationProperties(MachineProperties.class) like as following

Example configuration class:

Using of custom property:

Like as any other Spring common properties we can use our custom properties.

  • Specify property name and its value in application.properties file.
  • Access property value with the help of @Value annotation.

Example usage:


Note: SpringApplication will load properties from application.properties file by default. If you don’t like application.properties as the configuration file name you can switch to another by specifying a spring.config.name environment property like as following.


Thank You 🙂