Read Property Value from Properties File in Spring


The Java properties file can be used to store project configurations or settings.

constant.properties

ui.defaultIconUrl=graph.facebook.com/ek.pranav/picture

Environment - Interface representing the environment in which the current application is running.

@Configuration - Configuration classes are candidates for component scanning (typically using Spring XML’s <context:component-scan/> element) and therefore may also take advantage of @Autowired/@Inject at the field and method level (but not at the constructor level).

@PropertySource - Adds property sources to the enclosing Environment.

 

The values in the properties can be accessed by injecting Environment object to the configuration class.

ConstantPropertyInjector.java

@Configuration
@PropertySource("classpath:META-INF/constant.properties")
public class ConstantPropertyInjector {

@Autowired Environment env;

   @Bean
   public ConstantImageHolder getConstantProperty(){
     ConstantImageHolder constant = new ConstantImageHolder ();
     constant.setDefaultIcon(env.getProperty("ui.defaultIconUrl"));
     return constant;
   }

}