I can't get a very simple point on CDI!
I have these classes in my application:
public class CarrelloController extends AbstractController {
@Inject CarrelloService carrelloService;
...
}
@Stateless
public class CarrelloService implements CarrelloDataProvider {
...
}
public interface CarrelloDataProvider {
public Oggetto getSomething(String foo);
}
However, I am getting the following error, after deploying:
org.jboss.weld.exceptions.DeploymentException: WELD-001408: Unsatisfied dependencies for type CarrelloService with qualifiers @Default at injection point [BackedAnnotatedField] @Inject @Default it.footballove.web.controller.CarrelloController.carrelloService at it.footballove.web.controller.CarrelloController.carrelloService(CarrelloController.java:0)
at org.jboss.weld.bootstrap.Validator.validateInjectionPointForDeploymentProblems(Validator.java:359) at org.jboss.weld.bootstrap.Validator.validateInjectionPoint(Validator.java:281) at org.jboss.weld.bootstrap.Validator.validateGeneralBean(Validator.java:134) at org.jboss.weld.bootstrap.Validator.validateRIBean(Validator.java:155) at org.jboss.weld.bootstrap.Validator.validateBean(Validator.java:518) at org.jboss.weld.bootstrap.ConcurrentValidator$1.doWork(ConcurrentValidator.java:68) at org.jboss.weld.bootstrap.ConcurrentValidator$1.doWork(ConcurrentValidator.java:66) at org.jboss.weld.executor.IterativeWorkerTaskFactory$1.call(IterativeWorkerTaskFactory.java:60) at org.jboss.weld.executor.IterativeWorkerTaskFactory$1.call(IterativeWorkerTaskFactory.java:53) at java.util.concurrent.FutureTask.run(FutureTask.java:266) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) at java.lang.Thread.run(Thread.java:745) Exception 0 :
I get that only using an interface. Abstract class instead makes no problem!
Why?
This is the way that EJBs work with CDI. The CDI bean types of an EJB are given by the business interface of the EJB, not by the implementation class. The business interface may be explicitly declared with a @Local
annotation.
In your case, the business interface defaults to the one and only declared interface CarelloDataProvider
. So there really is no CDI bean of type CarelloService
.
Suggestion:
Rename your EJB class to CarelloServiceImpl
and factor out an interface CarelloService
containing the extra methods you need in CarelloController
.
@Stateless
public class CarelloServiceImpl implements CarelloService {
}
public interface CarelloService extends CarelloDataProvider {
}
Or just reconsider your design - usually, when you need to access an implementation method not contained in an interface, this is a symptom of mismatched abstractions.