Injected @EJB is null in @Service Singleton's constructor in JBoss EAP 5.x

Solution Verified - Updated -

Issue

I have a JBoss Singleton implemented with @Service annotation, which uses the @EJB annotation to inject an EJB reference. The injected EJB variable is being used in the constructor. When it deploys, it throws a null pointer exception (NullPointerException). How do I specify that this service is dependent on the EJB? I thought the @EJB handled that. See Example code:

import org.jboss.ejb3.annotation.Service;
import javax.ejb.EJB;

@Service
public class ExampleService{
    @EJB
    ExampleEJB ejb;

    public ExampleService(){
         ejb.hello(); // getting null pointer here
    }
}

Environment

  • JBoss Enterprise Application Platform (EAP)
    • 5.x

Subscriber exclusive content

A Red Hat subscription provides unlimited access to our knowledgebase, tools, and much more.

Current Customers and Partners

Log in for full access

Log In

New to Red Hat?

Learn more about Red Hat subscriptions

Using a Red Hat product through a public cloud?

How to access this content