gpt4 book ai didi

java - 无法使用 @PersistenceContext 注入(inject) EntityManager [将持久性单元注入(inject) CDI 托管 bean 时出错。]

转载 作者:行者123 更新时间:2023-11-30 21:48:15 26 4
gpt4 key购买 nike

我有 Dao 对象,我想将 EntityManager 注入(inject)到:

    @Transactional
public class ConfigEntryDao implements IDao<ConfigEntryEntity> {

//THIS IS WHAT I WANT TO DO!
@PersistenceContext(unitName = XChangeUtil.JPA.JPA_PU_NAME)
private EntityManager entityManager;
//ONLY THIS ONE WORKS!
//private EntityManagerFactory emf = Persistence.createEntityManagerFactory(XChangeUtil.JPA.JPA_PU_NAME);
//private EntityManager entityManager = emf.createEntityManager();
//....Other methods bellow

如果我在没有注入(inject)的情况下使用实体管理器工厂的注释掉版本,我可以让我的代码工作,一切正常,一旦我使用@Persistence context,我就会得到异常:

14:18:15,854 ERROR [org.jboss.msc.service.fail] (MSC service thread 1-5) MSC000001: Failed to start service jboss.deployment.unit."xChange-1.0-SNAPSHOT.war".WeldStartService: org.jboss.msc.service.StartException in service jboss.deployment.unit."xChange-1.0-SNAPSHOT.war".WeldStartService: Failed to start service
at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1978)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.IllegalArgumentException: WFLYWELD0037: Error injecting persistence unit into CDI managed bean. Can't find a persistence unit named 'eclipselinkPu' in deployment xChange-1.0-SNAPSHOT.war for injection point private javax.persistence.EntityManager com.bisciak.xchange.dao.ConfigEntryDao.entityManager
at org.jboss.as.weld.services.bootstrap.WeldJpaInjectionServices.getScopedPUName(WeldJpaInjectionServices.java:114)
at org.jboss.as.weld.services.bootstrap.WeldJpaInjectionServices.registerPersistenceContextInjectionPoint(WeldJpaInjectionServices.java:77)
at org.jboss.weld.injection.ResourceInjectionFactory$PersistenceContextResourceInjectionProcessor.getResourceReferenceFactory(ResourceInjectionFactory.java:350)
at org.jboss.weld.injection.ResourceInjectionFactory$PersistenceContextResourceInjectionProcessor.getResourceReferenceFactory(ResourceInjectionFactory.java:338)
at org.jboss.weld.injection.ResourceInjectionFactory$ResourceInjectionProcessor.createFieldResourceInjection(ResourceInjectionFactory.java:216)
at org.jboss.weld.injection.ResourceInjectionFactory$ResourceInjectionProcessor.createResourceInjections(ResourceInjectionFactory.java:188)
at org.jboss.weld.injection.ResourceInjectionFactory.discoverType(ResourceInjectionFactory.java:448)
at org.jboss.weld.injection.ResourceInjectionFactory.getResourceInjections(ResourceInjectionFactory.java:96)
at org.jboss.weld.injection.producer.ResourceInjector.<init>(ResourceInjector.java:59)
at org.jboss.weld.injection.producer.ResourceInjector.of(ResourceInjector.java:49)
at org.jboss.weld.injection.producer.BeanInjectionTarget.<init>(BeanInjectionTarget.java:63)
at org.jboss.weld.injection.producer.BeanInjectionTarget.createDefault(BeanInjectionTarget.java:47)
at org.jboss.weld.manager.InjectionTargetFactoryImpl.chooseInjectionTarget(InjectionTargetFactoryImpl.java:113)
at org.jboss.weld.manager.InjectionTargetFactoryImpl.createInjectionTarget(InjectionTargetFactoryImpl.java:86)
at org.jboss.weld.bean.ManagedBean.<init>(ManagedBean.java:100)
at org.jboss.weld.bean.ManagedBean.of(ManagedBean.java:80)
at org.jboss.weld.bootstrap.AbstractBeanDeployer.createManagedBean(AbstractBeanDeployer.java:261)
at org.jboss.weld.bootstrap.BeanDeployer.createClassBean(BeanDeployer.java:226)
at org.jboss.weld.bootstrap.ConcurrentBeanDeployer$2.doWork(ConcurrentBeanDeployer.java:74)
at org.jboss.weld.bootstrap.ConcurrentBeanDeployer$2.doWork(ConcurrentBeanDeployer.java:71)
at org.jboss.weld.executor.IterativeWorkerTaskFactory$1.call(IterativeWorkerTaskFactory.java:62)
at org.jboss.weld.executor.IterativeWorkerTaskFactory$1.call(IterativeWorkerTaskFactory.java:55)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
at org.jboss.threads.JBossThread.run(JBossThread.java:320)

