java - 带有限定符 @Default 的类型 ZZZZ 的不满足依赖关系

标签 java cdi

我有 bean,如下所示。

@Singleton
@DependsOn("DefaultEmailService")
public class CustomerService implements UserHandlingService {

    private DefaultEmailService mailService;

    @Inject
    public CustomerService(DefaultEmailService mailService) {       
        this.mailService = mailService;
    }
}


@Singleton
@Startup
public class DefaultEmailService implements EmailService {

    public DefaultEmailService() {  
    }
}

我得到类似这样的错误

Caused by: org.jboss.weld.exceptions.DeploymentException: WELD-001408: Unsatisfied dependencies for type DefaultEmailService with qualifiers @Default
  at injection point [BackedAnnotatedParameter] Parameter 1 of [BackedAnnotatedConstructor] @Inject public com.project.service.CustomerService(DefaultEmailService)
  at com.project.service.CustomerService.<init>(CustomerService.java:0)

我做错了什么吗?

最佳答案

问题出在 @Singleton 注释,它来自 javax.ejb 而不是 javax.inject。使用 ejb 并定义接口(interface),您的 bean 在 CDI 上下文中注册为接口(interface),而不是实现,更改您的代码:

@Inject
public CustomerService(EmailService mailService) {
    this.mailService = (DefaultEmailService) mailService;
}

关于java - 带有限定符 @Default 的类型 ZZZZ 的不满足依赖关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30996887/

相关文章:

javascript - 将变量从 jsp 页面传递到 servlet

java - N个元素的一维数组是否需要与2D[m][n]相同的内存量,m*n=N

java - 在带有空格的同一行中使用 Lambda foreach 进行打印

maven - 启动payara 5 遇到过

java - @ApplicationScoped 在集群中

java - 通过setter方法注入(inject)EJB

java - if-then-else 语句不起作用

java - 无法在 Eclipse Kepler 中创建 JavaFX 项目

java - 将 SDN-4 添加到 GF-3 : Exception while loading the app: IllegalStateException: ContainerBase. addChild : start: org. apache.catalina.LifecycleException

unit-testing - 使用 CDI Unit 和 EasyMock 进行单元测试