我正在使用一個不是執行緒安全的庫,并且每當我使用它時我都想要一個該庫物件的新實體。我在這里做了一個測驗回購:https : //github.com/lud/test-quarkus-arc
該庫帶有兩個類,SomeLibraryClass我需要使用SomeLibraryClassDependency它們,前者需要它,而后者是執行緒不安全的。
我試圖通過使用這個工廠類來完成所有這些作業:
@ApplicationScoped
public class MyAppFactory {
@Dependent
@Produces
SomeLibraryClassDependency getDep() {
return new SomeLibraryClassDependency();
}
@Dependent
@Produces
SomeLibraryClass getUsable(SomeLibraryClassDependency dep) {
return new SomeLibraryClass(dep);
}
@Inject
Instance<MyAppClass> myClass;
public MyAppClass getNewMyClass() {
return myClass.get(); // <-- this fails
}
}
這是我想編譯的一些測驗代碼。我兩次呼叫工廠吸氣劑,并驗證我的類使用了該類的不同實體SomeLibraryClassDependency。
@Test
public void testHelloEndpoint() {
var a = factory.getNewMyClass();
var b = factory.getNewMyClass();
assertNotEquals(a.getUsabeId(), b.getUsabeId());
}
這是應該通過呼叫實體化的類Instance<MyAppClass>#get:
@Dependent
public class MyAppClass {
@Inject
SomeLibraryClass usable;
public MyAppClass() {
}
public Integer getUsabeId() {
return usable.getId();
}
}
最后是庫模擬的代碼:
public class SomeLibraryClass {
private SomeLibraryClassDependency dep;
public SomeLibraryClass(SomeLibraryClassDependency dep) {
this.dep = dep;
}
public Integer getId() {
return dep.getId();
}
}
public class SomeLibraryClassDependency {
private static Integer n = 0;
private Integer id;
public SomeLibraryClassDependency() {
n = 1;
this.id = n;
}
public Integer getId() {
return id;
}
}
嘗試編譯時,出現以下錯誤,我不明白為什么
[錯誤]:構建步驟 io.quarkus.arc.deployment.ArcProcessor#validate 引發例外:javax.enterprise.inject.spi.DeploymentException:javax.enterprise.inject.UnsatisfiedResolutionException:不滿足型別 io.quarkus.arc.runtime 的依賴.BeanContainer$Instance<org.acme.getting.started.MyAppClass> 和限定符 [@Default]
- java 成員:org.acme.getting.started.MyAppFactory#myClass
- 在 CLASS bean 上宣告 [types=[org.acme.getting.started.MyAppFactory, java.lang.Object], qualifiers=[@Default, @Any], target=org.acme.getting.started.MyAppFactory]
我在想既然MyAppClass有@Dependent注釋,就應該解決。
編輯:我知道我也可以為我的類定義一個生產者,但我的最終目標是能夠@Inject 該類中的其他東西(如記錄器)并讓容器完成它的作業。
uj5u.com熱心網友回復:
錯誤訊息說Unsatisfied dependency for type io.quarkus.arc.runtime.BeanContainer$Instance<org.acme.getting.started.MyAppClass>,這表明您對該Instance類的匯入錯誤。正確的是javax.enterprise.inject.Instance.
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/407756.html
標籤:
