12

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?

M. Deinum
  • 115,695
  • 22
  • 220
  • 224
ams
  • 60,316
  • 68
  • 200
  • 288
  • Not without re-implementing the `SessionScope` as provided by Spring. The session scoped beans are meant to be used by Spring, they aren't meant to store things in the session and you obtaining them in a page (or whatever) to display information. – M. Deinum Nov 28 '19 at 10:30

2 Answers2

4

I'd use:

@Component (value="mySpecialName")
@Scope (value="session")
Jonathan
  • 859
  • 7
  • 15
  • 1
    that does not work, the component ends up with the id of "mySpecialName" but in the session the attribute that the bean is stored under is still scopedTarget.mySpecialName I really want to control the attribute in the http session that the session scoped installed is stored under – ams May 14 '12 at 06:43
  • I've change my answer to include @Scope. In my code, I didn't need to set the proxyMode. – Jonathan May 14 '12 at 09:23
3

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.

M. Deinum
  • 115,695
  • 22
  • 220
  • 224