未找到模糊依赖项的生产者方法

时间:2018-06-14 08:29:11

标签: cdi producer

我正在尝试使用生产者方法来解决模糊的依赖关系。但我只是不认识生产者方法。其他依赖项得到解决而没有问题。我已经在代码中有不同的变化但我总是得到相同的错误。这是我的日志输出:

10:17:36,317 ERROR [org.jboss.msc.service.fail] (MSC service thread 1-1) MSC000001: Failed to start service jboss.deployment.unit."java_rest_test_war_exploded.war".WeldStartService: org.jboss.msc.service.StartException in service jboss.deployment.unit."java_rest_test_war_exploded.war".WeldStartService: Failed to start service
    at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1904)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:748)
Caused by: org.jboss.weld.exceptions.DeploymentException: WELD-001408: Unsatisfied dependencies for type SecondInterface with qualifiers @SecondInterfaceAnnotation
  at injection point [BackedAnnotatedField] @Inject @SecondInterfaceAnnotation private rest.HelloWorldRest.secondInterface
  at rest.HelloWorldRest.secondInterface(HelloWorldRest.java:0)
WELD-001475: The following beans match by type, but none have matching qualifiers:
  - Managed Bean [class rest.SecondBeanImpl2] with qualifiers [@Any @Default],
  - Managed Bean [class rest.SecondBeanImpl1] with qualifiers [@Any @Default]

这是我的代码:

public interface SecondInterface {
    public String getData();
}

@ApplicationScoped
public class FirstBean {

    public String getName() {
        return "FirstBeanName";
    }
}

@ApplicationScoped
public class SecondBeanImpl1 implements SecondInterface {

    public String getData() {
        return "data: SecondBeanImpl ONE";
    }
}

@ApplicationScoped
public class SecondBeanImpl2 implements SecondInterface {

    public String getData() {
        return "data: SecondBeanImpl TWO";
    }
}

@Qualifier
@Target({ TYPE, METHOD, PARAMETER, FIELD })
@Retention(RUNTIME)
public @interface SecondInterfaceAnnotation {
}

@ApplicationScoped
public class SecondBeanProducer {

    @Produces
    @SecondInterfaceAnnotation
    public static SecondInterface getSecondInterface(InjectionPoint injectionPoint) {
        return new SecondBeanImpl1();
    }
}


@ApplicationScoped
@Path("/helloworld")
public class HelloWorldRest {

    @Inject
    FirstBean firstBean;

    @Inject
    @SecondInterfaceAnnotation
    SecondInterface secondInterface;

    @GET
    @Path("/getdata")
    public Response printAppName() throws Exception {
        System.err.println(appName);
        return Response.status(200).entity(secondInterface.getData()).build();
    }
}

我在WEB-INF中的beans.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns="http://xmlns.jcp.org/xml/ns/javaee"
       xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
       bean-discovery-mode="annotated">
</beans>

编辑:

我通过添加范围来了解它的工作原理:

@ApplicationScoped
public class SecondBeanProducer {

    @Produces
    @ApplicationScoped
    @SecondInterfaceAnnotation
    public static SecondInterface getSecondInterface(InjectionPoint injectionPoint) {
        return new SecondBeanImpl1();
    }
}


@ApplicationScoped
@Path("/helloworld")
public class HelloWorldRest {

    @Inject
    FirstBean firstBean;

    @Inject
    @ApplicationScoped
    @SecondInterfaceAnnotation
    SecondInterface secondInterface;

0 个答案:

没有答案