views:

107

answers:

1

The following code is returning null:

AController.java

    private MyAppUser getMyAppUser(HttpSession session) {
        MyAppUser myAppUser = (MyAppUser) session.getAttribute("myAppUserManager");
        return myAppUser;
    }

I also have tried this:

AController.java

@Autowired
MyAppUser myAppUser;

Despite the fact that I have the following in my context:

<bean id="myAppUserManager" class="com.myapp.profile.MyAppUser" scope="session"/>

This doesn't make any sense to me, the "myAppUser" bean is a bean that absolutely can never be null, and I need to be able to reference it from controllers, I don't need it in services or repositories, just controllers, but it doesn't seem to be getting stored in the session, the use case is extremely simple, but I haven't been able to get to the bottom of what's wrong, or come up with a good workaround

+1  A: 

Session-scoped beans aren't available in the session like that. Spring manages them, and stores them in the session, but not in a way that you can manually fish them out.

If you want to use the scoped bean, you wire it into your other beans, like any other bean. There are some things to look out for, though - see here. Essentially, if you want to wire session-scoped bean A into bean B, then bean B must also be session-scoped (or request-scoped), unless you use scoped-proxies - see previous link for info on how to do that.

skaffman
The code I linked is in an annotated controller, so I can't link in the properties in configuration like the documentation you referenced does. In fact, I had already looked at that documentation. I need this object in every controller too, so I'm not going to convert it all to be XML based. I also tried autowiring the "myAppUser", that failed with an exception on deployment, is the controller not session or request scoped? I find that hard to believe.
walnutmon
@jboyd: Controllers are singleton-scoped by default, like any other bean. Also, you don't need to put the controllers into XML - only the scoped bean needs to be defined in XML, with `<aop:scoped-proxy/>` defined within it.
skaffman