aim aim - 3 years ago 262
Java Question

Property placeholder from Configuration

With Spring in xml context we can simple load properties like this:

<context:property-placeholder location:"classpath*"/>

Is there any chance to configure same properties inside @Configuration bean (~ from java code) without boilerplate?


Answer Source

You can use the annotation @PropertySource like this

public class AppConfig {
 Environment env;

 public TestBean testBean() {
     TestBean testBean = new TestBean();
     return testBean;


EDIT: if you are using spring boot you can use @ConfigurationProperties annotation to wire the properties file directly to bean properties, see:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download