3
votes

I have a Spring bean with scope session. This bean holds a reference to another singleton bean which is not serializable. What is the best approach if I want to serialize the session scoped bean?

The same question is already asked here: Spring session-scoped beans (controllers) and references to services, in terms of serialization

The accepted answer is that:

[...]this issue is resolved in spring 3.0 by providing a proxy of non-serializable beans, which obtains an instance from the current application context

As far as I understand the speaker in the linked video it should "just work". But in my case it doesn't! When I try to serialize my session scoped bean i get a NotSerializableException.

How can I solve this problem?

2

2 Answers

1
votes

You need to instruct Spring to create that proxy. In XML-based config, via <aop:scoped-proxy/> tag, in component-scan mode via annotation:

@Scope(proxyMode = ScopedProxyMode.INTERFACES) 

on your controller class.

-2
votes

You may mark singleton reference field as transient. Then check How to execute method after deserialization and load reference from ApplicationContext. Also, please provide stacktrace.

P.S.

It is not too good idea to use session passivation.