资源类
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) {
}
}
注入:: 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"));
}
}
注入完美地适用于包含beans.xml
*。战争\ WEB-INF \类\ META-INF \ beans.xml中
但是它不是在球衣2.0中可选的beans.xml吗?
缺少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)
任何澄清都有帮助吗?
答案 0 :(得分:4)
您的问题的答案取决于@interface NSString (SWIFT_EXTENSION(ProjectName))
@end
的版本。
对于CDI
的1.0版,CDI
是启用CDI bean发现的必需项。如果没有beans.xml
,beans.xml
在相应的存档中就不会处于活动状态。
从CDI 1.1开始,CDI
不再是强制性的。扫描如下:
beans.xml
会使归档成为隐式归档。在这种情况下,容器将扫描带有注释范围类型的bean。bean-discovery-mode="annotated"
,将扫描所有课程。所以在你的情况下,如果你想让Jersey 2.0在没有bean-discovery-mode="all"
的情况下工作,假设CDI版本至少为1.1,你可以用范围注释你的Rest资源,通常是beans.xml
:
@RequestScoped
但是如果您使用CDI 1.0,那么您需要@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) {
}
}
。
答案 1 :(得分:0)
是CDI 1.1需要beans.xml文件。
根据您的包装方式,放置它们的地方可能会有所不同。因此,如果您要进行战争,那么beans.xml应该出现在WEB-INF文件夹中。
对于这个问题,为什么这很重要..
指定位置存在beans.xml文件有助于CDI容器进行类路径扫描。
在Jersey中,beans.xml是可选的,因为它使用hk2进行依赖注入。但我个人认为CDI 1.1比hk2更强大,更强大,所以我们应该使用CDI 1.1或更好的CDI 2.0(因为它有很多改进)。
如果您感到好奇,请随时在https://github.com/NajeebArif/CDI-2.0-Example
查看CDI 2.0示例(不带运动衫)