![](/img/trans.png)
[英]How do I force a spring container not to return a singleton instance of a bean?
[英]How do I call another Singleton bean from a Singleton bean?
我試過將另一個Singleton bean的遠程接口放入另一個。 但是,遠程對象將始終為null。 還有其他解決方法嗎?
@Singleton
public class SingletonBean1 implements SingletonBean1Remote {
@EJB
SingletonBean2Remote singletonBean2Remote;
...
public SingletonBean1() {
...
singletonBean2Remote.anyMethod(); // returns NullPointerException
...
}
}
它是一個Singleton的事實並不重要。 您必須初始化該引用以指向除null之外的其他內容。 如所寫,這正是應該發生的情況。
創建單例#1的方法應該獲得對單例#2的引用。
我知道這是一個老問題,但是依賴注入會在構造函數完成后發生。 因此,在上面的示例中,bean在構造函數中將始終為null
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.