【问题标题】:WELD-001408: Unsatisfied dependencies for type List with qualifiersWELD-001408:具有限定符的类型 List 的依赖关系不满足
【发布时间】:2015-05-08 09:45:42
【问题描述】:

我被臭名昭著的 WELD-001408 困住了,每个使用 CDI 编程的人都以某种方式遇到过。使用 Oracle JDK 1.8.0_25、Wildfly 8.2.0.Final。我的代码如下:

模块 availability-service (war) 依赖于模块 hospital-user (jar)。前者在WEB-INF 中有一个beans.xml,后来在META-INF 中有一个beans.xml,尽管CDI 1.1 不需要beans.xml

hospital-user:

@ApplicationScoped
public class Users {
    @Produces
    @Doctors
    public List<Doctor> getDoctors() {
        return getUsers("/doctors.json", Doctor.class);
    }

    @Produces
    @Patients
    public List<Patient> getPatients() {
        return getUsers("/patients.json", Patient.class);
    }
}

Doctors 注解(Patients 类似,只是名字不同):

@Qualifier
@Retention(RUNTIME)
@Target({ FIELD, METHOD })
public @interface Doctors {
}

availability-service:

@ApplicationScoped
public class AvailabilityService {
    @Inject
    @Doctors
    private List<Doctor> doctors;
}

错误:

Caused by: org.jboss.weld.exceptions.DeploymentException: WELD-001408: Unsatisfied dependencies for type List<Doctor> with qualifiers @Doctors
  at injection point [BackedAnnotatedField] @Inject @Doctors private name.abhijitsarkar.microservices.availability.AvailabilityService.doctors
  at name.abhijitsarkar.microservices.availability.AvailabilityService.doctors(AvailabilityService.java:0)

    at org.jboss.weld.bootstrap.Validator.validateInjectionPointForDeploymentProblems(Validator.java:372)

如果我注入 Users 而不是 List,它会起作用。 Here 是一个示例 Maven 项目,它演示了 4 个类的问题。只需运行mvn clean test

【问题讨论】:

    标签: dependency-injection cdi wildfly inject weld


    【解决方案1】:

    在您提供的示例 Maven 项目中,测试类中的依赖注入不可用,因为您将 Arquillian 部署指定为 @Deployment(testable = true)

    当设置为true(默认值)时,测试通过。

    另外,在测试方法中添加如下注入点:

    @Inject
    @Employees
    List<Employee> employees;
    

    工作正常,这表明您的 bean 部署是有效的。

    当 Arquillian 部署与您描述的应用程序 WAR 结构完全匹配时,它会继续工作,即:

    @Deployment
    public static WebArchive createDeployment() {
    return create(WebArchive.class, "availability-service.war")
        .addAsLibraries(create(JavaArchive.class, "hospital-user.jar")
            .addPackages(true, Filters.exclude(".*Test.*"), Producer.class.getPackage())
            .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml"))
        .addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml");
    }
    

    【讨论】:

      猜你喜欢
      • 2015-06-10
      • 2018-12-19
      • 2015-04-05
      • 2015-02-09
      • 1970-01-01
      • 2014-12-20
      • 1970-01-01
      • 2017-08-28
      • 1970-01-01
      相关资源
      最近更新 更多