我找不到在球衣资源中注入对象的正确方法。
我已经定义了这个提供者:
@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
}
}
当我运行测试时,我可以看到提供者和资源被发现,但似乎提供者没有注入:
信息:扫描包中的根资源和提供程序类:example.ws.resources example.ws.providers
2013 年 10 月 11 日 13:19:34 com.sun.jersey.api.core.ScanningResourceConfig logClasses 信息:找到根资源类:类 example.ws.resources.UpsellResource
2013 年 10 月 11 日 13:19:34 com.sun.jersey.api.core.ScanningResourceConfig logClasses 信息:找到提供程序类:类 example.ws.providers.UpsellPaymentProvider
...不重要的日志...
2013-10-11 13:19:35,092 INFO example.ws.providers.UpsellPaymentProvider.(29)---> Provider 被实例化
2013 年 10 月 11 日 13:19:36 com.sun.jersey.spi.inject.Errors processErrorMessages
GRAVE:在资源和/或提供程序类中检测到以下错误和警告:
严重:缺少字段依赖项:私有 example.ws.services.UpsellPaymentService example.ws.resources.UpsellResource.upsellPaymentService
有什么想法可以解决这个问题吗?