【问题标题】:CDI 2.0 (Weld): Can't inject non CDI componentCDI 2.0(焊接):无法注入非 CDI 组件
【发布时间】:2021-05-03 13:59:26
【问题描述】:

在 Wildfly 20 上部署的休息端点中,我需要注入 javax.ws.rs.core.UriBuilder 类的外部非 CDI 实例。例如:

@Path("...")
public class MyResource
{
  ...
  @Inject
  private UriBuilder uriBuilder;
  ...
}

这里是制作人:

public class UriBuilderProducer
{
  @Produces
  public UriBuilder getUriBuilder()
  {
    return UriBuilder.fromResource(Customer.class);
  }
}

beans.xml 文件内容如下:

<?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_2_0.xsd"
   version="2.0" bean-discovery-mode="all">
</beans>

但是,部署失败并引发以下异常:

15:03:24,837 ERROR [org.jboss.msc.service.fail] (MSC service thread 1-3) MSC000001: Failed to start service jboss.deployment.unit."myresource.war".WeldStartService: org.jboss.msc.service.StartException in service jboss.deployment.unit."myresource.war".WeldStartService: Failed to start service
    at org.jboss.msc@1.4.11.Final//org.jboss.msc.service.ServiceControllerImpl$StartTask.execute(ServiceControllerImpl.java:1731)
    at org.jboss.msc@1.4.11.Final//org.jboss.msc.service.ServiceControllerImpl$ControllerTask.run(ServiceControllerImpl.java:1559)
    at org.jboss.threads@2.3.3.Final//org.jboss.threads.ContextClassLoaderSavingRunnable.run(ContextClassLoaderSavingRunnable.java:35)
    at org.jboss.threads@2.3.3.Final//org.jboss.threads.EnhancedQueueExecutor.safeRun(EnhancedQueueExecutor.java:1982)
    at org.jboss.threads@2.3.3.Final//org.jboss.threads.EnhancedQueueExecutor$ThreadBody.doRunTask(EnhancedQueueExecutor.java:1486)
    at org.jboss.threads@2.3.3.Final//org.jboss.threads.EnhancedQueueExecutor$ThreadBody.run(EnhancedQueueExecutor.java:1377)
    at java.base/java.lang.Thread.run(Thread.java:834)
 Caused by: org.jboss.weld.exceptions.DeploymentException: WELD-001408: Unsatisfied dependencies for type UriBuilder with qualifiers @Default
   at injection point [UnbackedAnnotatedField] @Inject private ...MyResource.uriBuilder
   at ...MyResource.uriBuilder(MyResource.java:0)

我错过了什么?

非常感谢您的任何帮助。

亲切的问候,

西摩

【问题讨论】:

    标签: dependency-injection wildfly cdi


    【解决方案1】:

    我的错,我把 beans.xml 放在错误的地方。为了进入生成的 WAR 的 WEB-INF 目录,需要将它放在 src/main/webapp/WEB-INF 而不是 src/main/resources/WEB-INF 中,正如人们所期望的那样,基于maven COC (Convention Over Configuration) 规则。

    当然,将这个文件放在正确的位置可以解决问题。很抱歉给您带来不便。

    亲切的问候,

    西摩

    【讨论】:

      猜你喜欢
      • 2018-01-19
      • 1970-01-01
      • 1970-01-01
      • 2013-04-30
      • 1970-01-01
      • 2013-04-26
      • 2016-11-21
      • 2023-03-10
      • 2018-02-14
      相关资源
      最近更新 更多