我有一個 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 閱讀,但沒有奏效。
uj5u.com熱心網友回復:
這里的問題是,這new TransformationClass()不是“彈簧托管實體”,因此所有@Autowire/Value/Inject/...s 都沒有效果。
由于TransformationClassis (singleton, spring-managed) @Componentand is required by 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
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/527352.html
標籤:弹簧靴阿帕奇骆驼春骆驼
