Uncategorized

Read properties file in spring using annotation

Spring provides @PropertySource(“path of property file”) annotation and Environment class using which you can easily read the property file.

Let’s read the values below property file using this annotation and Environment class and add use those in our spring application and understand how to use as well as where to place these annotations.

details.properties


basic=basicPhone
smart=smartPhone

Phone.java


package com.codegeekslab.device;

public interface Phone {

public void openApp(int number);

}

BasicPhone.java


package com.codegeekslab.type;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

import com.codegeekslab.device.Phone;
@Component
@PropertySource("classpath:details.properties")
public class BasicPhone implements Phone {
@Autowired
Environment env;

public void openApp(int number) {

System.out.println("calling via " + env.getProperty("basic") + "... " + number);
}

}

SmartPhone.java


package com.codegeekslab.type;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

import com.codegeekslab.device.Phone;

@Component
@PropertySource("classpath:details.properties")
public class SmartPhone implements Phone {
@Autowired
Environment env;

public void openApp(int number) {
System.out.println("calling via " + env.getProperty("smart") + "... " + number);
}

}

CallingApp.java


package com.codegeekslab.app;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.codegeekslab.device.Phone;

@Component
public class CallingApp {
@Autowired
@Qualifier("basicPhone")
private Phone phone;

public void makeCall(int number) {
phone.openApp(number);
}

}

Test.java


package com.codegeekslab.test;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import com.codegeekslab.app.CallingApp;

public class Test {

public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.scan("com.codegeekslab.app","com.codegeekslab.type");
context.refresh();
CallingApp callingApp = context.getBean("callingApp", CallingApp.class);
callingApp.makeCall(99999);

}
}

Output


Apr 24, 2017 11:17:34 AM org.springframework.context.annotation.AnnotationConfigApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@378fd1ac: startup date [Mon Apr 24 11:17:34 IST 2017]; root of context hierarchy
calling via basicPhone... 99999

First step is to add this @PropertySource(“path of property file”) on top of the class into which to add the values from the property file.We also need to pass the location of the property file as @PropertySource also requires the path location of the property file.@PropertySource(“path of property file”) will load all the values from property file and will place it into memory. As i want to use the values from property file into BasicPhone and SmartPhone classes I have placed this @PropertySource(“classpath:details.properties”) annotations on top of these two classes.And as this property file is present in my java class path I have passed classpath:details.properties into this annotation.

BasicPhone.java


@Component
@PropertySource("classpath:details.properties")
public class BasicPhone implements Phone {

//more code

}

SmartPhone.java


@Component
@PropertySource("classpath:details.properties")
public class SmartPhone implements Phone {

//more code

}

Last step is to inject the Environment class into our class and then fetch the values using the getProperty(“name of the key”) method.

To do that, I have autowired the Environment class with my BasicPhone and SmartPhone classes respectively and called the getProperty method of the Environment class and also passed the key from property file details.property which are “smart” and “basic” to fetch the respective values from both the classes.

BasicPhone.java


@Autowired
Environment env;

public void openApp(int number) {

System.out.println("calling via " + env.getProperty("basic") + "... " + number);
}

SmartPhone.java


@Autowired
Environment env;

public void openApp(int number) {
System.out.println("calling via " + env.getProperty("smart") + "... " + number);
}

 

Leave a Reply

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