3

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;
    }
}
DT7
  • 1,615
  • 14
  • 26
feder
  • 1,775
  • 5
  • 25
  • 36

1 Answers1

7

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;
skuntsel
  • 11,624
  • 11
  • 44
  • 67
SRy
  • 2,901
  • 8
  • 36
  • 57
  • You can also use: @ Produces @ PersistenceContex(unitName="pscontext") EntityManager em; if you want to @ Inject the EntityManager. – Arturo Volpe Nov 04 '13 at 20:54
  • I just don't understand when you need to create your own Producer and when it applicable to simple user @Inject. What are the prerequisites for this? – feder Nov 04 '13 at 21:02
  • 2
    Check out this post you will understand more about Producer methods : http://stackoverflow.com/questions/16534728/please-explain-the-produces-annotation-in-cdi. Like @AVolpe..mentioned If you use `producer method ` to create `EntityManager` and `Inject` in other classes you don't need to look for all the places to change name of the `@ PersistenceContext` when you change name in the `persistence.xml` – SRy Nov 04 '13 at 21:06
  • I have the same problem, and the only way to make this work is changing the EJB to Stateful, why is that? I need to make it work with Stateless EJBs. – ps0604 Jan 17 '19 at 05:57