Java EE CDI 部署异常 - WELD-001408:具有限定符 @Default 的类型 EntityManager 的依赖关系不满足

Java EE CDI Deployment Exception - WELD-001408: Unsatisfied dependencies for type EntityManager with qualifiers @Default

我是 Java EE 的新手。我进行了长期而艰苦的搜索,但找不到其他人遇到的问题的解决方案。我查看了关于 SO 的其他相关帖子,并且按照其他帖子的建议查看了 JBoss 和 Oracle 文档,但我仍然无法解决我的问题。我正在使用 Eclipse 开发一个使用 JPA 与 MariaDB 交互的 JAX-RS 应用程序。我使用 Maven webapp-jee7-liberty 原型来创建项目,所以我 运行 它在 Liberty 运行时上。这是我的项目方面:

这是我的项目结构:

beans.xml 文件:

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

web.xml 文件:

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="3.1" 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/web-app_3_1.xsd">
    <display-name>journal-task-mgr</display-name>
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file>
    </welcome-file-list>
</web-app>

这是我的存储库 class:

package me.org.jtm.repository;

import java.util.List;

import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.persistence.TypedQuery;

import me.org.jtm.repository.bean.Task;

@RequestScoped
public class TaskRepositoryImpl implements TaskRepository {

    private EntityManager em;
    private EntityTransaction tx;

    @Inject
    public TaskRepositoryImpl(EntityManager em) {
        this.em = em;
        this.tx = em.getTransaction();
    }

    @Override
    public List<Task> get() {
        tx.begin();
        List<Task> resultList = em.createNamedQuery("AllTaskEntries", Task.class).getResultList();
        tx.commit();
        return resultList;
    }

    ...

}

我创建了一个生产者,以便能够为 EntityManager 使用 @Inject 注释:

package me.org.jtm.repository;

import javax.enterprise.context.RequestScoped;
import javax.enterprise.inject.Produces;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

public class EntityManagerProducer {
    @PersistenceContext
    private EntityManager em;

    @Produces
    @RequestScoped
    public EntityManager entityManager() {
        return em;
    }
}

这是我的 persistence.xml:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
    <persistence-unit name="journal-task-mgr" transaction-type="JTA">
        <jta-data-source>jdbc/JournalTaskMgrMariaDb</jta-data-source>
        <class>me.org.jtm.repository.bean.Journal</class>
        <class>me.org.jtm.repository.bean.Task</class>
        <class>me.org.jtm.repository.bean.User</class>
    </persistence-unit>
</persistence>

但我在启动时遇到此错误:

org.jboss.weld.exceptions.DeploymentException: WELD-001408: Unsatisfied dependencies for type EntityManager with qualifiers @Default at injection point [BackedAnnotatedParameter] Parameter 1 of [BackedAnnotatedConstructor] @Inject public me.org.jtm.repository.TaskRepositoryImpl(EntityManager) at me.org.jtm.repository.TaskRepositoryImpl.(TaskRepositoryImpl.java:0)

at org.jboss.weld.bootstrap.Validator.validateInjectionPointForDeploymentProblems(Validator.java:378) at org.jboss.weld.bootstrap.Validator.validateInjectionPoint(Validator.java:290) at org.jboss.weld.bootstrap.Validator.validateGeneralBean(Validator.java:143) at org.jboss.weld.bootstrap.Validator.validateRIBean(Validator.java:164) at org.jboss.weld.bootstrap.Validator.validateBean(Validator.java:526) at org.jboss.weld.bootstrap.ConcurrentValidator.doWork(ConcurrentValidator.java:64) at org.jboss.weld.bootstrap.ConcurrentValidator.doWork(ConcurrentValidator.java:62) at org.jboss.weld.executor.IterativeWorkerTaskFactory.call(IterativeWorkerTaskFactory.java:62) at org.jboss.weld.executor.IterativeWorkerTaskFactory.call(IterativeWorkerTaskFactory.java:55) at java.util.concurrent.FutureTask.run(Unknown Source) at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) at java.lang.Thread.run(Unknown Source)

依赖注入似乎在我的资源和服务中运行良好 classes:

package me.org.jtm.resources;

import java.util.List;

import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriBuilder;
import javax.ws.rs.core.UriInfo;

import me.org.jtm.domain.TaskService;
import me.org.jtm.repository.bean.Task;

@RequestScoped
public class TaskResourceImpl implements TaskResource {
    private TaskService taskService;

    @Inject
    public TaskResourceImpl(TaskService taskService) {
        this.taskService = taskService;
    }

    @Override
    public Response get() {
        List<Task> tasks = taskService.getAll();    
        return Response.ok(tasks).build();
    }

    ...

}

从我在其他帖子和其他地方看到的情况来看,我似乎已经准备好让它工作所需的一切,但我一直收到此异常。我错过了什么?

由于bean-discovery-mode在beans.xml中设置为"annotated",CDI不会将EntityManagerProducer视为Bean,不会激活producer方法。

EntityManagerProducer 上添加范围。 @ApplicationScoped 可能适合这种情况。