I'm getting NullPointerException
when trying to inject a CDI bean into an EJB.
My ejb is
@Stateless(mappedName = "myEjb")
@TransactionManagement(TransactionManagementType.CONTAINER)
public class MyEjb implements MyEjbLocal, MyEjbRemote {
private static final Logger logger = Logger.getLogger(MyEjb .class);
@Inject
@Named("myService")
private MyService myService;
public MyEjb () {
}
public MyEjb (MyService myService) {
this.myService= myService;
}
@PostConstruct
public void postConstruct() {
logger.info("Injected myService" + myService+ " into this " + this);
}
And actually myService
is always null.
Here is how I initialize myService
by using CDI and a Factory
@ApplicationScoped
public class ServiceFactory {
private MyService myService;
@Inject
@Named("myDao")
private MyDao dao;
@PostConstruct
public void afterCreate() {
myService= new MyServiceImpl(dao);
}
@Produces
@ApplicationScoped
@Named("myService")
public MyService myService() {
return myService;
}
}
And MyService
is just without annotations
public class MyServiceImpl implements MyService {
private final MyDao myDao;
public MyServiceImpl(MyDao dao) {
this.myDao = dao;
}
Well, if I invoke the EJB then myService
is always null
....what I'm missing? (bean.xml
is there)
Am I missing something around the interaction ejb/cdi?