14:18:15,858 ERROR [org.jboss.as.controller.management-operation] (management-handler-thread - 3) WFLYCTL0013: Operation ("deploy") failed - address: ([("deployment" => "xChange-1.0-SNAPSHOT")]) - failure description: {"WFLYCTL0080: Failed services" => {"jboss.deployment.unit.\"xChange-1.0-SNAPSHOT.war\".WeldStartService" => "Failed to start service
Caused by: java.lang.IllegalArgumentException: WFLYWELD0037: Error injecting persistence unit into CDI managed bean. Can't find a persistence unit named 'eclipselinkPu' in deployment xChange-1.0-SNAPSHOT.war for injection point private javax.persistence.EntityManager com.bisciak.xchange.dao.ConfigEntryDao.entityManager"}}
14:18:15,858 ERROR [org.jboss.as.server] (management-handler-thread - 3) WFLYSRV0021: Deploy of deployment "xChange-1.0-SNAPSHOT.war" was rolled back with the following failure message:
{"WFLYCTL0080: Failed services" => {"jboss.deployment.unit.\"xChange-1.0-SNAPSHOT.war\".WeldStartService" => "Failed to start service
Caused by: java.lang.IllegalArgumentException: WFLYWELD0037: Error injecting persistence unit into CDI managed bean. Can't find a persistence unit named 'eclipselinkPu' in deployment xChange-1.0-SNAPSHOT.war for injection point private javax.persistence.EntityManager com.bisciak.xchange.dao.ConfigEntryDao.entityManager"}}
14:18:15,862 INFO [org.jboss.as.connector.deployers.jdbc] (MSC service thread 1-2) WFLYJCA0019: Stopped Driver service with driver-name = xChange-1.0-SNAPSHOT.war_org.apache.derby.jdbc.AutoloadedDriver_10_12
14:18:15,862 INFO [org.jboss.as.connector.deployers.jdbc] (MSC service thread 1-4) WFLYJCA0019: Stopped Driver service with driver-name = xChange-1.0-SNAPSHOT.war_com.mysql.cj.jdbc.Driver_6_0
14:18:15,982 INFO [org.jboss.as.server.deployment] (MSC service thread 1-2) WFLYSRV0028: Stopped deployment xChange-1.0-SNAPSHOT (runtime-name: xChange-1.0-SNAPSHOT.war) in 122ms
[2018-01-28 02:18:16,041] Artifact xChange:war exploded: Error during artifact deployment. See server log for details.
[2018-01-28 02:18:16,041] Artifact xChange:war exploded: java.lang.Exception: {"WFLYCTL0080: Failed services" => {"jboss.deployment.unit.\"xChange-1.0-SNAPSHOT.war\".WeldStartService" => "Failed to start service
Caused by: java.lang.IllegalArgumentException: WFLYWELD0037: Error injecting persistence unit into CDI managed bean. Can't find a persistence unit named 'eclipselinkPu' in deployment xChange-1.0-SNAPSHOT.war for injection point private javax.persistence.EntityManager com.bisciak.xchange.dao.ConfigEntryDao.entityManager"}}

有趣的是 - 如果我从我的项目中删除 beans.xml,我可以使用 @PersistenceContext 注释并成功部署,但是我的 有问题CDI 。我的 beans.xml 除了 bean 发现之外什么都没有:

<?xml version="1.0" encoding="UTF-8"?>
<beans
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"
bean-discovery-mode="all">
</beans>

我的项目结构+persistence.xml: enter image description here

这是我的ConfigEntryDao的用法:

    public class ConfigurationService {

@Inject
private ConfigEntryDao configEntryDao;

private List<ConfigEntryEntity> configEntries;



@PostConstruct
public void initialize() {
System.out.println("DBG Constructed");
configEntryDao.insert(new ConfigEntryEntity(ConfigNamespace.xchange_test, "Hello"));
configEntryDao.insert(new ConfigEntryEntity(ConfigNamespace.xchange_test_subtest, "World"));
configEntries = configEntryDao.getAll();
}

public void tryTestValue() {
System.out.println("Hello");
System.out.println("Testing data from config db table Size: "+configEntries.size()+" Values:"+ configEntries.toString());
}

}

我从 REST 端点测试方法,因为我太酷了,无法使用 UI:

@Path("/test")
public class TestResource {

@Inject
private ConfigurationService configurationService;

@GET
@Path("/dbtest")
public Response testDb() {
System.out.println("DBG Invocation of dbtest");
configurationService.tryTestValue();
return Response.status(Response.Status.OK).build();
}

}

所以问题是:有人知道为什么我不能通过 PersistenceContext 注入(inject)实体管理器吗?

注意:我使用的是最新稳定版的Wildfly

最佳答案

您的 ConfigEntryDao 应该由容器管理,以便让容器注入(inject) EntityManager。例如,通过使用 @Stateless 注释使其成为无状态 session bean (SLSB)。

另见 here了解如何为 EntityManager 创建生产者。

关于java - 无法使用 @PersistenceContext 注入(inject) EntityManager [将持久性单元注入(inject) CDI 托管 bean 时出错。],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48486906/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com