![](/img/trans.png)
[英]Spring boot shows 'can not be resolved' even if the dependency exists in classpath
[英]Spring Boot can't find classpath property from dependency module
我目前有一個Spring Boot應用程序,它依賴於Java模塊。 該Java模塊具有一些classpath屬性。 當我通過Spring Boot應用程序中的Java模塊@Autowired一個bean並使用@Bean批注定義該bean,然后運行Spring Boot應用程序時,它將拋出錯誤。
引發錯誤:
2017-02-07 12:16:03.188 WARN 17620 --- [on(4)-127.0.0.1] ationConfigEmbeddedWebApplicationContext :
Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'MyService': Unsatisfied dependency expressed through method 'setClassPathProperty' parameter 0; nested exception is org.springframework.beans.factory.BeanExpressionException: Expression parsing failed; nested exception is org.springframework.expression.spel.SpelEvaluationException: EL1008E: Property or field 'myClassPathProperties' cannot be found on object of type 'org.springframework.beans.factory.config.BeanExpressionContext' - maybe not public?
Java模塊中定義的屬性
src/main/resources/myproject-context.xml
<util:properties id="myClassPathProperties" location="classpath:myproject.properties" />
Java模塊中的屬性用法
@Value("#{myClassPathProperties['property.x']}")
public void setClassPathProperty(String x) {
this.x = x;
}
Spring Boot應用程序中的Bean定義
@Bean (name = "mailSubscriptionDao")
public MyService getMyService() {
return new MyServiceImpl();
}
嘗試讓整個屬性對象注入
@Autowired
Properties myClassPathProperties;
如果這可行,則說明您已正確加載了myproject-context.xml。 比起調試器,您還可以查看是否存在名稱為“ property.x”的屬性。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.