简体   繁体   中英

No bean is eligible for injection to the injection point [JSR-299 §5.2.1]

I wanted to inject the default Java logger. However, Eclipse underlines it and states "No bean is eligible for injection to the injection point [JSR-299 §5.2.1]"

If I deploy anyway, the following exception is thrown. Why does it fail to inject Java Logger? Same for the EntityManager, but not for my own UserRepository Bean.

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

code:

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;
    }
}

To inject a logger you need a producer method which gives a Logger which you can Inject.

  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());   
    }   
  }   

And EntityManager needs to be injected using @PersistenceContext(unitName="pscontext") because it's created using data in your persistence.xml , so your EntityManager has to be

   @PersistenceContex(unitName="pscontext")
   private EntityManager em;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM