如何在使用Spring 3.2 MVC应用程序的javabased配置时读取属性文件的值? 我的配置类扩展了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;
}
...
}
尝试访问此资源时
@Value('${persistence.db.driverClass}') private String driverClassName;
$ {persistence.db.driverClass}无法识别。
如何从@Configuration类中的属性文件中读取值? 我想我不能在这个地方使用环境实例,可以吗?
答案 0 :(得分:3)
以正确的方式行事......它的确有效! 当然,你必须使用双引号而不是单引号:
@Value("${persistence.db.driverClass}") private String driverClassName;
而不是
@Value('${persistence.db.driverClass}') private String driverClassName;