带有jBoss的EJB中的@Inject Logger(CDI)

时间:2015-01-23 14:49:50

标签: java jboss cdi

我试图将我的Logger注入到我的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());
    }

}

1 个答案:

答案 0 :(得分:0)

您不必自己实现此功能,此用例有一个焊接扩展名:

Weld-Logger

来自documentation

通过将logger对象简单地注入任何CDI bean,现在可以更轻松地向应用程序添加日志记录。只需使用@Logger限定符注释注释org.jboss.weld.log.Log类型成员,并将适当的logger对象注入到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);
    }
 }