我目前有一个Spring Boot应用程序,它依赖于Java模块。此Java模块具有一些类路径属性。当我在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();
}
答案 0 :(得分:0)
尝试注入整个属性对象
@Autowired
Properties myClassPathProperties;
如果这样可行,则表示您已正确加载myproject-context.xml。 您还可以查看调试器,是否存在名为“property.x”的属性。