There are multiple way to access a values from application.properties in Spring Boot.
Using @Value Annotation :-
We can use the @Value annotation and access the property data from application.properties file which is use by spring bean.
@Value(“${employee-service.url}”)
private String url;
Using @ConfigurationProperties Annotation :-
- It is load the configuration properties data.
- this annotation identify the property to load.
- Here we are use prefix that is declare in all properties file data.
Syntax :- @ConfigurationProperties(prefix=”employee-service”)
Using @PropertySource Annotation :-
- We are loading application.properties file data from the property source file in an Enviroment variable.
- Here we autowired the Environment variable.
Syntax :- org.springframework.core.env.Environment
application.properties file :-
server.port=8089
employee-service.url=javatechnote.com
employee-service.username=test
employee-service.key=test123
//main class
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringHibernateApplication {
public static void main(String[] args) {
SpringApplication.run(SpringHibernateApplication.class, args);
}
}
// controller class
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.SpringHibernate.service.EmployeeServiceConfiguration;
import com.SpringHibernate.service.EmployeeValueFetch;
@RestController
@RequestMapping("/employee")
public class EmployeeConfigurationController {
@Autowired
private EmployeeServiceConfiguration employeeServiceConfiguration;
@Autowired
private EmployeeValueFetch employeeValueFetch;
@Autowired
private Environment env;
// from @value annotation read application.properties data
@GetMapping("/valueFetch")
public EmployeeValueFetch fetchAll() {
return employeeValueFetch;
}
// from configuration properties read application.properties data
@GetMapping("/propData")
public EmployeeServiceConfiguration retrieveAll() {
test();
return employeeServiceConfiguration;
}
// from Environment property source read application.properties data
public void test() {
String url = env.getProperty("employee-service.url");
String userName = env.getProperty("employee-service.username");
String key = env.getProperty("employee-service.key");
System.out.println("fetch from enviroment :- ");
System.out.println("url :- " + url + " ,username:- " + userName + " ,key:- " + key);
}
}
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
//from @value read application.properties data
@Component
public class EmployeeValueFetch {
@Value("${employee-service.url}")
private String url;
@Value("${employee-service.username}")
private String username;
@Value("${employee-service.key}")
private String key;
//create setter and getter of the variable
}
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
//from ConfigurationProperties read application.properties data
@Component
@ConfigurationProperties(prefix = "employee-service")
public class EmployeeServiceConfiguration {
private String url;
private String username;
private String key;
//create setter and getter of the variable
}
Output :-
// from @value annotation
url :- localhost:8089/employee/propData
Response :-
{
“url”: “javatechnote.com”,
“username”: “test”,
“key”: “test123”
}
// from @configurationProperties annotation
Url :- localhost:8089/employee/valueFetch
Response :-
{
“url”: “javatechnote.com”,
“username”: “test”,
“key”: “test123”
}
//fetch from enviroment :-
url :- javatechnote.com ,username:- test1 ,key:- test123