g0dzax
g0dzax

Reputation: 131

NPE on a @Inject field for a @Singleton class

I have come across a curious scenario in which any attempt of using CDI for managing a singleton has resulted in failure no matter the solutions I tried, and I am not even sure at this point if this is something feasable/that can be done, hence the reason I am posting here.

I'll try to explain to my best the current scenario and I would like to know whether my intention can be done, and if yes, by what way, and if not, is there some alternative to "upgrading" the singleton instances in that jar. If I have missed details/parts of the code, please let me know, as this is something a little bit unusual and I do not if I provided everything needed.

Background:

Intention:

Steps taken:

Things to consider:

Question:

Upvotes: 2

Views: 452

Answers (1)

fantarama
fantarama

Reputation: 880

If the Management class is in a jar dependency (provided from JBoss or packaged inside the ear) is not viewed as a CDI bean. It must be insiede an EJB/CDI module packaged at the same level as testing-ejb.

Another way is to use @Remote EJB annotation and deploy the jar as a library, not a simple dependecy.

It's not clear (for me) the purpose of this refactoring but if is to avoid to instantiate the class manually on every bean that need it you could write a simple producer:

@Singleton
public class ManagementProvider() {
    @Produces
    public Management getInstance() {
        return Management.getInstance();
    }
}

Then you can use @Inject anytime you want!

Upvotes: 1

Related Questions