Reputation: 3009
How can I read the values of a properties file while using javabased configuration of a Spring 3.2 MVC application? My configuration class extends WebMvcConfigurationAdapter...
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.foo.bara" , excludeFilters = { @Filter( Configuration.class ) } )
@PropertySource( {"classpath:abc.properties", "classpath:persistence.properties" } )
public class MokaWebAppContext extends WebMvcConfigurerAdapter {
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
PropertySourcesPlaceholderConfigurer pspc = new PropertySourcesPlaceholderConfigurer();
Resource[] resources = new ClassPathResource[ ] {
new ClassPathResource( "persistence.properties" ),
new ClassPathResource( "abc.properties" )
};
pspc.setLocations( resources );
pspc.setIgnoreUnresolvablePlaceholders( true );
return pspc;
}
...
}
When trying to access this resource whith
@Value('${persistence.db.driverClass}') private String driverClassName;
${persistence.db.driverClass} is not recognized.
What do I have to do to read values from properties files in a @Configuration class? I think I cannot use an Environment instance in this place, can I?
Upvotes: 1
Views: 2582
Reputation: 3009
Do it the right way...and it works! You have to use double quotes rather than single quotes, of course:
@Value("${persistence.db.driverClass}") private String driverClassName;
rather than
@Value('${persistence.db.driverClass}') private String driverClassName;
Upvotes: 3