gpt4 book ai didi

java - 定制 Jersey 注入(inject)

转载 作者:行者123 更新时间:2023-12-01 04:17:18 25 4
gpt4 key购买 nike

我找不到在 Jersey 资源中注入(inject)对象的正确方法。

我已经定义了这个提供者:

@Provider
public class UpsellPaymentProvider implements InjectableProvider<Context, UpsellPaymentService>{

private final Log log = LogFactory.getLog(getClass());

public UpsellPaymentProvider(){
log.info("Provider is instanced");
}

@Override
public ComponentScope getScope() {
return ComponentScope.PerRequest;
}

@Override
public Injectable<UpsellPaymentService> getInjectable(ComponentContext paramComponentContext,
Context paramA, UpsellPaymentService paramC) {
log.info("Injecting UpsellPaymentService");
return new AbstractHttpContextInjectable<UpsellPaymentService>() {
@Override
public UpsellPaymentService getValue(HttpContext arg0) {
return new UpsellPaymentService();
}
};
}

}

正如您在下一个代码中看到的,我有一个 UpsellPaymentService,它应该由 UpsellPaymentProvider 提供:

@Path("/rest/payment/upsell")
public class UpsellResource {

private final Log log = LogFactory.getLog(getClass());
@Context // Inject here
private UpsellPaymentService upsellPaymentService;

@POST
@Path("/first")
@Produces(MediaType.TEXT_PLAIN)
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public Response first(@InjectParam UpsellDto dto, Form allParams){
// code here
}

}

当我运行测试时,我可以看到提供程序和资源已被发现,但提供程序似乎没有注入(inject):

INFO: Scanning for root resource and provider classes in the packages:example.ws.resources example.ws.providers

11-oct-2013 13:19:34 com.sun.jersey.api.core.ScanningResourceConfig logClassesINFO: Root resource classes found: classexample.ws.resources.UpsellResource

11-oct-2013 13:19:34 com.sun.jersey.api.core.ScanningResourceConfig logClassesINFO: Provider classes found: classexample.ws.providers.UpsellPaymentProvider

...Not important logs...

2013-10-11 13:19:35,092 INFO example.ws.providers.UpsellPaymentProvider.(29)--->Provider is instanced

11-oct-2013 13:19:36 com.sun.jersey.spi.inject.Errors processErrorMessages

GRAVE: The following errors and warnings have been detected withresource and/or provider classes:

SEVERE: Missing dependency for field: privateexample.ws.services.UpsellPaymentServiceexample.ws.resources.UpsellResource.upsellPaymentService

有办法解决这个问题吗?

最佳答案

已解决,按类型更改UpsellPaymentService(InjectableProvider 的第二个泛型)。

关于java - 定制 Jersey 注入(inject),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19317309/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com