@Value(“#{systemProperties.AppPropertyDir}”)找不到AppPropertyDir

时间:2019-07-25 18:42:56

标签: java spring spring-boot

正如标题所述,错误在下面发布。我还尝试将AppPropertyDir放入我的环境变量中,但这仅对解决其他注入问题有所帮助,而对这一问题没有帮助!

@Configuration("appPropertiesLoader")
public class AppPropertiesLoader {
 // I ommitted other unnecessary code, this is where it fails at injecting
    @Value("#{systemProperties.AppPropertyDir}")
    private String appPropertyDir;
}
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
          metadata-complete="true" id="WebApp_ID" version="2.5">

    <!-- change below params according to local setup -->
    <context-param>
        <param-name>AppPropertyDir</param-name>
        <param-value>C:\Users\properties-location</param-value>
    </context-param>

</web-app>

错误

Caused by: org.springframework.expression.spel.SpelEvaluationException: EL1008E: Property or field 'AppPropertyDir' cannot be found on object of type 'java.util.Properties' - maybe not public or not valid?

1 个答案:

答案 0 :(得分:0)

问题是这个! @Value("${systemProperties.AppPropertyDir}")必须在系统运行时运行(因此在执行其他所有操作之前)!因此,需要@postConstruct才能从测试类包中的任何位置运行。在您的postConstruct方法中,只需确保将AppPropertyDir初始化为C:\Users\properties-location

@PostConstruct 
public void init() {
   System.setProperty("AppPropertyDir", "C:\Users\properties-location");
}