英文:
Weblogic cannot inject @PersistenceContext in static field
问题
我对Weblogic并不是很熟悉,也许这是一个愚蠢的问题,但我不明白为什么如果持久化上下文将被注入到静态字段中,部署会失败。
@PersistenceContext(unitName = "name")
private static EntityManager em;
java.lang.IllegalArgumentException: 类 "class_name" 中的字段 "em" 被定义为使用注解 @PersistenceContext 的注入目标,但是 "em" 不能被声明为静态字段。
at weblogic.j2ee.dd.xml.validator.injectiontarget.BaseValidator.error(BaseValidator.java:132)
at weblogic.j2ee.dd.xml.validator.injectiontarget.BaseValidator.error(BaseValidator.java:149)
at weblogic.j2ee.dd.xml.validator.injectiontarget.J2EEValidator.checkModifier(J2EEValidator.java:28)
at weblogic.j2ee.dd.xml.validator.AbstractAnnotationValidator.checkField(AbstractAnnotationValidator.java:57)
at weblogic.j2ee.dd.xml.validator.AbstractAnnotationValidator.validate(AbstractAnnotationValidator.java:26)
at weblogic.j2ee.dd.xml.validator.AnnotationValidatorVisitor.visitInjectionTargetBean(AnnotationValidatorVisitor.java:48)
at weblogic.j2ee.dd.xml.validator.AnnotationValidatorVisitor.visit(AnnotationValidatorVisitor.java:25)
at weblogic.descriptor.internal.AbstractDescriptorBean.accept(AbstractDescriptorBean.java:1448)
at weblogic.descriptor.internal.AbstractDescriptorBean.accept(AbstractDescriptorBean.java:1452)
at weblogic.descriptor.internal.AbstractDescriptorBean.accept(AbstractDescriptorBean.java:1452)
at weblogic.descriptor.internal.AbstractDescriptorBean.accept(AbstractDescriptorBean.java:1452)
at weblogic.descriptor.internal.AbstractDescriptorBean.accept(AbstractDescriptorBean.java:1452)
at weblogic.j2ee.dd.xml.BaseJ2eeAnnotationProcessor.validate(BaseJ2eeAnnotationProcessor.java:205)
at weblogic.j2ee.dd.xml.BaseJ2eeAnnotationProcessor.validate(BaseJ2eeAnnotationProcessor.java:197)
at weblogic.ejb.container.metadata.EjbAnnotationProcessor.processAnnotations(EjbAnnotationProcessor.java:260)
at weblogic.ejb.container.metadata.EjbDescriptorReaderImpl.processAnnotations(EjbDescriptorReaderImpl.java:364)
at weblogic.ejb.container.deployer.EJBMetadataHandler.processAnnotations(EJBMetadataHandler.java:164)
at weblogic.ejb.container.deployer.EJBMetadataHandler.processAnnotations(EJBMetadataHandler.java:157)
at weblogic.ejb.container.deployer.EJBModule.processAnnotations(EJBModule.java:175)
at weblogic.ejb.container.deployer.EJBModule.prepare(EJBModule.java:370)
at weblogic.application.internal.ExtensibleModuleWrapper$PrepareStateChange.next(ExtensibleModuleWrapper.java:295)
at weblogic.application.internal.ExtensibleModuleWrapper$PrepareStateChange.next(ExtensibleModuleWrapper.java:285)
at weblogic.application.utils.StateMachineDriver.nextState(StateMachineDriver.java:45)
at weblogic.application.internal.ExtensibleModuleWrapper.prepare(ExtensibleModuleWrapper.java:109)
at weblogic.application.internal.flow.ModuleListenerInvoker.prepare(ModuleListenerInvoker.java:100)
at weblogic.application.internal.flow.ModuleStateDriver$1.next(ModuleStateDriver.java:192)
at weblogic.application.internal.flow.ModuleStateDriver$1.next(ModuleStateDriver.java:187)
at weblogic.application.utils.StateMachineDriver$ParallelChange.run(StateMachineDriver.java:83)
at weblogic.work.ContextWrap.run(ContextWrap.java:46)
at weblogic.work.SelfTuningWorkManagerImpl$WorkAdapterImpl.run(SelfTuningWorkManagerImpl.java:670)
at weblogic.invocation.ComponentInvocationContextManager._runAs(ComponentInvocationContextManager.java:352)
at weblogic.invocation.ComponentInvocationContextManager.runAs(ComponentInvocationContextManager.java:337)
at weblogic.work.LivePartitionUtility.doRunWorkUnderContext(LivePartitionUtility.java:57)
at weblogic.work.PartitionUtility.runWorkUnderContext(PartitionUtility.java:41)
at weblogic.work.SelfTuningWorkManagerImpl.runWorkUnderContext(SelfTuningWorkManagerImpl.java:644)
at weblogic.work.ExecuteThread.execute(ExecuteThread.java:415)
at weblogic.work.ExecuteThread.run(ExecuteThread.java:355)
英文:
I'm not very skilled on Weblogic, and maybe is a stupid question, but I'm unable to understand why the deploy fail if the persistence context will be injected in a static field.
@PersistenceContext(unitName = "name")
private static EntityManager em;
java.lang.IllegalArgumentException: Field "em" in class "class_name" is defined as injection target with annotation @PersistenceContext, but "em" cannot be declared as static field.
at weblogic.j2ee.dd.xml.validator.injectiontarget.BaseValidator.error(BaseValidator.java:132)
at weblogic.j2ee.dd.xml.validator.injectiontarget.BaseValidator.error(BaseValidator.java:149)
at weblogic.j2ee.dd.xml.validator.injectiontarget.J2EEValidator.checkModifier(J2EEValidator.java:28)
at weblogic.j2ee.dd.xml.validator.AbstractAnnotationValidator.checkField(AbstractAnnotationValidator.java:57)
at weblogic.j2ee.dd.xml.validator.AbstractAnnotationValidator.validate(AbstractAnnotationValidator.java:26)
at weblogic.j2ee.dd.xml.validator.AnnotationValidatorVisitor.visitInjectionTargetBean(AnnotationValidatorVisitor.java:48)
at weblogic.j2ee.dd.xml.validator.AnnotationValidatorVisitor.visit(AnnotationValidatorVisitor.java:25)
at weblogic.descriptor.internal.AbstractDescriptorBean.accept(AbstractDescriptorBean.java:1448)
at weblogic.descriptor.internal.AbstractDescriptorBean.accept(AbstractDescriptorBean.java:1452)
at weblogic.descriptor.internal.AbstractDescriptorBean.accept(AbstractDescriptorBean.java:1452)
at weblogic.descriptor.internal.AbstractDescriptorBean.accept(AbstractDescriptorBean.java:1452)
at weblogic.descriptor.internal.AbstractDescriptorBean.accept(AbstractDescriptorBean.java:1452)
at weblogic.j2ee.dd.xml.BaseJ2eeAnnotationProcessor.validate(BaseJ2eeAnnotationProcessor.java:205)
at weblogic.j2ee.dd.xml.BaseJ2eeAnnotationProcessor.validate(BaseJ2eeAnnotationProcessor.java:197)
at weblogic.ejb.container.metadata.EjbAnnotationProcessor.processAnnotations(EjbAnnotationProcessor.java:260)
at weblogic.ejb.container.metadata.EjbDescriptorReaderImpl.processAnnotations(EjbDescriptorReaderImpl.java:364)
at weblogic.ejb.container.deployer.EJBMetadataHandler.processAnnotations(EJBMetadataHandler.java:164)
at weblogic.ejb.container.deployer.EJBMetadataHandler.processAnnotations(EJBMetadataHandler.java:157)
at weblogic.ejb.container.deployer.EJBModule.processAnnotations(EJBModule.java:175)
at weblogic.ejb.container.deployer.EJBModule.prepare(EJBModule.java:370)
at weblogic.application.internal.ExtensibleModuleWrapper$PrepareStateChange.next(ExtensibleModuleWrapper.java:295)
at weblogic.application.internal.ExtensibleModuleWrapper$PrepareStateChange.next(ExtensibleModuleWrapper.java:285)
at weblogic.application.utils.StateMachineDriver.nextState(StateMachineDriver.java:45)
at weblogic.application.internal.ExtensibleModuleWrapper.prepare(ExtensibleModuleWrapper.java:109)
at weblogic.application.internal.flow.ModuleListenerInvoker.prepare(ModuleListenerInvoker.java:100)
at weblogic.application.internal.flow.ModuleStateDriver$1.next(ModuleStateDriver.java:192)
at weblogic.application.internal.flow.ModuleStateDriver$1.next(ModuleStateDriver.java:187)
at weblogic.application.utils.StateMachineDriver$ParallelChange.run(StateMachineDriver.java:83)
at weblogic.work.ContextWrap.run(ContextWrap.java:46)
at weblogic.work.SelfTuningWorkManagerImpl$WorkAdapterImpl.run(SelfTuningWorkManagerImpl.java:670)
at weblogic.invocation.ComponentInvocationContextManager._runAs(ComponentInvocationContextManager.java:352)
at weblogic.invocation.ComponentInvocationContextManager.runAs(ComponentInvocationContextManager.java:337)
at weblogic.work.LivePartitionUtility.doRunWorkUnderContext(LivePartitionUtility.java:57)
at weblogic.work.PartitionUtility.runWorkUnderContext(PartitionUtility.java:41)
at weblogic.work.SelfTuningWorkManagerImpl.runWorkUnderContext(SelfTuningWorkManagerImpl.java:644)
at weblogic.work.ExecuteThread.execute(ExecuteThread.java:415)
at weblogic.work.ExecuteThread.run(ExecuteThread.java:355)
答案1
得分: 1
@Vincenzo和我是同事,所以我们要告诉您,他通过配置Weblogic在生产模式下解决了这个问题。
原因仍然不清楚,但暂时并不重要,因为...它起作用了!
英文:
@Vincenzo and I are colleagues, so we would inform you that he solved the issue by configuring Weblogic in production mode.
The reason is still obscure, but for the moment it doesn't matter, because... it works!
答案2
得分: 0
不应使用 static
修饰符,应使用非静态变量
英文:
Should not static
modifier, use non-static variable
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论