title: @ManagedProperty in a Spring managed bean is null
body: <p>I've some trouble with injecting one managedbean in another by defining a managedproperty. I'm googling and stackoverflowing now for 3 days, but with no result...</p>
  
  <p>I'm developing with eclipse 4.2 and deploying to an integrated Tomcat 7</p>
  
  <p>So, can anybody tell me, why my property is null?</p>
  
  <p><strong>pom.xml</strong></p>
  
  <pre><code>&lt;properties&gt;
      &lt;project.build.sourceEncoding&gt;UTF-8&lt;/project.build.sourceEncoding&gt;
          &lt;spring.version&gt;3.0.5.RELEASE&lt;/spring.version&gt;
          &lt;java.version&gt;1.6&lt;/java.version&gt;
      &lt;/properties&gt;    
  &lt;dependency&gt;
              &lt;groupId&gt;org.springframework&lt;/groupId&gt;
              &lt;artifactId&gt;spring-web&lt;/artifactId&gt;
              &lt;version&gt;${spring.version}&lt;/version&gt;
          &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework&lt;/groupId&gt;
          &lt;artifactId&gt;spring-expression&lt;/artifactId&gt;
          &lt;version&gt;${spring.version}&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework&lt;/groupId&gt;
          &lt;artifactId&gt;spring-orm&lt;/artifactId&gt;
          &lt;version&gt;${spring.version}&lt;/version&gt;
          &lt;type&gt;jar&lt;/type&gt;
          &lt;scope&gt;compile&lt;/scope&gt;
      &lt;/dependency&gt;
  </code></pre>
  
  <p><strong>web.xml</strong></p>
  
  <pre><code>    &lt;web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" 
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" 
      http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"&gt;
    &lt;context-param&gt;
      &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
      &lt;param-value&gt;/WEB-INF/applicationContext*.xml&lt;/param-value&gt;
    &lt;/context-param&gt;
    &lt;listener&gt;
      &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt;
    &lt;/listener&gt;
  </code></pre>
  
  <p>I have set the beans in applicationContext for scanning @Autowired annotation. (Yes, i tried it without beans in applicationContext, but ManagedProperty will not be set, too.)</p>
  
  <p><strong>applicationContext.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:context="http://www.springframework.org/schema/context"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
      xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
      http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"&gt;
  
  &lt;context:annotation-config /&gt;
  
  &lt;context:component-scan base-package="myPackage" /&gt;
  
  &lt;bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/&gt;
  
  &lt;bean class="myPackage.dao.UserDao" id="userDao" /&gt;
  &lt;bean class="myPackage.dao.WorldDao" id="worldDao" /&gt;
  &lt;bean class="myPackage.dao.BuildingTypeDao" id="buildingTypeDao" /&gt;
  &lt;bean class="myPackage.dao.BuffTypeDao" id="buffTypeDao" /&gt;
  &lt;bean class="myPackage.dao.ClanDao" id="clanDao" /&gt;
  
  &lt;bean class="myPackage.bean.MainBean" id="mainBean" /&gt;
  &lt;bean class="myPackage.bean.UserBean" id="userBean" /&gt;
  &lt;bean class="myPackage.bean.AdminBean" id="adminBean" /&gt;
  </code></pre>
  
  <p></p>
  
  <p><strong>MainBean</strong></p>
  
  <pre><code>package myPackage.bean;
  import java.io.Serializable;
  import javax.faces.bean.ManagedBean;
  import javax.faces.bean.SessionScoped;
  import org.slf4j.Logger;
  import org.slf4j.LoggerFactory;
  import myPackage.model.MainModel;
  
  @ManagedBean
  @SessionScoped
  public class MainBean implements Serializable {
  
  private static final long serialVersionUID = 1L;
  
  private static final Logger logger = LoggerFactory.getLogger(MainBean.class);
  
  private MainModel model;
  
  /**
   * @return the model
   */
  public MainModel getModel() {
      if (model == null) {
          model = new MainModel();
      }
      return model;
  }
  
  /**
   * @param model the model to set
   */
  public void setModel(MainModel model) {
      this.model = model;
      }
  }
  </code></pre>
  
  <p><strong>UserBean</strong></p>
  
  <pre><code>package myPackage.bean;
  
  import java.io.Serializable;
  
  import javax.faces.application.FacesMessage;
  import javax.faces.bean.ManagedBean;
  import javax.faces.bean.ManagedProperty;
  import javax.faces.bean.RequestScoped;
  import javax.faces.context.FacesContext;
  
  import org.slf4j.Logger;
  import org.slf4j.LoggerFactory;
  import org.springframework.beans.factory.annotation.Autowired;
  
  import myPackage.dao.UserDao;
  import myPackage.entity.User;
  
  @ManagedBean
  @RequestScoped
  public class UserBean implements Serializable {
  
  private static final long serialVersionUID = 1L;
  
  private static final Logger logger = LoggerFactory.getLogger(UserBean.class);
  
  @ManagedProperty(value="#{mainBean}")
  private MainBean mainBean;
  
  @Autowired
  private UserDao userDao;
  
  /**
   * @return the mainBean
   */
  public MainBean getMainBean() {
      return mainBean;
  }
  
  /**
   * @param mainBean the mainBean to set
   */
  public void setMainBean(MainBean mainBean) {
      this.mainBean = mainBean;
  }
  
  public String doLogin() {
      User user = userDao.getUserByUsernameAndPassword(getMainBean().getModel().getUser().getUsername(), getMainBean().getModel().getUser().getPassword());
      if (user != null) {
          getMainBean().getModel().setUser(user);
          logger.info("User '"+getMainBean().getModel().getUser().getUsername()+"' logged in");
          getMainBean().getModel().setSelectedTab(0);
      } else {
          FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"Login failed", "Username and/or password wrong!"));
          logger.warn("User '"+getMainBean().getModel().getUser().getUsername()+"' login failed");
      }
      return null;
  }
  </code></pre>
  
  <p><strong>UserDao</strong></p>
  
  <pre><code>package myPackage.dao;
  
  import java.util.List;
  
  import javax.persistence.EntityManager;
  import javax.persistence.PersistenceContext;
  import javax.persistence.Query;
  
  import org.springframework.stereotype.Repository;
  import org.springframework.transaction.annotation.Transactional;
  
  import myPackage.entity.User;
  
  @Repository
  public class UserDao {
  
  @PersistenceContext
  private EntityManager entityManager;
  
  @Transactional
  public void save(User user) {
      if (user.getId() == null) {
          entityManager.persist(user);    
      } else {
          entityManager.merge(user);
      }
  }
  
  @SuppressWarnings("unchecked")
  public List&lt;User&gt; list() {
      return entityManager.createQuery("select u from User u")
              .getResultList();
  }
  
  public User getUserByUsername(String username) {
      try {
          Query q = entityManager.createQuery("select u from User u where u.username = :username");
          q.setParameter("username", username);
          User u = (User) q.getSingleResult();
          return u;
      } catch (Exception e) {
          return null;
      }
  }
  
  public User getUserByUsernameAndPassword(String username, String password) {
      try {
          Query q = entityManager.createQuery("select u from User u where u.username = :username and u.password = :password");
          q.setParameter("username", username);
          q.setParameter("password", password);
          User u = (User) q.getSingleResult();
          return u;
      } catch (Exception e) {
          return null;
      }
  }
  
  @Transactional
  public User getUserById(Long id) {
      return entityManager.find(User.class, id);
  }
  
  @Transactional
  public void delete(User user) {
      entityManager.remove(user);
  }
  
  public void deleteById(Long id) {
      delete(getUserById(id));
  }
  
  }
  </code></pre>
  
  <p>And now the Exception...</p>
  
  <pre><code>Caused by: java.lang.NullPointerException
      at myPackage.bean.UserBean.doLogin(UserBean.java:47)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
      at java.lang.reflect.Method.invoke(Method.java:597)
  </code></pre>
  
  <p>Line 47:</p>
  
  <pre><code>    User user = userDao.getUserByUsernameAndPassword(getMainBean().getModel().getUser().getUsername(), getMainBean().getModel().getUser().getPassword());
  </code></pre>
  
  <p>Debugging shows that getMainBean() returns null.</p>
  
  <p>I'm open for suggests to improve my concept!</p>