Module 13: Injecting Values from Properties File - dineshmadhup/Spring GitHub Wiki
This description is baed on code module-13
In Module-12, we have seen how to inject Employee Service in Engineering Team using java code.
But in this module, we are going to modify code in module-12 to read values from external file and inject them to Engineering Team file.
Development Steps:
1. Create properties file in src directory.
File name: company.properties
[email protected]
foo.team=Softarica
2. Load properties file in spring config.
CompanyConfig.java:
@Configuration
@PropertySource("classpath:company.properties")
public class CompanyConfig {
// add support to resolve ${...} properties
@Bean
public static PropertySourcesPlaceholderConfigurer
propertySourcesPlaceholderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
. . . . . .
. . . . …
}
3. Reference values from properties file
EngineeringTeam.java:
public class EngineeringTeam implements Manager {
private EmployeeService empService;
@Value("${foo.email}") //field level injection
private String email;
@Value("${foo.team}") //field level injection
private String team;
… . . . .
.. . . . .
}