A few quick steps of configuring properties in spring boot, How can it help a developer?
Spring Boot is a way by which you can bootstrap or quickly create any Spring application. Spring Framework is for developing various web and enterprise applications. It has developed various projects over time and Spring Boot is one of them.
- It helps you to create Spring applications faster.
- You don’t have to deploy JAR as it comes with an embedded tomcat or jetty server.
- Spring Boot is an application configuration with an external properties file.
- It reduces development code by avoiding a lot of boilerplate code.
- It helps you create Spring applications quickly.
- No worry about version mismatches.
- It allows you to create sample projects using spring boot initializer.
- Spring Boot also takes the responsibility of managing versions of dependent libraries. Just migrate to the latest version of Spring Boot, and the corresponding compatible versions of all the frameworks being used are directly available for you.
A Quick Setup
add spring-boot-starter-parent as the parent in our pom.xml
<parent> <groupId> org.springframework.boot </groupId> <artifactId> spring-boot-starter-parent </artifactId> <version> 2.1.3.RELEASE </version> <relativePath/>; <!-- lookup parent from repository -->; </parent>
Properties
We configure properties using POJOs
@Configuration @ConfigurationProperties(prefix = "mail") public class ConfigProperties { private String hostName; private int port; private String from; // getters and setters }
@Configuration is used so that Spring creates a Spring bean in the application context.
@ConfigurationProperties works best with properties that all have the same prefix
If we don’t use @Configuration in the POJO, then we need to add @EnableConfigurationProperties(ConfigProperties.class) in the main Spring application class to bind the properties into the POJO:
@SpringBootApplication @EnableConfigurationProperties(ConfigProperties.class) public class SpringDemoApplication { public static void main(String[] args) { SpringApplication.run(SpringDemoApplication.class, args); } }
Spring will automatically bind any property defined in our property file that has the prefix mail and the same name as in the fields in the ConfigProperties class
mail.hostname=host@mail.com mail.port=900 mail.from=mailer@mail.com
Nested Properties
We can have nested properties in Lists, Maps, and Classes
public class Credentials { private String authMethod; private String username; private String password; // getters and setters }
public class ConfigProperties { private String host; private int port; private String from; private List defaultRecipients; private Map&lt;String, String&gt; additionalHeaders; private Credentials credentials; // getters and setters }
#Simple properties mail.hostname=support@mail.com mail.port=9000 mail.from=support@mail.com #List properties mail.defaultRecipients[0]=admin@mail.com mail.defaultRecipients[1]=owner@mail.com #Map Properties mail.additionalHeaders.redelivery=true mail.additionalHeaders.secure=true #Object properties mail.credentials.username=ram mail.credentials.password=password mail.credentials.authMethod=SHA1
Property Validation
@NotBlank private String hostName;
@NotBlank private String hostName;
@Length(max = 4, min = 1) private String authMethod;
@Min(1025) @Max(65536) private int port;
@Pattern(regexp = "^[a-z0-9._%+-]+@[a-z0-9.-]+\\.[a-z]{2,6}$") private String from;
Immutable @ConfigurationProperties binding
@ConfigurationProperties(prefix = "mail.credentials") @ConstructorBinding public class ImmutableCredentials { private final String authMethod; private final String username; private final String password; public ImmutableCredentials(String authMethod, String username, String password) { this.authMethod = authMethod; this.username = username; this.password = password; } public String getAuthMethod() { return authMethod; } public String getUsername() { return username; } public String getPassword() { return password; } }
How can Spring Boot help a developer?
- Integration with several frameworks
- Configuration management
- Logging
- Transaction management
- Error/Exception handling
- Monitoring and health checks
- Integrating unit testing and mocking frameworks
Conclusion
We have explored the @ConfigurationProperties annotation and some of the features it provides like data binding and Bean Validation. Spring Boot makes the life of a developer a lot easier by providing important debug and deployment tools. Spring Boot adds some intelligence with autoconfiguration. All we need to do is provide the frameworks that are needed by the application, and Spring Boot magically auto-configures them!
It reduces project startup time to a large extent and provides an easy mechanism termed as starter projects for the development of different applications.