WELD-001408:带有限定符的类型List的不满意依赖项

时间:2015-03-06 17:52:27

标签: dependency-injection cdi wildfly inject weld

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

模块availability-service(war)取决于模块hospital-user(jar)。前者在beans.xml中有一个WEB-INF,后来在META-INF中有beans.xml,即使CDI 1.1不需要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); } }

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而非注入mvn clean test,则可行。 Here是一个示例Maven项目,用于演示4个类的问题。只需运行{{1}}。

1 个答案:

答案 0 :(得分:0)

在您提供的示例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");
}