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 ?
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.
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).
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.
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.
The answer to your questions depends on the version of CDI
.
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.
Starting from CDI 1.1, beans.xml
is no longer mandatory. And the scanning is as follow :
bean-discovery-mode="annotated"
, makes the archive an implicit archive. In this case, the container will scan for beans with annotated scope types.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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With