title: Apache Tiles - Unable to access beans in a custom ViewPreparer in Spring MVC
body: <p>I am using Spring 3.2 with Apache Tiles. I generated a lot of my service classes using Roo. I am trying a simple procedure where I inject a variable into the jsp templates. That part works fine, but I am stuck at the point where I need to reference a service bean.</p>
  
  <pre><code>@Component
  public class CustomViewPreparer implements ViewPreparer {
  
  @Autowired
  UserProfileService ups;
  
  @Override
  public void execute(TilesRequestContext tilesContext,
                      AttributeContext attributeContext) {
  
         Authentication a = SecurityContextHolder.getContext().getAuthentication();
         String name = a.getName(); //get logged in username
  
         UserProfile up = ups.findByUsername(name);
         //request.setAttribute("isLoggedIn", up!=null);
  
      }
  }
  </code></pre>
  
  <p>The UserProfileService "ups" is always null. I found this: <a href="http://forum.springsource.org/showthread.php?48950-ViewPreparer-is-triggered-before-Session-starts" rel="noreferrer">http://forum.springsource.org/showthread.php?48950-ViewPreparer-is-triggered-before-Session-starts</a></p>
  
  <p>But I don't understand the response. I can work around this by injecting my variable everytime I return a View, but I am curious how others have solved this problem.</p>