简体   繁体   English

Spring Boot无法从依赖模块中找到classpath属性

[英]Spring Boot can't find classpath property from dependency module

I currently have a Spring Boot application with a dependency to a Java module. 我目前有一个Spring Boot应用程序,它依赖于Java模块。 This Java module has some classpath properties. 该Java模块具有一些classpath属性。 When I @Autowired a bean from the Java module in my Spring Boot application and define this bean with a @Bean annotation and then run the Spring Boot application it will throw errors. 当我通过Spring Boot应用程序中的Java模块@Autowired一个bean并使用@Bean批注定义该bean,然后运行Spring Boot应用程序时,它将抛出错误。

Error thrown: 引发错误:


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?

Property defined in Java Module Java模块中定义的属性

src/main/resources/myproject-context.xml
<util:properties id="myClassPathProperties" location="classpath:myproject.properties" />

Property usage in Java Module Java模块中的属性用法

@Value("#{myClassPathProperties['property.x']}")
public void setClassPathProperty(String x) {
    this.x = x;
}

Bean definition in Spring Boot application Spring Boot应用程序中的Bean定义

@Bean (name = "mailSubscriptionDao")
public MyService getMyService() {
    return new MyServiceImpl();
}

Try to get the whole properties object injected 尝试让整个属性对象注入

@Autowired    
Properties myClassPathProperties;

If this works, you know you have loaded myproject-context.xml correctly. 如果这可行,则说明您已正确加载了myproject-context.xml。 Than you can also have a look with the debugger, whether a property with name 'property.x' exists or not. 比起调试器,您还可以查看是否存在名称为“ property.x”的属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM