Initializing a Spring Session Bean using an HttpRequest (using annotations)
<p>I am trying to build (or augment) a webapp that uses Spring beans, initialized with annotations. I have a bean that I need to initialize, but the bean needs an <code>HttpServletRequest</code> to properly initialize (it's a user identity bean).</p>

<p>I've browsed the documentation, and I've seen pieces of what I need, but the pieces don't seem to assemble into a complete solution.</p>

<p>Pseudo-example:</p>

<pre><code>@Configuration
public class MyConfig {
    @Bean
    public UserBean userBean() {
        // initialize with HttpRequest somehow?
    }
}

@Controller
public class MyController {
    @Resource
    UserBean userBean;

    @RequestMapping(value="/userId", method=RequestMethod.GET)
    public String getUserInfo() {
        // do stuff with the initialized user bean here
    }
}
</code></pre>

<p>Note that there are multiple controller classes, so I'm not sure which may be encountered first.</p>

<p>I see two possible things that could be done:</p>

<ol>
<li>Initialize the bean in a lazy way in the <code>MyConfig</code> class. But I'm not sure how to access the <code>HttpServletRequest</code> object.</li>
<li>Inject the bean into the session in the <code>getUserInfo()</code> method. But then every method in the class might need the same initialization logic, and I'm not sure how to inject the initialized UserBean into the session so that other controllers would access the same UserBean.</li>
</ol>

<p>Any help/recommendations/etc. would be appreciated</p>