@Injection is not working for CDI bean

10,613

You must not create a CDI bean using new, nor use a constructor for any sort of initialization logic.

The reason behind this is that CDI beans (like EJBs, Spring beans, JSF beans) have an independent lifecycle and are managed by the relevant container. You cannot rely on the "traditional" understanding of when (and how often) new will be called. Use producers to create new beans, and use @PostConstruct for any logic to be performed after creation.

This should give you a good start with CDI. Feel free to post further questions :)

Share:
10,613
javaMS
Author by

javaMS

Updated on June 25, 2022

Comments

  • javaMS
    javaMS almost 2 years

    I have a CDI bean where I'm using @ConversationScoped. When I try to do an @Inject for the Conversation object, I get a NPE.

      @ConversationScoped
    @Named("customerbean")
    public class CustomerBean implements Serializable {
    
        @Inject
        private Conversation conversation;    
    
        private static final EntityManagerFactory emf = Persistence.createEntityManagerFactory("ba");
        private EntityManager em;
        private Customer customer;
        boolean disabled;    
    
        public CustomerBean() {
            beginConversation();
            customer = new Customer();
            em = emf.createEntityManager();
            disabled = false;
        }
    
        private void beginConversation() {
            if (this.conversation.isTransient()) {
                this.conversation.begin();
                return;
            }
            throw new IllegalStateException();
        }
    

    I have the beans.xml file (although empty) in the WEB-INF directory. The exception looks like this:

    INFO: Exception when handling error trying to reset the response.
    com.google.common.collect.ComputationException: java.lang.RuntimeException: java
    .lang.NullPointerException
            at com.google.common.collect.ComputingConcurrentHashMap$ComputingMapAdap
    ter.get(ComputingConcurrentHashMap.java:397)
            at org.jboss.weld.bean.proxy.ClientProxyProvider.getClientProxy(ClientPr
    oxyProvider.java:102)
            at org.jboss.weld.el.AbstractWeldELResolver.lookup(AbstractWeldELResolve
    r.java:115)
            at org.jboss.weld.el.AbstractWeldELResolver.getValue(AbstractWeldELResol
    ver.java:96)
            at org.jboss.weld.environment.servlet.util.ForwardingELResolver.getValue
    (ForwardingELResolver.java:49)
            at javax.el.CompositeELResolver.getValue(CompositeELResolver.java:67)
            at com.sun.faces.el.DemuxCompositeELResolver._getValue(DemuxCompositeELR
    esolver.java:176)
    
  • matbrgz
    matbrgz over 11 years
    Isn't this more like a comment?
  • Jan Groth
    Jan Groth over 11 years
    At least it wasn't intended to be like one. I'm referring to the fact that the OP has put logic into the constructor, which is completely wrong and lead to the assumption that he has probably not understood the basic principles of CDI...
  • javaMS
    javaMS over 11 years
    OK, I moved out the initialization logic to a postconstruct method. It seems to be initializing now. Thanks!