0
我有一個注入EJB入口的@TransactionScoped bean。當我通過例如一個JAX-RS端點,我可以看到創建了兩個bean的實例。我想知道爲什麼會創建第一個bean實例。它發生在蝠pay和wild。上。創建了兩個CDI @TransactionScoped Bean實例。爲什麼?
@TransactionScoped
public class TransactionBean implements Serializable {
private String data;
private static AtomicInteger counter = new AtomicInteger();
public TransactionBean() {
this.data = "TransactionBean #" + counter.getAndIncrement() + " created.";
System.out.println("Created " + this.data);
}
public String toString() {
return data;
}
}
我看到輸出
- TransactionBean#0創建。
- TransactionBean#1已創建。
#1實例是實際交易中使用的一個beeing。爲什麼創建這個第一個實例?它是這些特定應用程序服務器中CDI的實現細節還是故意發生的?這僅僅是出於好奇...
乾杯, 丹尼爾
很可能您正在見證創建代理。有關更多詳細信息,請參閱[this](http://stackoverflow.com/questions/18911646/constructor-of-cdi-managed-bean-is-invoked-twice-while-opening-the-page/18911939#18911939)。 –
您最好監視'@ PostConstruct'方法而不是構造函數。 –
謝謝!該鏈接非常有幫助。所以,實質上這是由於實現細節如何創建代理。 此外,提示與@PostConstruct是有道理的。 – 38leinad