Spring Boot Value & ConfigurationProperties Annotation examples
This tutorial shows how to use @Value & @ConfigurationProperties annotations to read configuration from properties file.
@Value vs @ConfigurationProperties Annotation
@Value annotation is provide by Spring Bean factory & is used at the field or method/constructor parameter level that indicates a default value expression for the annotated element. It supports SpEL (Spring Expression Language) expressions for setting the value.
@ConfigurationProperties annotation is provided by SpringBoot & is used for externalized configuration. You can add this to a class definition or a @Bean method in a @Configuration class if you want to bind and validate some external Properties (e.g. from a .properties file or .yml file). Contrary to @Value, SpEL expressions are not evaluated since property values are externalized.
Create pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.6.RELEASE</version> <relativePath/> </parent> <groupId>com.example</groupId> <artifactId>demo</artifactId> <version>0.0.1-SNAPSHOT</version> <name>demo</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> </dependencies> <!-- To create an executable jar --> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
1) Read key value from properties file using @Value annotation
Create application.properties file with key values and keep it under src/main/resources
db.username=scott db.password=tiger listOfTables=Students,Subjects,Teachers valuesMap={John: '1', Mark: '2', David: '3'}
Create AppConfig & ConfigurationApplication classes. AppConfig class will read the application.properties with the help of @PropertySource and uses @Value annotation to store the value into attributes. It reads simple key value pair using username, password attributes, reads list of tables into a string array, and map into valuesMap attribute.
package com.example.demo; import java.util.Map; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; @Configuration @PropertySource("classpath:application.properties") public class AppConfig { @Value("${db.username}") private String username; @Value("${db.password}") private String password; // reads data into a string array @Value("${listOfTables}") private String[] tableValues; // reads data into a map @Value("#{${valuesMap}}") private Map<String, Integer> valuesMap; // reads single value from key value pair @Value("#{${valuesMap}['John']}") private Integer value1; public String getUsername() { return username; } public String getPassword() { return password; } public String[] getTableValues() { return tableValues; } public Map<String, Integer> getValuesMap() { return valuesMap; } public Integer getValue1() { return value1; } }
package com.example.demo; import java.util.Arrays; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.ApplicationRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.Bean; @SpringBootApplication public class ConfigurationApplication { @Autowired private AppConfig appConfig; public static void main(String[] args) { SpringApplication.run(ConfigurationApplication.class, args); } @Bean public ApplicationRunner printDataBaseConfig() { return args -> { System.out.println(appConfig.getUsername()); System.out.println(appConfig.getPassword()); System.out.println(Arrays.toString( appConfig.getTableValues())); System.out.println(appConfig.getValuesMap()); System.out.println(appConfig.getValue1()); }; } }
scott tiger [Students, Subjects, Teachers] {John=1, Mark=2, David=3} 1
2) Read key value from properties file using @ConfigurationProperties annotation
Create application.properties file with key values and keep it under src/main/resources
app.username=scott app.password=tiger app.tableValues[0]=Students app.tableValues[1]=Subjects app.tableValues[2]=Teachers app.valuesMap.John=1 app.valuesMap.Mark=2 app.valuesMap.David=3 app.value1=1
Create AppProperties & ConfigurationApplication classes. AppProperties class will read the application.properties with the help of @ConfigurationProperties annotation to store the value into attributes. It reads simple key value pair using username, password attributes, reads list of tables into a string array, and map into valuesMap attribute.
import java.util.Map; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; @Component @ConfigurationProperties("app") public class AppProperties { private String username; private String password; private String[] tableValues; private Map<String, Integer> valuesMap; private Integer value1; public String getUsername() { return username; } public String getPassword() { return password; } public String[] getTableValues() { return tableValues; } public Map<String, Integer> getValuesMap() { return valuesMap; } public Integer getValue1() { return value1; } public void setUsername(String username) { this.username = username; } public void setPassword(String password) { this.password = password; } public void setTableValues(String[] tableValues) { this.tableValues = tableValues; } public void setValuesMap(Map<String, Integer> valuesMap) { this.valuesMap = valuesMap; } public void setValue1(Integer value1) { this.value1 = value1; } }
import java.util.Arrays; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.ApplicationRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.Bean; @SpringBootApplication public class ConfigurationApplication { @Autowired private AppProperties appProperties; public static void main(String[] args) { SpringApplication.run(ConfigurationApplication.class, args); } @Bean public ApplicationRunner printDataBaseConfig() { return args -> { System.out.println(appProperties); System.out.println(appProperties.getUsername()); System.out.println(appProperties.getPassword()); System.out.println(Arrays.toString( appProperties.getTableValues())); System.out.println(appProperties.getValuesMap()); System.out.println(appProperties.getValue1()); }; } }
scott tiger [Students, Subjects, Teachers] {John=1, Mark=2, David=3} 1