web-dev-qa-db-ja.com

CDI(溶接)にEntityManagerを挿入する方法は?

私のプロジェクトでは、永続層でJSF + JPA + CDI + WildFly8.2を使用しています。私は次のようなBasicDaoを持っています:

 public  class BasicDao<M, K> {
    private org.jboss.logging.Logger logger = org.jboss.logging.Logger
           .getLogger("BasicDao");

    @Inject
    @Primary
    protected EntityManager em;
    Class<M> mclass;

    public EntityManager getEm() {
        return em;
    }

    public void setEm(EntityManager em) {
        this.em = em;
    }

    @Transactional(value=TxType.NOT_SUPPORTED)
    public M find(K id){ 
        return em.find(mclass, id);
    }

    @Transactional(value=TxType.REQUIRED)
    public void insert(M inst){
        this.em.persist(inst);
    }

    @SuppressWarnings("unchecked")
    @Transactional(value=TxType.REQUIRED)
    public K insertWithAutoId(M inst){
        this.em.persist(inst);
        return (K) this.em.getEntityManagerFactory().getPersistenceUnitUtil().getIdentifier(inst);
    }

    @Transactional(value=TxType.REQUIRED)
    public M update(M updated){
        return this.em.merge(updated);
    }


    @Transactional(value=TxType.REQUIRED)
    public void delete(M nonUsed){
        this.em.remove(nonUsed);
    }
}

そしてこのような他のDAO:

@RequestScoped
public class UserDao extends BasicDao<User, Integer>{

    @Transactional(value = TxType.NOT_SUPPORTED)
    public User find(Integer id) {
        return em.find(User.class, id);
    }

    @Transactional(value = TxType.REQUIRED)
    public void insert(User inst) {
        this.em.persist(inst);
    }

    @Transactional(value = TxType.REQUIRED)
    public Integer insertWithAutoId(User inst) {
        this.em.persist(inst);
        return (Integer) this.em.getEntityManagerFactory()
                .getPersistenceUnitUtil().getIdentifier(inst);
    }

    @Transactional(value = TxType.REQUIRED)
    public User update(User updated) {
        return this.em.merge(updated);
    }

    @Transactional(value = TxType.REQUIRED)
    public void delete(User nonUsed) {
        this.em.remove(nonUsed);
    }

    @SuppressWarnings("unchecked")
    @Transactional(value = TxType.NOT_SUPPORTED)
    public User findByName(String loginName) {

        Query query = em.createQuery("select u from User u where u.loginName=:loginName");
        List<User> users = (List<User>) query.setParameter("loginName",
                loginName).getResultList();
        return users.isEmpty() ? null : users.get(0);
    }
}

私のManageredBeanでは、コードは次のようになっています。

 @Inject
 private UserDao userDao;

ただし、常にではなく、例外がスローされることがよくあります。とても混乱しました。このような例外スタック:

