Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set the attribute name of a Spring session scoped bean?

Given a spring bean that is configured with session scope such as:

@Component
@Scope(proxyMode=ScopedProxyMode.TARGET_CLASS,value=WebApplicationContext.SCOPE_SESSION)
public class SomeBean {
}

Is there some way to control the name that Spring will store the bean under in the http session?

By default spring seams to use the session key scopedTarget.someBean is there anything I can add to the annotations to explicitly specify the attribute name in the Session?

like image 538
ams Avatar asked May 14 '12 03:05

ams


2 Answers

I'd use:

@Component (value="mySpecialName")
@Scope (value="session")
like image 190
Jonathan Avatar answered Nov 08 '22 05:11

Jonathan


You cannot. The scopedTarget part is hardcoded in the scoped proxy creation in Spring. So without rewriting parts of the framework that simply isn't possible.

The name is hardcoded in the ScopedProxyBeanDefinitionDecorator which delegates to the ScopedProxyUtils.

The fact that you use a scoped proxy is something internal for the framework. You probably want to use it to store something in the session and retrieve it in a page or something like that. Don't, just expose the regular bean which will delegate to the proper scoped instance.

like image 27
M. Deinum Avatar answered Nov 08 '22 05:11

M. Deinum