@ConfigurationProperties : Spring Boot's way of handling properties
Hello coders,
We are always in quest of improving something or the other in our lifetime, this urge also fits in for technologies as well. We can see many enhancements and updates on each and every technology so as in our favourite Spring Framework.
Spring has come a long way ahead since its inceptions.. Yes, the latest buzz word is "SPRING BOOT"!! Though it has made a lot of things easier for a developer, but sometime it debatable. In a nutshell, understanding it deeply is a must.
Here, I will be sharing a tiny, may be a tiniest of a configuration annotations @ConfigurationProperties
We must have used properties files in almost all of our applications. Spring Boot provides a clean and neat way of using it in applications. Instead of writing the long properties name which is very error-prone (we must have spent lot of time debugging and at last it just a spelling mistake of property name).
So, how about creating a class that gets mapped to properties on a file? Isn't it a neat style?
Let's get into coding. We used to start with writing the properties first, but here we can start designing a properties in a Class then move to properties file.
Create a class:
package com.pack.subpack.utils.properties;
import java.util.List;
import java.util.Map;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix="jwt")
public class JwtProperties {
private String header;
@NotNull
private String secret;
private int expiration;
private Map <String,String> authorisation;
private List <String> someProperties;
//... Getters & Setters
}
This class is marked as @Component. Which makes it a candidate for @Autowiring. Also we have used @ConfigurationProperties(prefix="jwt"), which indicates that this will get mapped to all properties starts with jwt. Lets have an look at the properties file (properties.yaml)
jwt:
expiration: 604800
path: auth
secret: secret
authorization:
username: sovanm
salt: mysecr3t
some-properties:
- prop1
- prop2
Now the mapping is done, but how to use it? This must be simple. Yes you guessed it right. It is simple. Just Inject the Component and enjoy the get methods to get the values.
private JwtProperties jwtProperties;
// Constructor Injection
@Autowired
public JwtTokenUtils(JwtProperties jwtProperties) {
this.jwtProperties = jwtProperties;
System.out.println("Secret: " + this.jwtProperties.getSecret());
System.out.println("SomePropertiesList: " + this.jwtProperties.getSomeProperties());
this.jwtProperties.getAuthorization().entrySet().stream().parallel()
.forEach(e-> {
System.out.println(e.getKey() + " : " + e.getValue());
});
}
Done! we can now use the properties anywhere in this class. It helps to make our work easier as we all love to use object intelligence rather than writing the string version as
@Value("$(jwt.authorisation)")
private Map authorisations;
Features, that we can take advantages are:- We can use JSR-303 Annotations for validations. (E,g: Used above)
- Intelligence available in the properties file.
- We can load family of properties into separate classes using the appropriate prefix.
Thanks for reading and Happy Coding :)
Sovan
Comments
Post a Comment