In this guide, we will discuss two ways to obtain application property configuration values inside a Spring boot application. We will focus on properties that are loaded via an application.properties or an application.yml file. But all loaded properties in your application context should also be obtainable via the discussed methods. We will discuss the @Value and the @ConfigurationProperties annotations. If you would like to find the configured values in your environment, for example via command line parameters, then please stay tuned for our next guide ๐
@Value: Getting a single property value
In order to get the value of a single property, we need to use the @Value annotation. The annotation can be used to get the value of a single property from your loaded properties. Here is an example property:
myapp.configValue=ThisIsAConfigValue
In order to obtain the value of the myapp.configValue within a Spring boot environment, we simply annotate a field with the @Value annotation and provide the property key as follows:
@Value("${myapp.configValue}") private String configValue;
Let us try it out inside a spring managed class:
@SpringBootApplication public class NullbeansPersistenceApplication { private static final Logger log = LoggerFactory.getLogger(NullbeansPersistenceApplication.class); public static void main(String[] args) { SpringApplication.run(NullbeansPersistenceApplication.class, args); } @Value("${myapp.configValue}") private String configValue; @Bean public CommandLineRunner example(){ return new CommandLineRunner() { @Override public void run(String... args) throws Exception { log.info("The configured value is {}", configValue); } }; } }
When running the application, we get the following result:
2019-03-17 08:14:05.503 INFO 9044 --- [ main] c.n.NullbeansPersistenceApplication : The configured value is ThisIsAConfigValue
@ConfigurationProperties: Configuring multiple properties of a Spring managed entity
Sometimes we would need to configure multiple properties of an entity. Take for example the following example component
public class EncryptionProperties { private boolean enabled; private String algorithm; private int strengthInBits; public EncryptionProperties() { } public boolean isEnabled() { return enabled; } ..... .. some more getters and setters... @Override public String toString() { return "EncryptionProperties{" + "enabled=" + enabled + ", algorithm='" + algorithm + '\'' + ", strengthInBits=" + strengthInBits + '}'; } }
We can configure each of the entities properties using the @Value annotation. But that would be kind of tedious. Therefore, Spring-boot provides us with the @ConfigurationProperties annotation. The annotation can be configured with a property prefix to search with within your configured properties. For example, we can write our Encryption properties as follows in our properties file:
encryption.enabled=true encryption.algorithm=AES encryption.strengthInBits=256
Notice that we used the encryption.XXX format, with “encryption” as our prefix. Here, we will use the prefix to find the required properties. Since the suffixes .enabled, .algorithm and .strengthInBits match the field names in the EncryptionProperties class, Spring boot can easily map your application properties to the component fields. Let us declare our EncryptionProperties bean.
@ConfigurationProperties(prefix = "encryption") @Bean public EncryptionProperties encryptionProperties(){ return new EncryptionProperties(); }
All that required is to add the @ConfigurationProperties annotation to the bean definition, with the prefix configured in order for spring boot to automatically set the field values. Let us fire up that bean with a commandline runner example and take a look at the results.
@SpringBootApplication public class NullbeansPersistenceApplication { private static final Logger log = LoggerFactory.getLogger(NullbeansPersistenceApplication.class); public static void main(String[] args) { SpringApplication.run(NullbeansPersistenceApplication.class, args); } @ConfigurationProperties(prefix = "encryption") @Bean public EncryptionProperties encryptionProperties(){ return new EncryptionProperties(); } @Bean public CommandLineRunner example2(EncryptionProperties enc){ return new CommandLineRunner() { @Override public void run(String... args) throws Exception { log.info("The encryption properties are {}", enc); } }; } }
Running the example above yields us the following log statement. Notice that spring boot was also able to map the values correctly, even though we used different primitive data types.
2019-03-17 08:14:05.503 INFO 9044 --- [ main] c.n.NullbeansPersistenceApplication : The encryption properties are EncryptionProperties{enabled=true, algorithm='AES', strengthInBits=256}
Thank you ๐
In this article, we discussed two methods of obtaining configuration properties from a Spring boot’s application properties. We discussed an example usage of the @Value annotation in order to obtain a single property value. We also discussed how to use the @ConfigurationProperties annotation in order to set the values of multiple fields of a bean. Thank you for reading our article and please make sure to subscribe to our twitter in order to get our latest updates, by clicking on the follow button at the bottom of the page ๐
Leave a Reply
You must be logged in to post a comment.