fgonzalez
fgonzalez

Reputation: 3887

CDI dependency injection issue

I have an EJB correctly deployed in Wildfly application server. I can inject it using @EJB annotation, however now, I would like to wrap into a cdi bean to be able to inject it using @Inject annotation. For that I have created a class ResourceProducer which inject the EJB using @EJB and then wrap it into a cdi bean.

Here is the code:

public class ResourceProducer {

@EJB
BusinessHandler businessHandler;



@Produces
@Named("myBusinessHandler")
public BusinessHandler getMyBusinessHandler() {
    return businessHandler;
}
}   

Then in the injection point I inject the bean using

@Inject 
@Named("myBusinessHandler")
private BusinessHandler handler;

However Eclipse is telling me "No bean is eligible for injection to the injection point [JSR-299 §5.2.1]". What I'm doing wrong? DO you see something I'm missing. Any help would be appreciated.

Thank you!!

Upvotes: 1

Views: 704

Answers (1)

Salih Erikci
Salih Erikci

Reputation: 5087

Try the below code for injecting EJB into a CDI bean.

ResourceProducer.java

@Named
@SessionScoped // or some other scope
public class ResourceProducer { // Your CDI Bean
    @Inject
    BusinessHandler businessHandler;
}

Your EJB
BusinessHandler.java

@Stateless
public class BusinessHandler(){ // Your EJB
    ...
}

That's all you should do to inject an EJB to a CDI bean.

Upvotes: 2

Related Questions