You can load whole porperties file as Map by defining it as PropertiesFactoryBean and then using it with @Resource annotation.
@Configuration
@PropertySource(value = "file:src/main/resources/test.properties", encoding = "utf8")
public class AppConfig {
@Value("${propertyname}")
String prop;
@Resource(name = "propertyBean")
private Map<String, String> propMap;
@Bean(name = "propertyBean")
public static PropertiesFactoryBean mapper() {
PropertiesFactoryBean bean = new PropertiesFactoryBean();
bean.setLocation(new FileSystemResource("src/main/resources/test.properties"));
return bean;
}
public Map<String, String> getPropMap() {
return propMap;
}
}
And access any key present in the properties file using key like below:-
@RestController
public class Test {
@Autowired
private AppConfig appConfig;
@RequestMapping(value = "/test", method = RequestMethod.GET)
public String login(HttpServletRequest request){
return appConfig.getPropMap().get("application.name");
}
}
test.propeties:-
server.port1=8099
application.name=edge-service
propertyname=dddd
In this you don't need to write @Value annotation everytime, you can access values using
propMap. If you want to read single key use
@Value("${propertyname}")
String prop;
Even you can define map data into property file
propertyname={key1:'value1',key2:'value2'}
There can be many implementation to achieve it based on requirement.