Inject Properties from Properties File using @Value Annotation

This tutorial shows you the way to inject Properties from Properties File using @Value Annotation with Java Configuration and @PropertySource.

You will also know how to use SourcesPlaceHolder and ConversionService for specific cases including: List/Arrays, special types (DateTime, Pattern), when a value that should be treated as null or a placeholder fails to resolve.

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

springproperties-structure

2. Step to do

– Create Maven project
– Add Dependencies & Plugins
– Add Properties Files
– Create a Service
– Create Configuration Class
– 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 Files

Under src/main/resources, add these properties files:

myapp.properties

myapp2.properties

myapp3.properties

4. Create a Service

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

IService.java

MyService.java

5. Create Configuration Class

Under package config, create AppConfig.java:

– Because we don’t use XML Configuration File to define Beans, so @ComponentScan is necessary.

@PropertySource(value={String Array Paths}): adds property sources to the environment.
*Note: If there is a property key that exists in 2 or more files, when injecting using @Value, the value of the key in the last file is chosen.

PropertySourcesPlaceHolderConfigurer Bean is vital Bean for using @Value(“{…}”) annotations. Remove it if not using @Value for injecting properties.
+ setLocation: set location for properties file to be loaded, not necessary if we have used @PropertySource before.
+ if we setIgnoreUnresolvablePlaceholders by true, in case that placeholder fails to resolve, a String of Value Expression will be returned instead.
In our example, because there is no property with key config.string.keyUnresolvable, the system will show:

+ setNullValue(thing-will-be-NULL): set a value that should be treated as null. This is the way to express null as a property value.
*Note: only apply to full property values, not to parts of them.

ConversionService Bean is used to inject more complex property types as List/Array, DateTime… We can use addConverter to register a Converter. Our example will convert a string to a regular expression object Pattern.

6. Create MainApplication Class

MainApp.java

7. Run Application & Enjoy Result

– Config maven build:
clean install
– Run project with mode Java Application
– Check results in Console Screen:

IV. Source Code

springproperties


Related Posts


Got Something To Say:

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

*