Spring – Injecting values from Property Files

Facebooktwitterredditpinterestlinkedinmailby feather

Spring – Injecting values from Property Files

In this section, we will discuss how to inject values from a property file using the Java-based configuration. Here, we will use two annotations @Value and @PropertySource that we have described below.

@Value: The @Value annotation is a field, method or constructor level annotation. It is used to assign default values to fields and method arguments. We can also access the Spring environment variables and system variables using the @Value annotation. It supports the Spring Expression Language (SpEL) module. The @Value annotation is available in the org.springframework.beans.factory.annotation package.

Following code shows how to use @Value annotation:

In the above code, we have defined the @Value annotation above the email field (environment variables.)

@PropertySource – The @PropertySource annotation provides a convenient mechanism to add property files to the Spring environment. It is used with the @Configuration annotation. According to Java 8, @PropertySource is repeatable annotation. It means we can have more than one PropertySource in a configuration class.  The @PropertySource annotation is available in the org.springframework.context.annotation package.

Following code shows how to use the @PropertySource annotation:

In the above code, we have defined the @PropertySource(“classpath:sport.properties”) along with the @Configuration annotation in the AppConfig class. The sport.porperties is the properties file in which we have defined the properties.

Example of injecting properties file data using @Value and @PropertySource annotations

Here, we are going to create an example using the @Value and @PropertySource annotations to inject values from a properties file.

In this example, Fortune and Coach are the interfaces that consists unimplemented methods. The GoodLuckFortune and Cricket_Coach are the classes that implement the Fortune and Coach interface, respectively, and a sport.properties is a property file that contains some properties of Cricket_Coach class. The AppConfig class is a configuration class that contains the bean information.

Following are the steps to create an example of injecting properties using the @Value and @PropertySource annotations:

Fortune.java

GoodLuckFortune.java

Coach.java

Cricket_Coach.java

In the above code, the @Value annotation instructs the Spring to find properties named “foo.email” and “foo.team” (from sport.properties file) and assigns its value to variables named email and team respectively. To access these variables, we have created getter methods for both variables.

sport.properties file

In the above code, we have defined two properties named email and team.

AppConfig.java

App.java

In the above code, we have created an object of Cricket_Coach class instead of Coach because the properties we have defined can be accessed by the Cricket_Coach object only.

OUTPUT

Spring - Injecting values from Property Files
Facebooktwitterredditpinterestlinkedinmailby feather