Search code examples
jakarta-eeejbcdi

Unsatisfied dependency for container managed Stateless bean after adding interface implementation


I had some processor classes which did not implement any interface:

public class Processor1 {
}

@Stateles
public class Processor2 {
}

public class Processor3 {
}

One of them is a container managed Stateless bean.

Service class has all the processors injected:

public class MyService {
    @Inject private Processor1 p1;
    @Inject private Processor2 p2;
    @Inject private Processor3 p3;
}

Than I had a requirement that all processors should implement an interfase ProcessorInterfase;

After I changed the code, deployment failed with error:

Caused by: org.jboss.weld.exceptions.DeploymentException: WELD-001408: Unsatisfied dependencies for type Processor2 with qualifiers @Default

Ok. I created a new Qualifier @Process2Bean and added it to Processor2 declaration:

@Stateles
@Process2Bean
public class Processor2 {
}

and to injection point:

public class MyService {
    @Inject private Processor1 p1;
    @Inject @Process2Bean private Processor2 p2;
    @Inject private Processor3 p3;
}

Now I have the following deployment error:

Caused by: org.jboss.weld.exceptions.DeploymentException: WELD-001408: Unsatisfied dependencies for type Processor2 with qualifiers @Process2Bean
at injection point [UnbackedAnnotatedField] @Inject @Process2Bean private a.b.c.MyService.p2

Did I do something wrong? Thanks.


Solution

  • This looks like a twist on a problem I answered in this question.

    In short, when you are injecting EJB beans, you need to inject them based on their client-visible parts - interfaces. Unless, of course, you have no-interface view as was your original scenario. CDI spec covers this if you want to have a read.

    You are going to have to inject Processor2 via ProcessorInterfase. But there are three implementations, so that would lead to ambiguous dependency, hence you will also need the qualifier. Final solution can then look like this:

    @Inject
    @Process2Bean
    ProcessorInterfase processor2;