title: @ManagedProperty null after @PostConstruct in JSF 2.2
body: <p>My specs: </p>
  
  <ul>
  <li>Dynamic Web Module 3.1 </li>
  <li>GlassFish Web Extensions 4.0</li>
  <li>Java 1.8</li>
  <li>JavaScript 1.0</li>
  <li>JavaServer Faces 2.2</li>
  <li>Server: glassfish-4.1.1</li>
  <li>OS: Win 10</li>
  <li>IDE: Version: Neon.2 Release (4.6.2)</li>
  </ul>
  
  <p>Please, note that I have researched this topic and found several related posts.</p>
  
  <p>e.g.</p>
  
  <p><a href="https://stackoverflow.com/questions/32658346/managedproperty-postconstruct-init-nullpointer">@ManagedProperty + @PostConstruct + init() = Nullpointer</a></p>
  
  <p><a href="https://stackoverflow.com/questions/6605069/managedproperty-injected-after-postconstruct?rq=1">@ManagedProperty injected AFTER @PostConstruct</a></p>
  
  <p>But neither of the proposed solutions worked for me or applied to my situation.
  I don't mix CDI and/or JSF and/or Spring. It's JSF 2.2 annotations only. </p>
  
  <p>I inject @ManagedProperty("#{country}") Country country; to my ChangeCountrySystemEventListener but the value of the @ManagedProperty country is null.
  I don't really see where the issue is. The Country constructor does get invoked.</p>
  
  <p>Any tips where the issues is?</p>
  
  <p>Here's my full code: </p>
  
  <p><strong>index.xhtml</strong></p>
  
  <pre><code>    &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
          "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
  
      &lt;html xmlns="http://www.w3.org/1999/xhtml"
          xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
          xmlns:h="http://xmlns.jcp.org/jsf/html"
          xmlns:f="http://xmlns.jcp.org/jsf/core"&gt;
      &lt;h:head&gt;
          &lt;title&gt;Title&lt;/title&gt;
      &lt;/h:head&gt;
      &lt;h:body&gt;
  
          &lt;h3&gt;&lt;h:outputText value="#{country.name}" /&gt; &lt;/h3&gt;
          &lt;h:form&gt;
  
              &lt;h:commandButton
                  id="changeCountryNameBtn"
                  value="Change"
                  action="result"
                  actionListener="#{appBean.changeCountryName}"
               /&gt;
  
          &lt;/h:form&gt;
      &lt;/h:body&gt;
      &lt;/html&gt;
  </code></pre>
  
  <p><strong>AppBean.java</strong> </p>
  
  <pre><code>    package com.test.beans;
  
      import javax.faces.application.Application;
      import javax.faces.bean.ManagedBean;
      import javax.faces.bean.SessionScoped;
      import javax.faces.context.FacesContext;
      import javax.faces.event.ActionEvent;
  
      @ManagedBean
      @SessionScoped
      public class AppBean {
  
          public void changeCountryName(ActionEvent ev) {
  
              FacesContext context = FacesContext.getCurrentInstance();
              Application app = context.getApplication();
              app.publishEvent(context, ChangeCountrySystemEvent.class, ev.getSource());
              System.out.println("&gt;&gt;&gt;&gt; AppBean.publishEvent(ChangeCountrySystemEvent) fired... " + ev.getSource());
  
          }
  
      }
  </code></pre>
  
  <p><strong>ChangeCountrySystemEvent.java</strong> </p>
  
  <pre><code>    package com.test.beans;
      import javax.faces.event.SystemEvent;
  
      public class ChangeCountrySystemEvent extends SystemEvent {
  
          private static final long serialVersionUID = -1587717461942271611L;
  
          public ChangeCountrySystemEvent(Object source) {
              super(source);
              System.out.println("&gt;&gt;&gt;&gt; ChangeCountrySystemEvent.class :: constructor invoked!");
          }
  
      }
  </code></pre>
  
  <p><strong>ChangeCountrySystemEventListener.java</strong></p>
  
  <pre><code>    package com.test.beans;
  
      import javax.faces.bean.ManagedProperty;
      import javax.faces.context.FacesContext;
      import javax.faces.event.SystemEvent;
      import javax.faces.event.SystemEventListener;
  
      public class ChangeCountrySystemEventListener implements SystemEventListener {
  
          @ManagedProperty("#{country}")
          Country country;
  
          // getters and setters
          public Country getCountry() {
              return country;
          }
  
          public void setCountry(Country country) {
              this.country = country;
          }
  
          public ChangeCountrySystemEventListener(FacesContext fc) {
              super();
              System.out.println("&gt;&gt;&gt;&gt; ChangeCountrySystemEventListener.class :: Listener constructor invoked!!!");
          }
  
          @Override
          public void processEvent(SystemEvent se) {
  
              if (country != null) {
                  country.setName("Sweden");
                  System.out.println("&gt;&gt;&gt;&gt; ChangeCountrySystemEventListener.class :: SYSTEM EVENT PROCESSED... &lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt; ");
              } else if (country == null) {
                  System.out.println("&gt;&gt;&gt;&gt; ChangeCountrySystemEventListener.class :: processEvent() &gt; country managed property is EMPTY !!!!");
              }
          }
  
          @Override
          public boolean isListenerForSource(Object source) {
              return true; // needs to be set to true, otherwise "processEvent" won't be called...
          }
  
      }
  </code></pre>
  
  <p><strong>Country.java</strong></p>
  
  <pre><code>    package com.test.beans;
  
      import javax.annotation.PostConstruct;
      import javax.faces.application.Application;
      import javax.faces.bean.ManagedBean;
      import javax.faces.bean.SessionScoped;
      import javax.faces.context.FacesContext;
  
      @ManagedBean(name = "country", eager = true)
      @SessionScoped
      public class Country {
  
          private String name = "Norway";
  
          public Country() {
              System.out.println("&gt;&gt;&gt;&gt; Country constructor called...");
          }
  
          public String getName() {
              return name;
          }
  
          public void setName(String name) {
              this.name = name;
          }
  
          @PostConstruct
          public void init() {
              FacesContext fc = FacesContext.getCurrentInstance();
              Application app = fc.getApplication();
              app.subscribeToEvent(ChangeCountrySystemEvent.class, new ChangeCountrySystemEventListener(fc));
              System.out.println("&gt;&gt;&gt;&gt; Country.class :: app.subscribeToEvent() called... ");
          }
      }
  </code></pre>
  
  <p><strong>Console output:</strong> </p>
  
  <pre><code>2017-04-02T21:49:51.392-0300|Info: JSF_TestProject was successfully deployed in 579 milliseconds.
  2017-04-02T21:49:52.251-0300|Info: &gt;&gt;&gt;&gt; Country constructor called...
  2017-04-02T21:49:52.277-0300|Info: &gt;&gt;&gt;&gt; ChangeCountrySystemEventListener.class :: Listener constructor invoked!!!
  2017-04-02T21:49:52.277-0300|Info: &gt;&gt;&gt;&gt; Country.class :: app.subscribeToEvent() called...
  2017-04-02T21:50:16.572-0300|Info: &gt;&gt;&gt;&gt; ChangeCountrySystemEvent.class :: constructor invoked!
  2017-04-02T21:50:16.572-0300|Info: &gt;&gt;&gt;&gt; ChangeCountrySystemEventListener.class :: processEvent() &gt; country managed property is EMPTY !!!!
  2017-04-02T21:50:16.572-0300|Info: &gt;&gt;&gt;&gt; AppBean.publishEvent(ChangeCountrySystemEvent) fired... javax.faces.component.html.HtmlCommandButton@424c250c
  </code></pre>