I have a class like this:
@Singleton
@Startup
@Default
public class A {
private Manager manager; // Manager is an interface
@PostConstruct
public void init() {
if (some rule is true) {
manager = new ManagerA();
} else {
manager = new ManagerB();
}
}
public Manager getManager() {
return manager;
}
}
Now I have a endpoint JAX-RS like this:
@Path("mypath")
public class B {
// @Inject vs @Resource vs @EJB - my doubt
private A objA;
@POST
@Path("resource")
@Consumes("application/json")
@Produces("application/json")
public Response myMethod(String param) {
objA.getMamager().executeSomeMethod(param);
return Response.status(HttpStatus.SC_OK).build();
}
}
When I go inject the object it takes errors regardless of the annotation that I use. Some errors:
WFLYWELD0044: Error injecting resource into CDI managed bean. Can't find a resource named
Failed to start service Caused by: org.jboss.weld.exceptions.DeploymentException: WELD-001408: Unsatisfied dependencies for type A with qualifiers @Default
How to solve it?
@Inject
(which I would suggest), then the discovery of beans is dependant on whenther you havebeans.xml
and what bean discovery mode you have. If it is wrongly set up, you end up with what you saw -WELD-001408: Unsatisfied dependencies...
. In short that means no such beanA
was found with qualifiers@Default
. Try adding emptybeans.xml
into your application and also mark yourA
bean as@ApplicationScoped
(keep it@Singleton
and@Startup
as well). HAving the@Default
qualifier there is redundant, you can remove it (it is assumed by defauft). – Siliarus