i am developing a Webapplication with JSF2 and Spring3 and have a problem with deserialization.
I have some session-scoped beans defined like so:
@Controller(value = "admin")
@Scope(value = "session")
public class AdminBean implements Serializable {
...
also i have some singletons defined like so:
@Repository
public class Repo {
Singletons are injected into the session-beans like this
@Resource
private transient Repo repo;
After i added transient, my problems with serialization/deserialization went away. But now i have the problem that after deserialzing the dependencies (repo in this case) are null. I searched a lot on this problem and found some workarounds, but i still wonder what the best solution for this problem is?
It seems to me that using application-scoped beans in session-beans is a quite common case, isn’t there a clean solution for this? I came arround a solution with @Configurable, but do i really need some load-time-weaving stuff? The targets of the injection are already spring-managed, so it doesn’t make sense to me..
Please enlight me
update 2 years later: You CAN transparently inject session-scoped beans into application-scoped-beans (might not be a good idea in most cases though). I just had to set the proxyMode on @Scope accordingly.
Try getting
AutowireCapableBeanFactorythroughapplicationContext.getAutowireCapableBeanFactory()and there are some methods likeautowireBeanProperties,autowireBeanandconfigureBeanthat should be able to reconfigure your ban after deserialization. Choose a method best for you (one of them triggers post processing, others not etc..)Second idea is to wrap
Repoin a proxy, that is serializable. It’ll serialize withAdminBeanand deserialize. This proxy should hold ‘real’ transient reference and if it gets null after deserialization, it should lookup it from the Application Context.I heard that Spring 3 automaticaly wraps beans with such a proxy, but I’ve never managed to make it work.