3

我想注入默认的 Java 记录器。然而,Eclipse 强调它并声明“没有 bean 有资格注入到注入点 [JSR-299 §5.2.1]”

如果我仍然部署,则会引发以下异常。为什么注入Java Logger失败?EntityManager 相同,但我自己的 UserRepository Bean 则不然。

org.jboss.weld.exceptions.DeploymentException: WELD-001408 Unsatisfied dependencies for type [Logger] with qualifiers [@Default] at injection point [[field] 

代码:

import java.util.logging.Logger;

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

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


// The @Stateless annotation eliminates the need for manual transaction demarcation
@Stateless
public class LoginService {

    @Inject
    private Logger log;

    @Inject
    private EntityManager em;

    @Inject
    private UserRepository repository;

    public User login(User user) {
        log.info("login " + user.getUsername());

        User rUser = repository.findByCredentials(user.getUsername(), user.getPassword());
        return rUser;
    }
}
4

1 回答 1

7

要注入记录器,您需要一个生产者方法,该方法提供Logger您可以注入的方法。

  import java.util.logging.Logger;   

  import javax.enterprise.inject.Produces;   
  import javax.enterprise.inject.spi.InjectionPoint;   

  public class LoggerProduer {   

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

并且EntityManager需要使用注入,@PersistenceContext(unitName="pscontext")因为它是使用你的数据创建的persistence.xml,所以你EntityManager必须是

   @PersistenceContex(unitName="pscontext")
   private EntityManager em;
于 2013-11-04T20:46:40.637 回答