【问题标题】:@Value annotation unable to read properties file in spring boot camel application@Value 注释无法读取 Spring Boot Camel 应用程序中的属性文件
【发布时间】:2022-11-03 00:37:37
【问题描述】:

我有一个 spring-boot 应用程序,我从队列中读取数据并使用 .bean() 将数据发送到转换类

集成.java

class Integration {

    @Value("${someURL}")
    private String someURL; //able to read someURL from property file

    from("queue")
    // some intermediate code

    .bean(new TransformationClass(), "transformationMethod")

    // other code

}

现在,在 TransformationClass 内部,我有 @Value 注释来从属性文件中读取值,但它总是返回 null。

转换类.java

@Component
class TransformationClass {

    @Value("${someURL}")
    private String someURL; //someURL return null though there is key-value associated in props file.

    public void transformationMethod(Exchange exchange) {
        // other stuff related to someURL
    }
}

注意 - 我能够从 Integration.java 类中的属性文件中读取值,但无法从 TransformationClass.java 类中读取值

我正在使用 Spring Boot 版本 - 2.7.2 和 Camel 版本 - 3.18.1 jdk - 17

我尝试使用骆驼 PropertiesComponent 阅读,但没有奏效。

【问题讨论】:

  • 谢谢,它有效!

标签: spring-boot apache-camel spring-camel


【解决方案1】:

这里的问题是,new TransformationClass() 不是“弹簧托管实例”,因此所有@Autowire/Value/Inject/...s 都有没有效果.

由于TransformationClass 是(单件,弹簧管理)@Component 并且是Integration 所需要的,因此接线这些是有意义的:

通过字段...:

class Integration {
   @Autowired
   private TransformationClass trnsObject;
   // ...

或构造函数注入:

class Integration {

   private final TransformationClass trnsObject;

   public Integration(/*im- /explicitely @Autowired*/ TransformationClass pTrnsObject) {
      trnsObject = pTrnsObject;
   }
   // ...
   // then:
       doSomethingWith(trnsObject); // has correct @Values
}

【讨论】:

    猜你喜欢
    • 2021-12-07
    • 2017-07-09
    • 2021-09-01
    • 1970-01-01
    • 2017-06-26
    • 1970-01-01
    • 1970-01-01
    • 2020-02-14
    • 2017-07-03
    相关资源
    最近更新 更多