09:50:59,945 FATAL [javax.enterprise.resource.webcontainer.jsf.context] (default task-27) JSF1073:在 RENDER_RESPONSE 6 的处理过程中捕捉到 com.Sun.faces.mgbean.ManagedBeanCreationException:UIComponent-ClientId=,Message=对受管 bean resumeAudit 执行资源注入 (resource injection) 时发生错误  
09:50:59,946 FATAL [javax.enterprise.resource.webcontainer.jsf.context] (default task-27) 对受管 bean resumeAudit 执行资源注入 (resource injection) 时发生错误: com.Sun.faces.mgbean.ManagedBeanCreationException: 对受管 bean resumeAudit 执行资源注入 (resource injection) 时发生错误  
  at com.Sun.faces.mgbean.BeanBuilder.invokePostConstruct(BeanBuilder.Java:227) [jsf-impl-2.2.8-jbossorg-1.jar:]  
  at com.Sun.faces.mgbean.BeanBuilder.build(BeanBuilder.Java:103) [jsf-impl-2.2.8-jbossorg-1.jar:]  
  at com.Sun.faces.mgbean.BeanManager.createAndPush(BeanManager.Java:409) [jsf-impl-2.2.8-jbossorg-1.jar:]  
  at com.Sun.faces.mgbean.BeanManager.create(BeanManager.Java:269) [jsf-impl-2.2.8-jbossorg-1.jar:]  
  at com.Sun.faces.el.ManagedBeanELResolver.resolveBean(ManagedBeanELResolver.Java:257) [jsf-impl-2.2.8-jbossorg-1.jar:]  
  at com.Sun.faces.el.ManagedBeanELResolver.getValue(ManagedBeanELResolver.Java:117) [jsf-impl-2.2.8-jbossorg-1.jar:]  
  at com.Sun.faces.el.DemuxCompositeELResolver._getValue(DemuxCompositeELResolver.Java:176) [jsf-impl-2.2.8-jbossorg-1.jar:]  
  at com.Sun.faces.el.DemuxCompositeELResolver.getValue(DemuxCompositeELResolver.Java:203) [jsf-impl-2.2.8-jbossorg-1.jar:]  
  at com.Sun.el.parser.AstIdentifier.getValue(AstIdentifier.Java:116) [javax.el-3.0.1-b05.jar:]  
  at com.Sun.el.parser.AstValue.getBase(AstValue.Java:151) [javax.el-3.0.1-b05.jar:]  
  at com.Sun.el.parser.AstValue.getValue(AstValue.Java:200) [javax.el-3.0.1-b05.jar:]  
  at com.Sun.el.ValueExpressionImpl.getValue(ValueExpressionImpl.Java:226) [javax.el-3.0.1-b05.jar:]  
  at org.jboss.weld.el.WeldValueExpression.getValue(WeldValueExpression.Java:50) [weld-core-impl-2.2.6.Final.jar:2014-10-03 10:05]  
  at org.jboss.weld.el.WeldValueExpression.getValue(WeldValueExpression.Java:50) [weld-core-impl-2.2.6.Final.jar:2014-10-03 10:05]  
  at com.Sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.Java:109) [jsf-impl-2.2.8-jbossorg-1.jar:]  
  at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.Java:194) [jboss-jsf-api_2.2_spec-2.2.8.jar:2.2.8]  
  at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.Java:182) [jboss-jsf-api_2.2_spec-2.2.8.jar:2.2.8]  
  at javax.faces.component.UIOutput.getValue(UIOutput.Java:174) [jboss-jsf-api_2.2_spec-2.2.8.jar:2.2.8]  
  at com.Sun.faces.renderkit.html_basic.HtmlBasicInputRenderer.getValue(HtmlBasicInputRenderer.Java:205) [jsf-impl-2.2.8-jbossorg-1.jar:]  
  at com.Sun.faces.renderkit.html_basic.HtmlBasicRenderer.getCurrentValue(HtmlBasicRenderer.Java:355) [jsf-impl-2.2.8-jbossorg-1.jar:]  
  at com.Sun.faces.renderkit.html_basic.LabelRenderer.encodeBegin(LabelRenderer.Java:120) [jsf-impl-2.2.8-jbossorg-1.jar:]  
  at javax.faces.component.UIComponentBase.encodeBegin(UIComponentBase.Java:864) [jboss-jsf-api_2.2_spec-2.2.8.jar:2.2.8]  
  at com.Sun.faces.renderkit.html_basic.HtmlBasicRenderer.encodeRecursive(HtmlBasicRenderer.Java:302) [jsf-impl-2.2.8-jbossorg-1.jar:]  
  at com.Sun.faces.renderkit.html_basic.GridRenderer.renderRow(GridRenderer.Java:185) [jsf-impl-2.2.8-jbossorg-1.jar:]  
  at com.Sun.faces.renderkit.html_basic.GridRenderer.encodeChildren(GridRenderer.Java:129) [jsf-impl-2.2.8-jbossorg-1.jar:]  
  at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.Java:889) [jboss-jsf-api_2.2_spec-2.2.8.jar:2.2.8]  
  at javax.faces.component.UIComponent.encodeAll(UIComponent.Java:1856) [jboss-jsf-api_2.2_spec-2.2.8.jar:2.2.8]  
  at javax.faces.render.Renderer.encodeChildren(Renderer.Java:176) [jboss-jsf-api_2.2_spec-2.2.8.jar:2.2.8]  
  at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.Java:889) [jboss-jsf-api_2.2_spec-2.2.8.jar:2.2.8]  
  at javax.faces.component.UIComponent.encodeAll(UIComponent.Java:1856) [jboss-jsf-api_2.2_spec-2.2.8.jar:2.2.8]  
  at javax.faces.component.UIComponent.encodeAll(UIComponent.Java:1859) [jboss-jsf-api_2.2_spec-2.2.8.jar:2.2.8]  
  at javax.faces.component.UIComponent.encodeAll(UIComponent.Java:1859) [jboss-jsf-api_2.2_spec-2.2.8.jar:2.2.8]  
  at com.Sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.Java:456) [jsf-impl-2.2.8-jbossorg-1.jar:]  
  at com.Sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.Java:133) [jsf-impl-2.2.8-jbossorg-1.jar:]  
  at javax.faces.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.Java:337) [jboss-jsf-api_2.2_spec-2.2.8.jar:2.2.8]  
  at javax.faces.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.Java:337) [jboss-jsf-api_2.2_spec-2.2.8.jar:2.2.8]  
  at com.Sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.Java:120) [jsf-impl-2.2.8-jbossorg-1.jar:]  
  at com.Sun.faces.lifecycle.Phase.doPhase(Phase.Java:101) [jsf-impl-2.2.8-jbossorg-1.jar:]  
  at com.Sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.Java:219) [jsf-impl-2.2.8-jbossorg-1.jar:]  
  at javax.faces.webapp.FacesServlet.service(FacesServlet.Java:647) [jboss-jsf-api_2.2_spec-2.2.8.jar:2.2.8]  
  at io.undertow.servlet.handlers.ServletHandler.handleRequest(ServletHandler.Java:85) [undertow-servlet-1.1.0.Final.jar:1.1.0.Final]  
  at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.Java:130) [undertow-servlet-1.1.0.Final.jar:1.1.0.Final]  
  at com.wangyin.hzcrp.auth.AuthFilter.doFilter(AuthFilter.Java:109) [classes:]  
  at io.undertow.servlet.core.ManagedFilter.doFilter(ManagedFilter.Java:60) [undertow-servlet-1.1.0.Final.jar:1.1.0.Final]  
  at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.Java:132) [undertow-servlet-1.1.0.Final.jar:1.1.0.Final]  
  at io.undertow.servlet.handlers.FilterHandler.handleRequest(FilterHandler.Java:85) [undertow-servlet-1.1.0.Final.jar:1.1.0.Final]  
  at io.undertow.servlet.handlers.security.ServletSecurityRoleHandler.handleRequest(ServletSecurityRoleHandler.Java:61) [undertow-servlet-1.1.0.Final.jar:1.1.0.Final]  
  at io.undertow.servlet.handlers.ServletDispatchingHandler.handleRequest(ServletDispatchingHandler.Java:36) [undertow-servlet-1.1.0.Final.jar:1.1.0.Final]  
  at org.wildfly.extension.undertow.security.SecurityContextAssociationHandler.handleRequest(SecurityContextAssociationHandler.Java:78)  
  at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.Java:43) [undertow-core-1.1.0.Final.jar:1.1.0.Final]  
  at io.undertow.servlet.handlers.security.SSLInformationAssociationHandler.handleRequest(SSLInformationAssociationHandler.Java:131) [undertow-servlet-1.1.0.Final.jar:1.1.0.Final]  
  at io.undertow.servlet.handlers.security.ServletAuthenticationCallHandler.handleRequest(ServletAuthenticationCallHandler.Java:56) [undertow-servlet-1.1.0.Final.jar:1.1.0.Final]  
  at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.Java:43) [undertow-core-1.1.0.Final.jar:1.1.0.Final]  
  at io.undertow.security.handlers.AbstractConfidentialityHandler.handleRequest(AbstractConfidentialityHandler.Java:45) [undertow-core-1.1.0.Final.jar:1.1.0.Final]  
  at io.undertow.servlet.handlers.security.ServletConfidentialityConstraintHandler.handleRequest(ServletConfidentialityConstraintHandler.Java:63) [undertow-servlet-1.1.0.Final.jar:1.1.0.Final]  
  at io.undertow.security.handlers.AuthenticationMechanismsHandler.handleRequest(AuthenticationMechanismsHandler.Java:58) [undertow-core-1.1.0.Final.jar:1.1.0.Final]  
  at io.undertow.servlet.handlers.security.CachedAuthenticatedSessionHandler.handleRequest(CachedAuthenticatedSessionHandler.Java:70) [undertow-servlet-1.1.0.Final.jar:1.1.0.Final]  
  at io.undertow.security.handlers.SecurityInitialHandler.handleRequest(SecurityInitialHandler.Java:76) [undertow-core-1.1.0.Final.jar:1.1.0.Final]  
  at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.Java:43) [undertow-core-1.1.0.Final.jar:1.1.0.Final]  
  at org.wildfly.extension.undertow.security.jacc.JACCContextIdHandler.handleRequest(JACCContextIdHandler.Java:61)  
  at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.Java:43) [undertow-core-1.1.0.Final.jar:1.1.0.Final]  
  at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.Java:43) [undertow-core-1.1.0.Final.jar:1.1.0.Final]  
  at io.undertow.servlet.handlers.ServletInitialHandler.handleFirstRequest(ServletInitialHandler.Java:261) [undertow-servlet-1.1.0.Final.jar:1.1.0.Final]  
  at io.undertow.servlet.handlers.ServletInitialHandler.dispatchRequest(ServletInitialHandler.Java:247) [undertow-servlet-1.1.0.Final.jar:1.1.0.Final]  
  at io.undertow.servlet.handlers.ServletInitialHandler.access$000(ServletInitialHandler.Java:76) [undertow-servlet-1.1.0.Final.jar:1.1.0.Final]  
  at io.undertow.servlet.handlers.ServletInitialHandler$1.handleRequest(ServletInitialHandler.Java:166) [undertow-servlet-1.1.0.Final.jar:1.1.0.Final]  
  at io.undertow.server.Connectors.executeRootHandler(Connectors.Java:197) [undertow-core-1.1.0.Final.jar:1.1.0.Final]  
  at io.undertow.server.HttpServerExchange$1.run(HttpServerExchange.Java:759) [undertow-core-1.1.0.Final.jar:1.1.0.Final]  
  at Java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.Java:1145) [rt.jar:1.7.0_75]  
  at Java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.Java:615) [rt.jar:1.7.0_75]  
  at Java.lang.Thread.run(Thread.Java:745) [rt.jar:1.7.0_75]  
