java - 如何将 servlet contextPath 与 PropertySource 一起使用?

标签 java spring spring-mvc

我想加载一个与 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/

相关文章:

java - 在 Ubuntu 上执行 Java 会产生乱码输出

java - 如何将 Karate 与 testrail 集成

jsp - 如何制作 fmt :formatDate work for form:input

java - 在服务器中找不到 Spring Boot 资源文件,但在 Eclipse 中可以使用

java - 我的 RestController 不返回我的对象​​的所有属性

Java、HashMaps 和使用字符串作为键 - 字符串值是否存储了两次?

java - 当 JFrame 或 JDialog 隐藏时覆盖 WindowAdapter 的正确方法是什么?

java.lang.NoSuchMethodException : org. springframework.boot.autoconfigure.http.HttpMessageConverters 问题

java - Spring data jpa + 连接2个表

Spring Batch - 当 ItemReader 没有返回数据时如何使作业失败