我在tomcat上下文文件中定义了一个参数
<Parameter name="filePath" value="C:/test/test.properties" override="false"/>
我想使用contextParameter bean在Spring的Configuration类中读取此参数,但是我得到的是空值。请在本课程中建议如何读取此值。当我在其他任何班级都做同样的事情时,我得到的是期望值。我的代码如下:
@Configuration
@EnableWebMvc
public class WebAppConfig {
//Using value annotation and contextParameter bean for reading value
@Value("#{contextParameters.filePath}")
private String filePath;
// Want to use that context Parameter to load propertiy file
@Bean
public PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurerBean = new PropertySourcesPlaceholderConfigurer();
propertySourcesPlaceholderConfigurerBean.setLocations(new Resource[]{new FileSystemResource(filePath)});
return propertySourcesPlaceholderConfigurerBean;
}
该属性应该通过Environment可用,这意味着您应该能够直接将其引用为:
@Value("${filePath}")
private String filePath;
或通过自动装配的环境bean获得它,例如:
@Bean
public String filePath(Environment environment) {
return environment.getProperty("filePath");
}
@Value("#{filePath}")
private String filePath;
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句