Spring boot @ConfigurationProperties

In this tutorial, we are going to see about Spring boot @ConfigurationProperties annotation.

Spring boot @ConfigurationProperties allows you to map properties values with java object easily.

Let’s first see the normal mapping first. Let’s say you have properties file below.

application.properties

You can access above property file as below.

So, you can use @Value to get property value from application.properties but it might be tedious to use @Value everywhere,Spring boot provides @ConfigurationProperties to map properties value to java Object.

Let’s understand with the help of example.

Step 1: Create a dynamic web project using maven in eclipse named “SpringBootHelloWorldExample”.

Maven dependencies:

Step 2: Change pom.xml as below:

step 3: Create a package named "org.arpit.java2blog" and class named ServerDetails

step 4: Make changes to application.properties to add below properties.

step 5: Create Spring boot main class.

As you can see, we have used @Autowired to inject ServerDetails to SpringBootExample.

Step 6: Run the application.

When you run above example, you will get below output.

. ____ _ __ _ _
/\\ / ___’_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | ‘_ | ‘_| | ‘_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
‘ |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.0.3.RELEASE)

2018-07-14 23:53:47.766 INFO 67832 — [ main] org.arpit.java2blog.SpringBootExample : Starting SpringBootExample on apples-MacBook-Air.local with PID 67832 (/Users/apple/Downloads/SpringBootConfigurationPropertiesExample/target/classes started by apple in /Users/apple/Downloads/SpringBootConfigurationPropertiesExample)
2018-07-14 23:53:47.774 INFO 67832 — [ main] org.arpit.java2blog.SpringBootExample : No active profile set, falling back to default profiles: default
2018-07-14 23:53:47.901 INFO 67832 — [ main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@429bd883: startup date [Sat Jul 14 23:53:47 IST 2018]; root of context hierarchy
Server Details{name=’amazonEc2′, url=’dummy.xyz.com’, port='[8080, 8081]’, username=’admin’, password=’admin123′}
2018-07-14 23:53:49.000 INFO 67832 — [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2018-07-14 23:53:49.024 INFO 67832 — [ main] org.arpit.java2blog.SpringBootExample : Started SpringBootExample in 2.252 seconds (JVM running for 3.406)
2018-07-14 23:53:49.028 INFO 67832 — [ Thread-2] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@429bd883: startup date [Sat Jul 14 23:53:47 IST 2018]; root of context hierarchy
2018-07-14 23:53:49.030 INFO 67832 — [ Thread-2] o.s.j.e.a.AnnotationMBeanExporter : Unregistering JMX-exposed beans on shutdown

Here is the picture which will help you understand @ConfigurationProperties better.

Relaxed binding

One of the interesting feature of Spring boot is relaxed binding.
for com.server.application.username, below are valid spring boot bindings.

  • com.server.application.user_name(with _)
  • com.server.application.user-name(with -)
  • com.server.application.USER_NAME(with uppercase)

Custom property file

You can use custom property as well instead of application.property using @PropertySource annotation.

Validation

We can use JSR-303 Validation API to validate properties.You need to add below dependency.

For example:
Let’s say you want to restrict username from 5 to 8 characters, you can use @Length annotaton as below.

If you change userName to adam

You will get below validation error.

Property:.com.server.application.username
Value: adam
Reason: length must be between 5 and 8

That’s all about Spring boot @ConfigurationProperties.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *