java CDI: @Singleton @Startup @Inject not working implementing an interface

67 Views Asked by At

On openLiberty, java 8, Using a class

@Singleton(name = "AppContext")
@Startup
public class A implement B{
...
}


@ApplicationScoped
public class C{

    @Inject
    private A someA;

}

I get a

org.jboss.weld.exceptions.DeploymentException: WELD-001408: Unsatisfied dependencies for type A with qualifiers @Default
  at injection point [BackedAnnotatedParameter] Parameter 1 of [BackedAnnotatedMethod] @Inject public someA....

I dont understand why if I dont implement any interface its working but as soon as I add implement B it is not anymore! Didnt find any documentation about that :(

Thx!

1

There are 1 best solutions below

0
On BEST ANSWER

It looks like you're declaring a singleton session EJB.

By default, a session bean class which implements no interfaces and doesn't define any views in another way has a no-interface view and so other classes can inject it directly using its class:

If the bean does not expose any other client views (local, remote, no-interface, 2.x Remote Home, 2.x Local Home, Web Service) and its implements clause is empty, the bean defines a no-interface view.

Spec 4.9.8

However, if a session bean implements an interface then unless you use annotations to explicitly define the bean's business interfaces, all the interfaces it implements are treated as business interfaces and it doesn't get a no-interface view. In this case, it can't be injected directly using its class.

[If business interfaces are not explititly designated and] If the bean class is annotated with the Local annotation, or if the bean class is annotated with neither the Local nor the Remote annotation, all implemented interfaces (excluding the interfaces listed above) are assumed to be local business interfaces of the bean.

Spec 4.9.7

You should either inject the EJB using its interface (i.e. @Inject private B someB;) or explicitly declare that the EJB has a no-interface view by adding @LocalBean to A. (In this case you may also want to use @Local to say which other implemented interfaces are business interfaces).