Caused by: com.Sun.faces.spi.InjectionProviderException: Java.lang.IllegalStateException: JBAS011048: Failed to construct component instance  
  at org.jboss.as.jsf.injection.JSFInjectionProvider.invokePostConstruct(JSFInjectionProvider.Java:63) [wildfly-jsf-injection-8.2.0.Final.jar:8.2.0.Final]  
  at com.Sun.faces.mgbean.BeanBuilder.invokePostConstruct(BeanBuilder.Java:221) [jsf-impl-2.2.8-jbossorg-1.jar:]  
  ... 70 more  
Caused by: Java.lang.IllegalStateException: JBAS011048: Failed to construct component instance  
  at org.jboss.as.ee.component.BasicComponent.constructComponentInstance(BasicComponent.Java:162)  
  at org.jboss.as.ee.component.BasicComponent.constructComponentInstance(BasicComponent.Java:133)  
  at org.jboss.as.ee.component.BasicComponent.createInstance(BasicComponent.Java:100)  
  at org.jboss.as.ee.component.ComponentRegistry$ComponentManagedReferenceFactory.getReference(ComponentRegistry.Java:171)  
  at org.jboss.as.ee.component.ComponentRegistry.createInstance(ComponentRegistry.Java:87)  
  at org.jboss.as.web.common.WebInjectionContainer.newInstance(WebInjectionContainer.Java:77)  
  at org.jboss.as.jsf.injection.JSFInjectionProvider.invokePostConstruct(JSFInjectionProvider.Java:61) [wildfly-jsf-injection-8.2.0.Final.jar:8.2.0.Final]  
  ... 71 more  
