开发者

How do I call another Singleton bean from a Singleton bean?

开发者 https://www.devze.com 2023-01-23 00:33 出处:网络
I\'ve tried putting the remote interface of another Singleton bean into another. However, the remote object will always be null. Is there any other way I could get around it?

I've tried putting the remote interface of another Singleton bean into another. However, the remote object will always be null. Is there any other way I could get around it?

@Singleton
public class SingletonBean1 implements SingletonBean1Remote {

 开发者_StackOverflow中文版   @EJB
    SingletonBean2Remote singletonBean2Remote;

    ...

    public SingletonBean1() {
        ...

        singletonBean2Remote.anyMethod(); // returns NullPointerException

        ...
    }

}


The fact that it's a Singleton is immaterial. You have to initialize that reference to point to something besides null. As written, that's exactly what should happen.

The method that creates singleton #1 should get a reference to singleton #2.


I know this is an old question but dependency injection occurs AFTER the constructor has completed. So in the example above the bean will always be null in the constructor

0

精彩评论

暂无评论...
验证码 换一张
取 消