我想加载一个与 contextPath 同名的配置文件。这是我尝试过的代码:
@Configuration
@PropertySource("file:${user.home}/#{servletContext.contextPath}.properties")
public class PropertiesConfiguration {
@Bean
public static PropertySourcesPlaceholderConfigurer propertyPlaceholderConfigurer()
{
return new PropertySourcesPlaceholderConfigurer();
}
}
上述代码不起作用,因为 #{servletContext.contextPath}
未解析。
有没有简单的方法可以做到这一点?
最佳答案
@EnableWebMvc
@Configuration
@PropertySource("classpath:/my.properties")
public class MyConfig {
public @Value("#{ servletContext.getContextPath() }") String test;
}
- 试试这个
关于java - 如何将 servlet contextPath 与 PropertySource 一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31540371/