Caused by: org.jboss.weld.exceptions.WeldException: WELD-000049: Unable to invoke public void com.wangyin.hzcrp.controller.ResumeAudit.init() on com.wangyin.hzcrp.controller.ResumeAudit@628e5ac9  
  at org.jboss.weld.injection.producer.DefaultLifecycleCallbackInvoker.invokeMethods(DefaultLifecycleCallbackInvoker.Java:100) [weld-core-impl-2.2.6.Final.jar:2014-10-03 10:05]  
  at org.jboss.weld.injection.producer.DefaultLifecycleCallbackInvoker.postConstruct(DefaultLifecycleCallbackInvoker.Java:81) [weld-core-impl-2.2.6.Final.jar:2014-10-03 10:05]  
  at org.jboss.weld.injection.producer.BasicInjectionTarget.postConstruct(BasicInjectionTarget.Java:126) [weld-core-impl-2.2.6.Final.jar:2014-10-03 10:05]  
  at org.jboss.weld.injection.producer.BeanInjectionTarget.postConstruct(BeanInjectionTarget.Java:70) [weld-core-impl-2.2.6.Final.jar:2014-10-03 10:05]  
  at org.jboss.as.weld.deployment.processors.WeldComponentIntegrationProcessor$3.run(WeldComponentIntegrationProcessor.Java:196) [wildfly-weld-8.2.0.Final.jar:8.2.0.Final]  
  at org.jboss.as.weld.deployment.processors.WeldComponentIntegrationProcessor$AbstractInjectionTargetDelegatingInterceptor.processInvocation(WeldComponentIntegrationProcessor.Java:260) [wildfly-weld-8.2.0.Final.jar:8.2.0.Final]  
  at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.Java:309)  
  at org.jboss.as.weld.injection.WeldInjectionInterceptor.processInvocation(WeldInjectionInterceptor.Java:53) [wildfly-weld-8.2.0.Final.jar:8.2.0.Final]  
  at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.Java:309)  
  at org.jboss.as.ee.component.AroundConstructInterceptorFactory$1.processInvocation(AroundConstructInterceptorFactory.Java:28)  
  at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.Java:309)  
  at org.jboss.as.weld.injection.WeldInterceptorInjectionInterceptor.processInvocation(WeldInterceptorInjectionInterceptor.Java:56) [wildfly-weld-8.2.0.Final.jar:8.2.0.Final]  
  at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.Java:309)  
  at org.jboss.as.weld.injection.WeldInjectionContextInterceptor.processInvocation(WeldInjectionContextInterceptor.Java:43) [wildfly-weld-8.2.0.Final.jar:8.2.0.Final]  
  at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.Java:309)  
  at org.jboss.as.ee.concurrent.ConcurrentContextInterceptor.processInvocation(ConcurrentContextInterceptor.Java:45) [wildfly-ee-8.2.0.Final.jar:8.2.0.Final]  
  at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.Java:309)  
  at org.jboss.invocation.ContextClassLoaderInterceptor.processInvocation(ContextClassLoaderInterceptor.Java:64)  
  at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.Java:309)  
  at org.jboss.invocation.InterceptorContext.run(InterceptorContext.Java:326)  
  at org.jboss.invocation.PrivilegedWithCombinerInterceptor.processInvocation(PrivilegedWithCombinerInterceptor.Java:80)  
  at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.Java:309)  
  at org.jboss.invocation.ChainedInterceptor.processInvocation(ChainedInterceptor.Java:61)  
  at org.jboss.as.ee.component.BasicComponent.constructComponentInstance(BasicComponent.Java:160)  
  ... 77 more  
