Spring Session-Scoped Beans (Controllers) and References to Services, in Terms of Serialization

Spring session-scoped beans (controllers) and references to services, in terms of serialization

In this presentation (around 1:14) the speaker says 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 (on deserialization)

Spring: Serialization of session scoped bean

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.

Spring services should be serializable

As Mahendra already pointed out:

While deserializing the object, transient fields will reset to default value, so you might need to reinitialize it using if( transientField == null) { .. }

When you make a serializable bean, make sure that any reference you don't want to store to disk is marked as transient. This means that after the bean is loaded you re-set the members of the bean that were set as null.

for instance:

private transient SomeService localServiceRef;
SomeService getSomeService() {
if (localServiceRef == null) {
localServiceRef = SomeService.getInstance();
}
return localServiceRef;
}

Or you can store the id locally, and have a transient resolved object

private String objectId;
private transient SomeObject localObject;
SomeObject getLocalObject() {
if (localObject == null) {
localObject = SomeObjectFactory.getById(objectId);
}
return localObject;
}

Though this ofcourse depends on your actual code.

Personally I don't like working with beans that actually contain services and suchlike. I prefer having beans as value objects and services to be findable through other means.

Spring serializable proxy

The feature is called scoped proxy and can be triggered via annotation to the service, that will be injected to session scoped beans:

@Scope(proxyMode = ScopedProxyMode.INTERFACES) 

More details in question:

  • Spring session-scoped beans (controllers) and references to services, in terms of serialization

Serialization exception using spring-sessions

As @Selaron pointed out, the issue was non-transient spring beans on JSF controllers. Don't do that.

How do you serialize a Spring Bean (spring 3)

I think only session scoped beans are serialized automatically. There's a similar case here. So, all credits to Hans Westerbeek.

Spring3 deserialization - singleton injection

Try getting AutowireCapableBeanFactory through applicationContext.getAutowireCapableBeanFactory() and there are some methods like autowireBeanProperties, autowireBean and configureBean that 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 Repo in a proxy, that is serializable. It'll serialize with AdminBean and 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.

Spring: serializing objects with references to non-serializable beans

You have 2 options:

  1. Mark the dao as transient so it does not serialize.
  2. Serialize the dao yourself.

Java provides a means to serialize non-serializable objects. You will need to implement



private void writeObject(java.io.ObjectOutputStream out)
throws IOException
private void readObject(java.io.ObjectInputStream in)
throws IOException, ClassNotFoundException;

The Serializable interface includes a writeup of these methods. Here is a link to the docs (java 1.6) Serializable



Related Topics



Leave a reply



Submit