gpt4 book ai didi

Java EE CDI 部署异常 - WELD-001408 : Unsatisfied dependencies for type EntityManager with qualifiers @Default

转载 作者:行者123 更新时间:2023-12-02 01:05:59 24 4
gpt4 key购买 nike

我对 Java EE 还很陌生。我经过长时间的努力搜索,但找不到其他人遇到的问题的解决方案。我查看了 SO 上的其他相关帖子,并且按照其他帖子的建议查看了 JBoss 和 Oracle 文档,但我仍然无法解决我的问题。我正在使用 Eclipse 开发一个 JAX-RS 应用程序,该应用程序使用 JPA 与 MariaDB 交互。我使用 Maven webapp-jee7-liberty 原型(prototype)来创建该项目,因此我在 Liberty 运行时上运行它。以下是我的项目方面:

enter image description here

这是我的项目结构:

enter image description here

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>

这是我的存储库类:

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$1.doWork(ConcurrentValidator.java:64) at org.jboss.weld.bootstrap.ConcurrentValidator$1.doWork(ConcurrentValidator.java:62) 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(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)

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

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();
}

...

}

从我在其他帖子和其他地方看到的情况来看,我似乎已经具备了实现此功能所需的条件,但我不断收到此异常。我错过了什么?

最佳答案

由于 beans.xml 中的 bean-discovery-mode 设置为 "annotated",CDI 不会将 EntityManagerProducer 视为一个 Bean 并且不会激活生产者方法。

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

关于Java EE CDI 部署异常 - WELD-001408 : Unsatisfied dependencies for type EntityManager with qualifiers @Default,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57728702/

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