我的问题类似于 @InterceptorBinding / CDI/ EJB 3.2 - problems with injection
但我的问题是:为什么资源/ EJB注入对EJB拦截器工作正常,但在以CDI方式执行时会中断? 特别是因为在所有其他情况下,EJB和CDI在Java EE 7中的工作方式非常相似(@Inject + @JJB在POJO中......)
这个差异是在某处记录还是错误?
设置 Glassfish 4.1(EJB 3.2),Weld 2.2.2.Final(CDI 1.1)=> Java EE 7
工作资源注入(EJB)
public class ResourceInjectedInterceptor {
@Resource(name = "java:comp/DefaultManagedExecutorService")
private ManagedExecutorService managedExecutorService;
@Resource
private ManagedExecutorService managedExecutorService2;
@EJB
private SomeEjb someEjb;
@AroundInvoke
public Object intercept(InvocationContext ctx) throws Exception {
return ctx.proceed();
}
}
@Stateless
public class InterceptedBean {
@Interceptors(AsyncTestInterceptor.class)
public void beingIntercepted() {
//...
}
}
在上面的版本中,所有三个managedExecutorService,managedExecutorService2和someEjb都被注入,拦截器也按预期工作。
资源注入损失(CDI)
@Intercept
@Interceptor
public class ResourceInjectedInterceptor {
@Resource(name = "java:comp/DefaultManagedExecutorService")
private ManagedExecutorService managedExecutorService;
@Resource
private ManagedExecutorService managedExecutorService2;
@EJB
private SomeEjb someEjb;
@AroundInvoke
public Object intercept(InvocationContext ctx) throws Exception {
return ctx.proceed();
}
}
@Inherited
@InterceptorBinding
@Retention(RUNTIME)
@Target({METHOD, TYPE})
public @interface Intercept {
}
public class InterceptedBean {
@Intercept
public void beingIntercepted() {
//...
}
}
<beans bean-discovery-mode="all"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd">
<interceptors>
<class>com.xxx.ejb.ResourceInjectedInterceptor</class>
</interceptors>
</beans>
在运行时实例化拦截器时导致此异常(请参阅下面的完整堆栈跟踪) - 没有名称的@Resource无法按预期工作:
Caused by: javax.naming.NameNotFoundException:
No object bound to name java:comp/env/com.xxx.ejb.ResourceInjectedInterceptor/managedExecutorService2
删除managedExecutorService2&#34;修复&#34;问题,但是有一些Ejb的模拟异常。删除someEjb也会导致一个缺少重要部分的工作iterceptor:)
我尝试了什么
完整的堆栈跟踪
javax.ejb.EJBException: javax.ejb.EJBException: javax.ejb.CreateException: Could not create stateless EJB
at com.sun.ejb.containers.StatelessSessionContainer._getContext(StatelessSessionContainer.java:435)
at com.sun.ejb.containers.BaseContainer.getContext(BaseContainer.java:2579)
at com.sun.ejb.containers.BaseContainer.preInvoke(BaseContainer.java:1971)
at com.sun.ejb.containers.EJBLocalObjectInvocationHandler.invoke(EJBLocalObjectInvocationHandler.java:210)
at com.sun.ejb.containers.EJBLocalObjectInvocationHandlerDelegate.invoke(EJBLocalObjectInvocationHandlerDelegate.java:88)
at com.sun.proxy.$Proxy969.computeConstructions(Unknown Source)
at com.xxx.ejb.service.__EJB31_Generated__ConstructionComputation__Intf____Bean__.computeConstructions(Unknown Source)
at com.xxx.ejb.constructionComputation.web.ConstructionComputationService$1.executeService(ConstructionComputationService.java:52)
at com.xxx.ejb.constructionComputation.web.ConstructionComputationService$1.executeService(ConstructionComputationService.java:47)
at com.xxx.ejb.ui.util.GenericService.genericServiceCall(GenericService.java:148)
at com.xxx.ejb.constructionComputation.web.ConstructionComputationService.computeConstructions(ConstructionComputationService.java:55)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at flex.messaging.services.remoting.adapters.JavaAdapter.invoke(JavaAdapter.java:418)
at flex.messaging.services.RemotingService.serviceMessage(RemotingService.java:183)
at flex.messaging.MessageBroker.routeMessageToService(MessageBroker.java:1400)
at flex.messaging.endpoints.AbstractEndpoint.serviceMessage(AbstractEndpoint.java:1005)
at flex.messaging.endpoints.amf.MessageBrokerFilter.invoke(MessageBrokerFilter.java:103)
at flex.messaging.endpoints.amf.LegacyFilter.invoke(LegacyFilter.java:158)
at flex.messaging.endpoints.amf.SessionFilter.invoke(SessionFilter.java:44)
at flex.messaging.endpoints.amf.BatchProcessFilter.invoke(BatchProcessFilter.java:67)
at flex.messaging.endpoints.amf.SerializationFilter.invoke(SerializationFilter.java:166)
at flex.messaging.endpoints.BaseHTTPEndpoint.service(BaseHTTPEndpoint.java:291)
at flex.messaging.MessageBrokerServlet.service(MessageBrokerServlet.java:353)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:318)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:415)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:282)
at com.sun.enterprise.v3.services.impl.ContainerMapper$HttpHandlerCallable.call(ContainerMapper.java:459)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:167)
at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:201)
at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:175)
at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:235)
at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:284)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:201)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:133)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:112)
at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:561)
at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:117)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:56)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:137)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:565)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:545)
at java.lang.Thread.run(Thread.java:745)
Caused by: javax.ejb.EJBException: javax.ejb.CreateException: Could not create stateless EJB
at com.sun.ejb.containers.StatelessSessionContainer$SessionContextFactory.create(StatelessSessionContainer.java:700)
at com.sun.ejb.containers.util.pool.NonBlockingPool.getObject(NonBlockingPool.java:246)
at com.sun.ejb.containers.StatelessSessionContainer._getContext(StatelessSessionContainer.java:430)
... 54 more
Caused by: javax.ejb.CreateException: Could not create stateless EJB
at com.sun.ejb.containers.StatelessSessionContainer.createStatelessEJB(StatelessSessionContainer.java:514)
at com.sun.ejb.containers.StatelessSessionContainer.access$000(StatelessSessionContainer.java:97)
at com.sun.ejb.containers.StatelessSessionContainer$SessionContextFactory.create(StatelessSessionContainer.java:698)
... 56 more
Caused by: java.lang.IllegalStateException: Exception attempting to inject Env-Prop: com.xxx.ejb.ResourceInjectedInterceptor/managedExecutorService2@Field-Injectable Resource. Class name = com.xxx.ejb.ResourceInjectedInterceptor Field name=managedExecutorService2@java.lang.String@@@ into class com.xxx.ejb.ResourceInjectedInterceptor: Lookup failed for 'java:comp/env/com.xxx.ejb.ResourceInjectedInterceptor/managedExecutorService2' in SerialContext[myEnv={java.naming.factory.initial=com.sun.enterprise.naming.impl.SerialInitContextFactory, java.naming.factory.state=com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl, java.naming.factory.url.pkgs=com.sun.enterprise.naming}
at org.glassfish.weld.services.InjectionServicesImpl.aroundInject(InjectionServicesImpl.java:175)
at org.jboss.weld.injection.InjectionContextImpl.run(InjectionContextImpl.java:46)
at org.jboss.weld.injection.producer.ResourceInjector.inject(ResourceInjector.java:65)
at org.jboss.weld.injection.producer.BasicInjectionTarget.inject(BasicInjectionTarget.java:109)
at org.jboss.weld.interceptor.reader.PlainInterceptorFactory.create(PlainInterceptorFactory.java:58)
at org.jboss.weld.interceptor.proxy.InterceptionContext.initInterceptorInstanceMap(InterceptionContext.java:87)
at org.jboss.weld.interceptor.proxy.InterceptionContext.of(InterceptionContext.java:64)
at org.jboss.weld.interceptor.proxy.InterceptionContext.forNonConstructorInterception(InterceptionContext.java:60)
at org.jboss.weld.injection.producer.InterceptorApplyingInstantiator.newInstance(InterceptorApplyingInstantiator.java:51)
at org.jboss.weld.injection.producer.BasicInjectionTarget.produce(BasicInjectionTarget.java:104)
at org.jboss.weld.injection.producer.BeanInjectionTarget.produce(BeanInjectionTarget.java:190)
at org.jboss.weld.bean.ManagedBean.create(ManagedBean.java:149)
at org.jboss.weld.context.unbound.DependentContextImpl.get(DependentContextImpl.java:69)
at org.jboss.weld.manager.BeanManagerImpl.getReference(BeanManagerImpl.java:740)
at org.jboss.weld.manager.BeanManagerImpl.getInjectableReference(BeanManagerImpl.java:831)
at org.jboss.weld.injection.FieldInjectionPoint.inject(FieldInjectionPoint.java:92)
at org.jboss.weld.util.Beans.injectBoundFields(Beans.java:365)
at org.jboss.weld.util.Beans.injectFieldsAndInitializers(Beans.java:376)
at org.jboss.weld.injection.producer.DefaultInjector$1.proceed(DefaultInjector.java:71)
at org.glassfish.weld.services.InjectionServicesImpl.aroundInject(InjectionServicesImpl.java:172)
at org.jboss.weld.injection.InjectionContextImpl.run(InjectionContextImpl.java:46)
at org.jboss.weld.injection.producer.DefaultInjector.inject(DefaultInjector.java:68)
at org.jboss.weld.injection.producer.StatelessSessionBeanInjector.inject(StatelessSessionBeanInjector.java:58)
at org.jboss.weld.injection.producer.ejb.SessionBeanInjectionTarget.inject(SessionBeanInjectionTarget.java:117)
at org.glassfish.weld.services.JCDIServiceImpl.injectEJBInstance(JCDIServiceImpl.java:257)
at com.sun.ejb.containers.BaseContainer.injectEjbInstance(BaseContainer.java:1748)
at com.sun.ejb.containers.StatelessSessionContainer.createStatelessEJB(StatelessSessionContainer.java:475)
... 58 more
Caused by: com.sun.enterprise.container.common.spi.util.InjectionException: Exception attempting to inject Env-Prop: com.xxx.ejb.ResourceInjectedInterceptor/managedExecutorService2@Field-Injectable Resource. Class name = com.xxx.ejb.ResourceInjectedInterceptor Field name=managedExecutorService2@java.lang.String@@@ into class com.xxx.ejb.ResourceInjectedInterceptor: Lookup failed for 'java:comp/env/com.xxx.ejb.ResourceInjectedInterceptor/managedExecutorService2' in SerialContext[myEnv={java.naming.factory.initial=com.sun.enterprise.naming.impl.SerialInitContextFactory, java.naming.factory.state=com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl, java.naming.factory.url.pkgs=com.sun.enterprise.naming}
at com.sun.enterprise.container.common.impl.util.InjectionManagerImpl._inject(InjectionManagerImpl.java:740)
at com.sun.enterprise.container.common.impl.util.InjectionManagerImpl.inject(InjectionManagerImpl.java:507)
at org.glassfish.weld.services.InjectionServicesImpl.aroundInject(InjectionServicesImpl.java:119)
... 84 more
Caused by: javax.naming.NamingException: Lookup failed for 'java:comp/env/com.xxx.ejb.ResourceInjectedInterceptor/managedExecutorService2' in SerialContext[myEnv={java.naming.factory.initial=com.sun.enterprise.naming.impl.SerialInitContextFactory, java.naming.factory.state=com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl, java.naming.factory.url.pkgs=com.sun.enterprise.naming} [Root exception is javax.naming.NameNotFoundException: No object bound to name java:comp/env/com.xxx.ejb.ResourceInjectedInterceptor/managedExecutorService2]
at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:491)
at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:438)
at javax.naming.InitialContext.lookup(InitialContext.java:417)
at javax.naming.InitialContext.lookup(InitialContext.java:417)
at com.sun.enterprise.container.common.impl.util.InjectionManagerImpl._inject(InjectionManagerImpl.java:636)
... 86 more
Caused by: javax.naming.NameNotFoundException: No object bound to name java:comp/env/com.xxx.ejb.ResourceInjectedInterceptor/managedExecutorService2
at com.sun.enterprise.naming.impl.GlassfishNamingManagerImpl.lookup(GlassfishNamingManagerImpl.java:741)
at com.sun.enterprise.naming.impl.GlassfishNamingManagerImpl.lookup(GlassfishNamingManagerImpl.java:715)
at com.sun.enterprise.naming.impl.JavaURLContext.lookup(JavaURLContext.java:159)
at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:471)
... 90 more