Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jersey 2.0 :: for cdi injection, is beans.xml mandatory?

Resource class

public class UploadFileService {

    @Inject public Logger logger;

    @POST
    @Path("/upload")
    @Consumes(MediaType.MULTIPART_FORM_DATA)
    public Response uploadFile(
        @FormDataParam("file") InputStream uploadedInputStream,
        @FormDataParam("file") FormDataContentDisposition fileDetail) {
    }
}

Injecting :: Logger class

@Dependent
public final class Loggers {

    @Produces
    public static final Logger getLogger(final InjectionPoint injectionPoint) {
    if (injectionPoint == null) {
        throw new IllegalArgumentException("injectionPoint", new NullPointerException("injectionPoint"));
    }
}

Injection perfectly works on including beans.xml at

*.war\WEB-INF\classes\META-INF\beans.xml

But is it not beans.xml optional in jersey 2.0 ?

Error reported in the absence of beans.xml

org.glassfish.hk2.api.UnsatisfiedDependencyException: There was no object available for injection at SystemInjecteeImpl(requiredType=Logger,parent=UploadFileService,
qualifiers={},position=-1,optional=false,self=false,unqualified=null,1642832267)
        at org.jvnet.hk2.internal.ThreeThirtyResolver.resolve(ThreeThirtyResolver.java:74)
        at org.jvnet.hk2.internal.Utilities.justInject(Utilities.java:947)
        at org.jvnet.hk2.internal.ServiceLocatorImpl.inject(ServiceLocatorImpl.java:902)
        at org.glassfish.jersey.gf.cdi.internal.CdiComponentProvider$CdiFactory$2.getInstance(CdiComponentProvider.java:245)
        at org.glassfish.jersey.gf.cdi.internal.CdiComponentProvider$CdiFactory.provide(CdiComponentProvider.java:189)

Any clarification is helpful ?

like image 737
Vikram Alva Avatar asked Jun 08 '15 13:06

Vikram Alva


People also ask

Is Beans xml required?

For version 1.0 of CDI, beans. xml is mandatory to enable CDI bean discovery. Without beans. xml, CDI is simply not active in the corresponding archive.

What is Beans xml for?

xml or WEB-INF/beans. xml) is not there to define beans in XML, like in other popular bean containers. Rather, you use this file to enable CDI services for the current bean archive that are difficult to define consistently in Java or which you don't want to define in Java (e.g., to accomodate testing).

What is CDI bean?

But what is a CDI bean? A CDI bean is a POJO, plain old java object, that has been automatically instantiated by the CDI container, and is injected into all, and any qualifying injection points in the application. The CDI container initiates the bean discovery process during deployment.

Can beans xml be empty?

The beans. xml file can be empty, or can contain XML data that is described by either the CDI 1.0 or CDI 1.1 schemas. Note: CDI versions 1.2 and 2.0 use the version 1.1 schema.


1 Answers

The answer to your questions depends on the version of CDI.

CDI 1.0

For version 1.0 of CDI, beans.xml is mandatory to enable CDI bean discovery. Without beans.xml, CDI is simply not active in the corresponding archive.

From CDI 1.1

Starting from CDI 1.1, beans.xml is no longer mandatory. And the scanning is as follow :

  • Omitting beans.xml, or setting bean-discovery-mode="annotated", makes the archive an implicit archive. In this case, the container will scan for beans with annotated scope types.
  • Setting bean-discovery-mode="all", all classes will be scanned.

So in your case, if you want to make Jersey 2.0 work without beans.xml, assuming that CDI version is at least 1.1, you can annotate your Rest resource with scope, typically @RequestScoped :

@RequestScoped
public class UploadFileService {

    @Inject 
    private Logger logger;

    @POST
    @Path("/upload")
    @Consumes(MediaType.MULTIPART_FORM_DATA)
    public Response uploadFile(
        @FormDataParam("file") InputStream uploadedInputStream,
        @FormDataParam("file") FormDataContentDisposition fileDetail) {
    }
}

But if you use CDI 1.0, then yes you will need beans.xml.

like image 154
Rouliboy Avatar answered Oct 02 '22 21:10

Rouliboy