2011-11-14 131 views
1

在glassfish 3.1服務器上,我有一個@Stateful會話bean被注入到另一個有狀態會話bean中。將@Stateful bean注入另一@Stateful bean

注入的有狀態會話bean提供了我的實體訪問層,它本身有它的注入了@PersistenceContext的EntityManager,它看起來像這樣。

@Stateful 
MyEAO { 
    @PersistenceContext 
    EntityManager em; 

    MyEAO() { 
     // default constructor 
    } 

    .... 
} 

該接入層注入到另一個狀態bean:

@Stateful 
public class ShopAdmin implements ShopAdminInterface { 
    @EJB MyEAO; 

    .... 
} 

這個工作! - 但是現在,只要我向MyEAO添加了另一個構造函數,將MyEAO注入第二個bean失敗,併發生異常。

奇怪的是,這兩個bean過去都是@Stateless會話bean,根本沒有任何問題。

順便說一句,我使用第二個構造函數來傳遞我的JUnit測試的實體管理器,它在glassfish容器之外運行。

短:在「無狀態時代」,一切都按預期工作!

我對EJB很新穎 - 所以我在這裏錯過了什麼?

這裏是堆棧跟蹤:

com.sun.faces.mgbean.ManagedBeanCreationException: Bei der Ressourcen-Einspeisung auf dem verwalteten Bean securityGuardBean ist ein Fehler aufgetreten. 
at com.sun.faces.mgbean.BeanBuilder.injectResources(BeanBuilder.java:211) 
at com.sun.faces.mgbean.BeanBuilder.build(BeanBuilder.java:103) 
at com.sun.faces.mgbean.BeanManager.createAndPush(BeanManager.java:409) 
at com.sun.faces.mgbean.BeanManager.create(BeanManager.java:269) 
at com.sun.faces.el.ManagedBeanELResolver.resolveBean(ManagedBeanELResolver.java:244) 
at com.sun.faces.el.ManagedBeanELResolver.getValue(ManagedBeanELResolver.java:116) 
at com.sun.faces.el.DemuxCompositeELResolver._getValue(DemuxCompositeELResolver.java:176) 
at com.sun.faces.el.DemuxCompositeELResolver.getValue(DemuxCompositeELResolver.java:203) 
at com.sun.el.parser.AstIdentifier.getValue(AstIdentifier.java:99) 
at com.sun.el.parser.AstValue.getTarget(AstValue.java:127) 
at com.sun.el.parser.AstValue.invoke(AstValue.java:217) 
at com.sun.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:297) 
at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105) 
at com.sun.faces.facelets.tag.jsf.core.DeclarativeSystemEventListener.processEvent(EventHandler.java:128) 
at javax.faces.component.UIComponent$ComponentSystemEventListenerAdapter.processEvent(UIComponent.java:2508) 
at javax.faces.event.SystemEvent.processListener(SystemEvent.java:106) 
at com.sun.faces.application.ApplicationImpl.processListeners(ApplicationImpl.java:2129) 
at com.sun.faces.application.ApplicationImpl.invokeComponentListenersFor(ApplicationImpl.java:2077) 
at com.sun.faces.application.ApplicationImpl.publishEvent(ApplicationImpl.java:286) 
at com.sun.faces.application.ApplicationImpl.publishEvent(ApplicationImpl.java:244) 
at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:108) 
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101) 
at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139) 
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:594) 
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1539) 
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281) 
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175) 
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655) 
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595) 
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:98) 
at com.sun.enterprise.web.PESessionLockingStandardPipeline.invoke(PESessionLockingStandardPipeline.java:91) 
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:162) 
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:330) 
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231) 
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:232) 
at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:828) 
at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:725) 
at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1019) 
at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:225) 
at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137) 
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104) 
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90) 
at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79) 
at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54) 
at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59) 
at com.sun.grizzly.ContextTask.run(ContextTask.java:71) 
at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532) 
at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513) 
at java.lang.Thread.run(Thread.java:680) 
Caused by: com.sun.faces.spi.InjectionProviderException: com.sun.enterprise.container.common.spi.util.InjectionException: Exception attempting to inject Local ejb-ref name=de.sdata.service.SecurityGuardBean/sgi,Local 3.x interface =de.sdata.service.SecurityGuardInterface resolved to intra-app EJB SecurityGuard in module sdataEJB.jar,ejb-link=sdataEJB.jar#SecurityGuard,lookup=,mappedName=,jndi-name=,refType=Session into class de.sdata.service.SecurityGuardBean: Lookup failed for 'java:comp/env/de.sdata.service.SecurityGuardBean/sgi' 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.faces.integration.GlassFishInjectionProvider.inject(GlassFishInjectionProvider.java:194) 
at com.sun.faces.mgbean.BeanBuilder.injectResources(BeanBuilder.java:205) 
... 48 more 
Caused by: com.sun.enterprise.container.common.spi.util.InjectionException: Exception attempting to inject Local ejb-ref name=de.sdata.service.SecurityGuardBean/sgi,Local 3.x interface =de.sdata.service.SecurityGuardInterface resolved to intra-app EJB SecurityGuard in module sdataEJB.jar,ejb-link=sdataEJB.jar#SecurityGuard,lookup=,mappedName=,jndi-name=,refType=Session into class de.sdata.service.SecurityGuardBean: Lookup failed for 'java:comp/env/de.sdata.service.SecurityGuardBean/sgi' 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:703) 
at com.sun.enterprise.container.common.impl.util.InjectionManagerImpl.inject(InjectionManagerImpl.java:470) 
at com.sun.enterprise.container.common.impl.util.InjectionManagerImpl.injectInstance(InjectionManagerImpl.java:171) 
at org.glassfish.faces.integration.GlassFishInjectionProvider.inject(GlassFishInjectionProvider.java:184) 
... 49 more 
Caused by: javax.naming.NamingException: Lookup failed for 'java:comp/env/de.sdata.service.SecurityGuardBean/sgi' 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 for java:comp/env/de.sdata.service.SecurityGuardBean/sgi [Root exception is java.lang.reflect.UndeclaredThrowableException]] 
at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:518) 
at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:455) 
at javax.naming.InitialContext.lookup(InitialContext.java:392) 
at javax.naming.InitialContext.lookup(InitialContext.java:392) 
at com.sun.enterprise.container.common.impl.util.InjectionManagerImpl._inject(InjectionManagerImpl.java:599) 
... 52 more 
Caused by: javax.naming.NameNotFoundException: No object bound for java:comp/env/de.sdata.service.SecurityGuardBean/sgi [Root exception is java.lang.reflect.UndeclaredThrowableException] 
at com.sun.enterprise.naming.impl.JavaURLContext.lookup(JavaURLContext.java:242) 
at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:498) 
... 56 more 
Caused by: java.lang.reflect.UndeclaredThrowableException 
at $Proxy273.create(Unknown Source) 
at com.sun.ejb.EJBUtils.resolveEjbRefObject(EJBUtils.java:366) 
at com.sun.ejb.EjbNamingReferenceManagerImpl.resolveEjbReference(EjbNamingReferenceManagerImpl.java:190) 
at com.sun.enterprise.container.common.impl.ComponentEnvManagerImpl$EjbReferenceProxy.create(ComponentEnvManagerImpl.java:1106) 
at com.sun.enterprise.naming.impl.GlassfishNamingManagerImpl.lookup(GlassfishNamingManagerImpl.java:776) 
at com.sun.enterprise.naming.impl.GlassfishNamingManagerImpl.lookup(GlassfishNamingManagerImpl.java:744) 
at com.sun.enterprise.naming.impl.JavaURLContext.lookup(JavaURLContext.java:172) 
... 57 more 
Caused by: javax.ejb.CreateException: ERROR creating stateful SessionBean 
at com.sun.ejb.containers.StatefulSessionContainer.createEJBLocalBusinessObjectImpl(StatefulSessionContainer.java:551) 
at com.sun.ejb.containers.BaseContainer.createEJBLocalBusinessObjectImpl(BaseContainer.java:2486) 
at com.sun.ejb.containers.EJBLocalHomeImpl.createEJBLocalBusinessObjectImpl(EJBLocalHomeImpl.java:117) 
at com.sun.ejb.containers.EJBLocalHomeInvocationHandler.invoke(EJBLocalHomeInvocationHandler.java:177) 
... 64 more 
Caused by: java.lang.InstantiationException: de.sdata.service._SecurityGuard_Serializable 
at java.lang.Class.newInstance0(Class.java:340) 
at java.lang.Class.newInstance(Class.java:308) 
at com.sun.ejb.containers.StatefulSessionContainer._constructEJBInstance(StatefulSessionContainer.java:566) 
at com.sun.ejb.containers.BaseContainer.createEjbInstanceAndContext(BaseContainer.java:1643) 
at com.sun.ejb.containers.StatefulSessionContainer.createBeanInstance(StatefulSessionContainer.java:578) 
at com.sun.ejb.containers.StatefulSessionContainer.createEJBLocalBusinessObjectImpl(StatefulSessionContainer.java:532) 
... 67 more 
+0

「失敗,一個例外。」 - 你能粘貼痕跡嗎? – stratwine

回答

0

我懷疑你沒有在你的班上一個明確的無參數的構造函數。

IMO它不是一個很好的解決方案,因爲您需要爲測試設置一些字段,因此需要在bean類中添加特殊的構造函數。有一些很好的工具可以爲你做很多技巧。 看看http://unitils.org/cookbook.html - >測試一個JPA DAO,它應該是你需要的。

最佳, 一個

+0

實際上有一個明確的默認構造函數 - 很好的猜測。第二個構造函數用於注入一個EntityManager用於我的junit-out-of-container-testing。我只是將構造函數改爲常規函數,並使用兩步設置:m = new MyBean(); M->注入(EM);一切都在重新開始。但是我仍然不明白這裏發生了什麼。是的,通過反射注入也是一種選擇。 – cssdata

+0

我知道這並不能解決你原來的問題,但你的EntityManager是包私有的。默認情況下,測試類與被測試類位於同一個包中。然後你可以使用字段訪問來設置你的EntityManager:'new MyBean()。em = yourEm' –