Caused by: Java.lang.reflect.InvocationTargetException  
  at Sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [rt.jar:1.7.0_75]  
  at Sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.Java:57) [rt.jar:1.7.0_75]  
  at Sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.Java:43) [rt.jar:1.7.0_75]  
  at Java.lang.reflect.Method.invoke(Method.Java:606) [rt.jar:1.7.0_75]  
  at org.jboss.weld.injection.producer.DefaultLifecycleCallbackInvoker.invokeMethods(DefaultLifecycleCallbackInvoker.Java:98) [weld-core-impl-2.2.6.Final.jar:2014-10-03 10:05]  
  ... 100 more  
Caused by: Java.lang.IllegalArgumentException: Can not set javax.persistence.EntityManager field com.wangyin.hzcrp.storage.dao.BasicDao.em to com.wangyin.hzcrp.storage.dao.ResumeDao$Proxy$_$$_WeldSubclass  
  at Sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.Java:164) [rt.jar:1.7.0_75]  
  at Sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.Java:168) [rt.jar:1.7.0_75]  
  at Sun.reflect.UnsafeFieldAccessorImpl.ensureObj(UnsafeFieldAccessorImpl.Java:55) [rt.jar:1.7.0_75]  
  at Sun.reflect.UnsafeObjectFieldAccessorImpl.set(UnsafeObjectFieldAccessorImpl.Java:75) [rt.jar:1.7.0_75]  
  at Java.lang.reflect.Field.set(Field.Java:741) [rt.jar:1.7.0_75]  
  at org.jboss.weld.injection.FieldInjectionPoint.inject(FieldInjectionPoint.Java:94) [weld-core-impl-2.2.6.Final.jar:2014-10-03 10:05]  
  at org.jboss.weld.util.Beans.injectBoundFields(Beans.Java:370) [weld-core-impl-2.2.6.Final.jar:2014-10-03 10:05]  
  at org.jboss.weld.util.Beans.injectFieldsAndInitializers(Beans.Java:381) [weld-core-impl-2.2.6.Final.jar:2014-10-03 10:05]  
  at org.jboss.weld.injection.producer.ResourceInjector$1.proceed(ResourceInjector.Java:70) [weld-core-impl-2.2.6.Final.jar:2014-10-03 10:05]  
  at org.jboss.weld.injection.InjectionContextImpl.run(InjectionContextImpl.Java:48) [weld-core-impl-2.2.6.Final.jar:2014-10-03 10:05]  
  at org.jboss.weld.injection.producer.ResourceInjector.inject(ResourceInjector.Java:72) [weld-core-impl-2.2.6.Final.jar:2014-10-03 10:05]  
  at org.jboss.weld.injection.producer.BasicInjectionTarget.inject(BasicInjectionTarget.Java:121) [weld-core-impl-2.2.6.Final.jar:2014-10-03 10:05]  
  at org.jboss.weld.bean.ManagedBean.create(ManagedBean.Java:150) [weld-core-impl-2.2.6.Final.jar:2014-10-03 10:05]  
  at org.jboss.weld.context.AbstractContext.get(AbstractContext.Java:96) [weld-core-impl-2.2.6.Final.jar:2014-10-03 10:05]  
  at org.jboss.weld.bean.proxy.ContextBeanInstance.getInstance(ContextBeanInstance.Java:98) [weld-core-impl-2.2.6.Final.jar:2014-10-03 10:05]  
  at org.jboss.weld.bean.proxy.ProxyMethodHandler.invoke(ProxyMethodHandler.Java:78) [weld-core-impl-2.2.6.Final.jar:2014-10-03 10:05]  
  at com.wangyin.hzcrp.storage.dao.ResumeDao$Proxy$_$$_WeldClientProxy.find(Unknown Source) [crp-storage-1.0.0-SNAPSHOT.jar:]  
  at com.wangyin.hzcrp.controller.ResumeAudit.init(ResumeAudit.Java:81) [classes:]  
  ... 105 more  

誰かが私を助けることができますか?どうもありがとう。

11
SuperChia

永続コンテキストをマネージドBeanに注入する適切な方法は次のとおりです。

@PersistenceContext(unitName = "my-pu-name")
private EntityManager em;

これは、マネージドBean、つまりコンテナーによって作成/破棄されたBeanでのみ実行できることに注意してください。

ベストプラクティスは、これをプロデューサーとして追加することです。これにより、@InjectをマネージドBeanに使用できるようになります。

@ApplicationScoped
public class ApplicationResources {
    @Produces
    @PersistenceContext(unitName = "my-pu-name")
    private EntityManager em;
}

次に、それをJAX-RSエンドポイント、EJB、およびコンテナーによって管理されるその他のBeanに注入できます。

@Stateless
public class FooService {
    @Inject
    EntityManager em;
}
19
jpkrohling