简体   繁体   中英

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

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 {

    @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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM