@Inject Logger (CDI) in EJB with jBoss



我试图将我的记录器注入到我的EJB中,做:

@Inject
Logger logger;

但是我收到以下错误

Caused by: org.jboss.weld.exceptions.AmbiguousResolutionException: WELD-001318 Cannot resolve an ambiguous dependency between 
[Producer Method [Logger] with qualifiers [@Any @Default] declared as [[method] @Produces public be.fgov.health.ecad.Resource.createLogger(InjectionPoint)], 
Producer Method [Logger] with qualifiers [@Any @Default] declared as [[method] @Produces public be.fgov.health.ecad.Resource.createLogger(InjectionPoint)]]

我真的不知道这可能是什么?据我所知,下面的课程没有问题吗?我不明白为什么jBoss说有一个模棱两可的依赖关系。

@Dependent
public class Resource {
    @Produces
    public Logger createLogger(final InjectionPoint ip) {
        return Logger.getLogger(ip.getMember().getDeclaringClass());
    }
}

您不必自己实现这一点,此用例有一个焊接扩展:

焊接记录仪

从文档中:

现在,只需将记录器对象注入任何 CDI Bean 即可更轻松地将日志记录添加到应用程序中。只需使用 @Logger 限定符注释注释 org.jboss.weld.log.Log 类型成员,就会将适当的记录器对象注入到 Bean 的任何实例中。

import org.jboss.weld.annotation.Logger;
import org.jboss.weld.log.Log;
public class Checkout {
    private @Inject @Logger Log log;

    public void invoiceItems() {
        ShoppingCart cart;
        ...
        log.debug("Items invoiced for {0}", cart);
    }
 }

最新更新