简体   繁体   English

jersey 2.0 ::用于cdi注入,是否必须使用beans.xml?

[英]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 注入:: Logger类

@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 注入可以完美地在以下位置包含beans.xml

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

But is it not beans.xml optional in jersey 2.0 ? 但是,球衣2.0中的beans.xml是否不是可选的?

Error reported in the absence of beans.xml 没有bean.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 ? 任何澄清是有帮助的吗?

The answer to your questions depends on the version of CDI . 您问题的答案取决于CDI的版本。

CDI 1.0 CDI 1.0

For version 1.0 of CDI , beans.xml is mandatory to enable CDI bean discovery. 对于CDI 1.0版,必须启用beans.xml才能启用CDI bean发现。 Without beans.xml , CDI is simply not active in the corresponding archive. 如果没有beans.xml ,那么CDI根本不会在相应的归档文件中处于活动状态。

From CDI 1.1 从CDI 1.1开始

Starting from CDI 1.1, beans.xml is no longer mandatory. 从CDI 1.1开始, beans.xml不再是必需的。 And the scanning is as follow : 扫描如下:

  • Omitting beans.xml, or setting bean-discovery-mode="annotated" , makes the archive an implicit archive. 省略beans.xml或设置bean-discovery-mode="annotated"会使归档成为隐式归档。 In this case, the container will scan for beans with annotated scope types. 在这种情况下,容器将扫描具有注释范围类型的bean。
  • Setting bean-discovery-mode="all" , all classes will be scanned. 设置bean-discovery-mode="all" ,将扫描所有类。

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 : 因此,在您的情况下,如果要使Jersey 2.0在没有beans.xml的情况下工作,假设CDI版本至少为1.1,则可以使用范围(通常为@RequestScoped注释Rest资源:

@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 . 但是,如果您使用CDI 1.0,那么您将需要beans.xml

Yes beans.xml file is required by CDI 1.1. 是,CDI 1.1需要bean.xml文件。

The places where to put them may vary depending upon your packaging style. 放置它们的位置可能会因您的包装风格而异。 So if you are going to have war then beans.xml should be present in the WEB-INF folder. 因此,如果您要打仗,则应该在WEB-INF文件夹中包含beans.xml。

For the question why is it important.. 对于这个问题,为什么重要?

The presence of beans.xml file at the designated location facilitates the CDI container to do the classpath scanning. bean.xml文件在指定位置的存在有助于CDI容器进行类路径扫描。

The beans.xml is optional in Jersey as it uses hk2 for dependency injection. beans.xml在Jersey中是可选的,因为它使用hk2进行依赖项注入。 But i personally think CDI 1.1 is more robust and powerful than hk2 so we should use CDI 1.1 or better CDI 2.0 (as there are hell lot of improvements in it). 但是我个人认为CDI 1.1比hk2更强大和强大,因此我们应该使用CDI 1.1或更好的CDI 2.0(因为其中有很多改进之处)。

And if you are curious, please feel free to look into CDI 2.0 example(without jersey) at https://github.com/NajeebArif/CDI-2.0-Example 并且,如果您好奇,请随时在https://github.com/NajeebArif/CDI-2.0-Example中查看CDI 2.0示例(不含球衣)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM