[英]Jersey: Injection works in resource method, but not for a EJB attribute
我有一個自定義的InjectionProvider,可以從SecurityContext檢索User
對象並使它可用。 定制的ResourceFilter
執行HTTP基本身份驗證,創建SecurityContext並使用Principal進行填充。 基於此建議 。
當我從資源方法訪問它時,它按預期工作:
@Stateless
@Path("foo")
public class FooResource {
@GET
@ResourceFilters(value = {UserAuthenticationFilter.class})
public Response get(@Context User user) {
return Response.ok().entity(user).build();
}
}
但是,當我嘗試將對象作為EJB的屬性/字段注入時,會拋出異常, 即使我在日志中看到調用了InjectionProvider的情況也是如此:
@Stateless
@Path("foo")
public class FooResource {
@Context
private User user;
@GET
@ResourceFilters(value = {UserAuthenticationFilter.class})
public Response get() {
return Response.ok().entity(user).build();
}
}
異常很奇怪:
[#|2011-10-31T13:35:47.691+0000|WARNING|glassfish3.1.1|javax.enterprise.system.container.web.com.sun.enterprise.web|_ThreadID=18;_ThreadName=Thread-3;|StandardWrapperValve[Gateway Servlet]: PWC1406: Servlet.service() for servlet Gateway Servlet threw exception javax.ejb.CreateException: Could not create stateless EJB at com.sun.ejb.containers.StatelessSessionContainer.createStatelessEJB(StatelessSessionContainer.java:534) at com.sun.ejb.containers.StatelessSessionContainer.access$000(StatelessSessionContainer.java:95) at com.sun.ejb.containers.StatelessSessionContainer$SessionContextFactory.create(StatelessSessionContainer.java:724) at com.sun.ejb.containers.util.pool.NonBlockingPool.getObject(NonBlockingPool.java:247) at com.sun.ejb.containers.StatelessSessionContainer._getContext(StatelessSessionContainer.java:449) at com.sun.ejb.containers.BaseContainer.getContext(BaseContainer.java:2528) at com.sun.ejb.containers.BaseContainer.preInvoke(BaseContainer.java:1895) at com.sun.ejb.containers.EJBLocalObjectInvocationHandler.invoke(EJBLocalObjectInvocationHandler.java:212) at com.sun.ejb.containers.EJBLocalObjectInvocationHandlerDelegate.invoke(EJBLocalObjectInvocationHandlerDelegate.java:88) at $Proxy166.get(Unknown Source) .... Caused by: com.sun.jersey.spi.inject.Errors$ErrorMessagesException at com.sun.jersey.spi.inject.Errors.processErrorMessages(Errors.java:170) at com.sun.jersey.spi.inject.Errors.postProcess(Errors.java:136) at com.sun.jersey.spi.inject.Errors.processWithErrors(Errors.java:199) at com.sun.jersey.server.impl.application.WebApplicationImpl$ComponentProcessorFactoryImpl.get(WebApplicationImpl.java:499) at com.sun.jersey.server.impl.ejb.EJBInjectionInterceptor.get(EJBInjectionInterceptor.java:104) at com.sun.jersey.server.impl.ejb.EJBInjectionInterceptor.init(EJBInjectionInterceptor.java:71) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at com.sun.ejb.containers.interceptors.SystemInterceptorProxy.doCallback(SystemInterceptorProxy.java:133) at com.sun.ejb.containers.interceptors.SystemInterceptorProxy.init(SystemInterceptorProxy.java:120) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at com.sun.ejb.containers.interceptors.CallbackInterceptor.intercept(InterceptorManager.java:964) at com.sun.ejb.containers.interceptors.CallbackChainImpl.invokeNext(CallbackChainImpl.java:65) at com.sun.ejb.containers.interceptors.InterceptorManager.intercept(InterceptorManager.java:393) at com.sun.ejb.containers.interceptors.InterceptorManager.intercept(InterceptorManager.java:376) at com.sun.ejb.containers.StatelessSessionContainer.createStatelessEJB(StatelessSessionContainer.java:526) ... 55 more
我究竟做錯了什么?
編輯 :我只是注意到,在引發異常之前,server.log中報告了一個錯誤:
[#|2011-10-31T15:33:01.854+0000|SEVERE|glassfish3.1.1|com.sun.jersey.spi.inject.Errors|_ThreadID=18;_ThreadName=Thread-3;|The following errors and warnings have been detected with resource and/or provider classes: SEVERE: Missing dependency for field: private com.skalio.bonusapp.core.User com.skalio.bonusapp.gateway.FooResource.user|#]
這是由於示波器不匹配造成的。 您正在嘗試將請求范圍的事物注入到沒有請求范圍的Bean中。 那行不通。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.