Inject Properties from Properties File using @ConfigurationProperties Annotation

This tutorial will show you the better way to handle properties using @ConfigurationProperties Annotation.

In previous article, we have learned about using @Value annotation to inject properties from properties file. This approach has disadvantage that if we wanna change any property, we have to update all files which referenced it.

I. Technology

– Java 1.8
– Maven 3.3.9
– Spring Tool Suite – Version 3.8.1.RELEASE (It’s OK if you use Eclipse)

II. Overview
1. Project Structure

configproperties-structure1

– In the project, we use a separate configuration properties classes annotated with @ConfigurationProperties, so property values can be bound to structured objects conveniently.
With this approach, when we wanna find or change the handling properties object, we just notice a separate component class. In this example, it is class AppicationProperties.

– For configuration class of the whole application (AppConfig.java), we should enable class above by using @EnableConfigurationProperties annotation.

– To get environment properties value, we create a Service and inject AppicationProperties object and use getter methods easily.

2. Step to do

– Create Maven project
– Add Dependencies & Plugins
– Add Properties File
– Create Properties Class
– Create Configuration Class
– Create a Service
– Create MainApplication Class
– Run Application & Enjoy Result

III. Practice
1. Create Maven project

– Open Spring Tool Suite, on Menu, choose File -> New -> Maven Project.
– Check Create a simple project, choose Workspace Location and click Next.
– Fill all fields in Artifact group box, then click Finish.

2. Add Dependencies & Plugins

Open pom.xml, add:

3. Add Properties File

Under src/main/resources, add properties file:

myapp.properties

4. Create Properties Class

Under package config/properties, create ApplicationProperties.java:

prefix = “app” indicates that any property defined with the app prefix will be mapped onto ApplicationProperties bean.

ignoreUnknownFields = false helps to throw an exception when there is any property which doesn’t match any declared field in the configuration properties class.

– to make sure that our application fails fast at the beginning instead of misbehaving, we add Validation Annotation @NotNull, @NotBlank, @Max… before fields of class.

– in case of nested key-values, we can use a custom structure comfortably, just create an inner class with appropriate properties and getters/setters. In this example, to match with connection[url,name], we have Connection class with 2 members: url and name.

5. Create Configuration Class

Under package config, create AppConfig.java:

6. Create a Service

Under package service, create interface for Service and implementation of that interface.

MyService.java

MyServiceImp.java

7. Create MainApplication Class

MainApp.java

8. Run Application & Enjoy Result

– Config maven build:
clean install
– Run project with mode Spring Boot App
– Check results in Console Screen:

IV. Source Code

spring-config-properties

By JavaSampleApproach | October 5, 2016.

Last updated on June 4, 2017.


Related Posts


Got Something To Say:

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

*