简体   繁体   中英

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!!

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.

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