【问题标题】:Spring Boot can't find classpath property from dependency moduleSpring Boot 无法从依赖模块中找到类路径属性
【发布时间】:2017-02-07 12:57:48
【问题描述】:

我目前有一个依赖于 Java 模块的 Spring Boot 应用程序。这个 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();
}

【问题讨论】:

  • setClassPathProperty 中有 ger 属性
  • 你确定src/main/resources/myproject-context.xml已经加载了吗?
  • 仅包含依赖项并创建该模块的类的实例将不会加载 xml 文件。您将不得不告诉 spring boot 加载 xml 文件。
  • @Gatusko 你的意思是获取属性?因为我确实有一个 getClassPathProperty() 变体

标签: java spring maven spring-boot


【解决方案1】:

尝试让整个属性对象注入

@Autowired    
Properties myClassPathProperties;

如果这有效,您就知道您已正确加载 myproject-context.xml。 您还可以使用调试器查看名称为“property.x”的属性是否存在。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-05
    • 1970-01-01
    相关资源
    最近更新 更多