我无法找到在泽西资源中注入对象的正确方法。
我已经定义了这个提供者:
@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();
}
};
}
}
正如您在下一个代码中看到的那样,我有一个应该由UpsellPaymentProvider提供的upsellPaymentService:
@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
11-oct-2013 13:19:34 com.sun.jersey.api.core.ScanningResourceConfig logClasses 信息:找到的根资源类:类 example.ws.resources.UpsellResource
11-oct-2013 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)---&gt; 提供者是实例
11-oct-2013 13:19:36 com.sun.jersey.spi.inject.Errors processErrorMessages
GRAVE:检测到以下错误和警告 资源和/或提供者类:
严重:缺少字段依赖:私有 example.ws.services.UpsellPaymentService example.ws.resources.UpsellResource.upsellPaymentService
有什么想法解决这个问题吗?谢谢。
答案 0 :(得分:0)
解决,通过类型更改 UpsellPaymentService (InjectableProvider的第二个通用)。