Spring - Injecting values from Property Files

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:

@Value("${foo.email}")
 private String email; 

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:

@Configuration
 @PropertySource("classpath:sport.properties")
 public class AppConfig { 
    …. 
 } 

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

public interface Fortune {
public String getDailyFortune();
} 

GoodLuckFortune.java

import org.springframework.stereotype.Component;
@Component
public class GoodLuckFortune implements Fortune{
public String getDailyFortune() {
return " Using @Bean : Goodluck For today..!!" ;
}
} 

Coach.java

public interface Coach {
public String getDailyTraining();
public String getFortuneService();
} 

Cricket_Coach.java

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class Cricket_Coach implements Coach {
private Fortune fortuneService;
@Value("${foo.email}")
private  String email;
@Value("${foo.team}")
private String team;
public String getEmail() { 
return email;
}
public String getTeam() {
return team;
}
public Cricket_Coach(Fortune fortuneService) { 
this.fortuneService = fortuneService;
}
public String getDailyTraining() {
return " Using @Bean : Daily 5 hours of running..!" ;
}
public String getFortuneService() {
return fortuneService.getDailyFortune() ;
}
} 

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

[email protected]
foo.team= Spring Coders 

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

AppConfig.java

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
@Configuration
@PropertySource("classpath:sport.properties")
public class AppConfig {
// defining bean for GoodLuckFortune class
@Bean
 public Fortune goodLuckFortune() {
return new GoodLuckFortune();
}
//defining bean for Cricket_Coach class
@Bean
public Coach cricket_Coach() {
 return new Cricket_Coach(goodLuckFortune());
}
} 

App.java

import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class App 
{
public static void main( String[] args )
{
AnnotationConfigApplicationContext appcont = new AnnotationConfigApplicationContext(AppConfig.class);
Cricket_Coach newCoach = appcont.getBean("cricket_Coach", Cricket_Coach.class);
System.out.println(newCoach.getDailyTraining());
System.out.println(newCoach.getFortuneService());
System.out.println(" Email :  " + newCoach.getEmail());
System.out.println(" Team :  " + newCoach.getTeam());
appcont.close();
}
} 

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



ADVERTISEMENT
ADVERTISEMENT