当前位置: 首页 > 知识库问答 >
问题:

使用CDI-Weld-001408注入log4j失败:带有限定符@default的类型Logger的依赖项不满足

阎唯
2023-03-14

我确实遇到了与这里所解释的相同的问题:JBOSS7.1.3:@EJB工作而@Inject失败,这可能是相同的设置(Wildfly 8.0)。

尽管有一个带有producer的资源类,但我不能注入一个Logger实例。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;

   [...]
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;
    }
}
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企业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”:我有什么不明白的地方,这次注射失败,出现WELLD-001408错误。

共有1个答案

方通
2023-03-14

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

loginservice导入并希望注入以下内容:

import java.util.logging.Logger;

...

@Inject
private transient Logger log;

LoginController尝试使用SLF4J:

import org.slf4j.Logger;

...

@Inject
private Logger log;
import org.apache.log4j.Logger;
 类似资料: