无法使用CDI注入log4j - WELD-001408:对于具有限定符@Default的Logger类型的不满意依赖性

时间:2014-04-26 08:34:14

标签: logging jsf-2 cdi managed-bean

我确实遇到了与此处所述相同的问题:JBoss7.1.3: @EJB works while @Inject fails 据推测,这甚至是相同的设置(Wildfly 8.0)。

尽管有一个带有生产者的Resource类,但我无法注入一个Logger实例。在Controller中Neiter,也不在EJB中。 [问题1]如果我错了,请纠正我,我应该能够将它们注入@Model bean并注入使用@Stateless(EJB)注释的bean,不应该吗?

package com.doe.webapp.service.auxilliary.cdiproducer;

import javax.enterprise.context.Dependent;
import javax.enterprise.context.RequestScoped;
import javax.enterprise.inject.Produces;
import javax.enterprise.inject.spi.InjectionPoint;
import javax.faces.context.FacesContext;
import org.apache.log4j.Logger;

    @Dependent
    public class Resources {

        @Produces
        public Logger produceLog(InjectionPoint injectionPoint) {
            return Logger.getLogger(injectionPoint.getMember().getDeclaringClass().getName());
        }

        @Produces
        @RequestScoped
        public FacesContext produceFacesContext() {
            return FacesContext.getCurrentInstance();
        }
    }

这是我的控制器的开始:

package com.doe.webapp.controller.security;

import java.io.IOException;
import java.util.Map;    
import javax.annotation.PostConstruct;
import javax.enterprise.inject.Model;
import javax.enterprise.inject.Produces;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedProperty;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.inject.Named;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.Pattern;    
import org.slf4j.Logger;

import com.doe.webapp.model.usermgmt.User;
import com.doe.webapp.service.security.LoginService;

@Model
public class LoginController {

    private static final String ADMIN = "admin";

    @Inject
    private Logger log;

    @Inject
    private FacesContext facesContext;

    @Inject
    private LoginService loginService;

   [...]

这是EJB,它也是@Model注释类中的Injected。同样是记录器。虽然前者工作,但Logger的注入会抛出先前解释的错误。

package com.doe.webapp.service.security;

import java.util.logging.Logger;

import javax.ejb.Stateless;
import javax.enterprise.event.Event;
import javax.inject.Inject;

import com.doe.webapp.data.UserRepository;
import com.doe.webapp.model.usermgmt.User;

@Stateless
public class LoginService {

    @Inject
    private transient Logger log;

    @Inject
    private UserRepository repository;

    @Inject
    private Event<User> userEvent;

    public User login(User user) {
        log.info("login " + user.getUsername());
        User rUser = repository.findByCredentials(user.getUsername(), user.getPassword());
        return rUser;
    }
}

现在,我收到LoginController.java控制器的错误。

WELD-001408: Unsatisfied dependencies for type Logger with qualifiers @Default
  at injection point [BackedAnnotatedField] @Inject private com.doe.webapp.controller.security.LoginController.log
  at com.doe.webapp.controller.security.LoginController.log(LoginController.java:0)

另一个用于LoginService.java Enterprise Bean。

WELD-001408: Unsatisfied dependencies for type Logger with qualifiers @Default
  at injection point [BackedAnnotatedField] @Inject private transient com.doe.webapp.service.security.LoginService.log
  at com.doe.webapp.service.security.LoginService.log(LoginService.java:0)
<问题2:问题2:我不理解这种注入因WELD-001408错误而失败。

1 个答案:

答案 0 :(得分:8)

我现在没有Wildfly / JBoss环境,但有一个可能的错误:

LoginService导入并想要注入以下内容:

import java.util.logging.Logger;

...

@Inject
private transient Logger log;

LoginController尝试使用SLF4J:

import org.slf4j.Logger;

...

@Inject
private Logger log;

虽然Resources类使用第三种Logger

import org.apache.log4j.Logger;