Home » Java » Enterprise Java » Spring Annotations I never had the chance to use part 2: @ConfigurationProperties

About Sezin Karli

Sezin Karli
Mathematics Engineer & Computer Scientist with a passion for software development. Avid learner for new technologies. Currently working as Senior Software Engineer at Sahibinden.com.

Spring Annotations I never had the chance to use part 2: @ConfigurationProperties

Few days ago, I accidentally stumbled upon a Spring annotation from Spring Boot project while I was checking something else.

We all know how to bind property values with “@Value” to the classes and we all know that this can be quite cumbersome if there are multiple properties to bind. Spring Boot is here to help. You can use “@ConfigurationProperties” and bind multiple values quite concisely. We will give a prefix to differentiate other configs from ours. e.g. “@ConfigurationProperties(prefix = “jdbc”)”.

Any field this annotated class has is populated with property values from the property resource. For instance if it has a username parameter then property resource with “jdbc.username” key will populate this field. The most practical way of using this annotation is using it with “@Configuration”.

You can check how we create the config class.

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package com.sezinkarli.tryconfigprops;
 
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
 
import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;
 
@Configuration
@ConfigurationProperties(prefix = "jdbc")
public class JdbcConfig
{
    private String user;
    private String password;
    private String url;
    private String driver;
 
    public String getUser()
    {
        return user;
    }
 
    public void setUser(String user)
    {
        this.user = user;
    }
 
    public String getPassword()
    {
        return password;
    }
 
    public void setPassword(String password)
    {
        this.password = password;
    }
 
    public String getUrl()
    {
        return url;
    }
 
    public void setUrl(String url)
    {
        this.url = url;
    }
 
    public String getDriver()
    {
        return driver;
    }
 
    public void setDriver(String driver)
    {
        this.driver = driver;
    }
 
    public String getProperty(String key)
    {
        return propertyMap.get(key);
    }
}

And below you can check the properties we map from application properties

1
2
3
4
jdbc.user=myJdbcUser
jdbc.password=myPwd
jdbc.url=myUrl
jdbc.driver=myJdbcDriver

After that you can easily get these values by injecting the configuration class to somewhere.

1
2
3
4
5
6
7
@Service
public class YourService
{
 
    @Autowired
    private JdbcConfig jdbcConfig;
}

You can also check here for a working toy project using “@ConfigurationProperties”.

Published on Java Code Geeks with permission by Sezin Karli, partner at our JCG program. See the original article here: spring annotations i never had the chance to use part 2: @ConfigurationProperties

Opinions expressed by Java Code Geeks contributors are their own.

(0 rating, 0 votes)
You need to be a registered member to rate this.
Start the discussion Views Tweet it!
Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments