简体   繁体   中英

Java: Inject Singleton bean to test

I have a Singleton bean, and I have a Stateful bean too. I need to use the Singleton bean in the testclass of my Stateful bean. If I simply just inject it:

@Inject
private MySingletonBean mySingletonBean;

I got null instead of the Stateful bean. Any ideas, why is it, how to fix it?

Could be many reasons. It can happen if the stateful bean was manually instantiated like

new StatefulBean()

If MySingletonBean is in a jar that doesn't have a META-INF/beans.xml file, it will also not be used for injection. Is this a JEE application? Do you get in messages in the log when the application is started? There might be hints about what's going wrong ifit's not one of the reasons mentioned above.

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