Spring has many different ways of creating beans, but is it possible to create a bean by deserializing a resource?
My application has a number of Components, and each manipulates a certain type of data. During test, the data object is instantiated directly and set directly on the component, e.g. component.setData(someDataObject). At runtime, the data is available as a serialized object and read in from the serialized stream by the component (the stream is passed in as a Resource from the Spring context.)
Rather than having each component explicitly deserialize it's data from the stream, it would be more consistent and flexible to have Spring deserialize the data object from a resource and set it on the target component. That way, components are isolated from where their data is coming from.
Is there a DeserializerFactoryBean or something similar?
EDIT:
Here's some sample code to hopefully clarify:
public class ComponentData implements Externalizable
{
// a complex data structure, behind a simpler interface
}
public class Component
{
private ComponenttData componentData;
public Component(ComponentData data)
{
componentData = data;
}
// other methods that operate using the data
// factory method to create from serialized data
static Component createFromResource(Resource resource)
{
return new Component(deserialize(resource));
}
}
There are many types of component, and each component type is instantated several times with different data instances.
In test, Components and their ComponentData instnaces are constructed in code. In production, a spring-bean with a "factory-method" attribute is used to invoke the static Componnet.createFromResource method, which deserializes the data from the resource. There are many types of components, and each one has the same static method to construct from deserialized data. This in itself seems bad because of the repetition. It also seems odd to me that Component construction in test and production are not the same. If the deserialization can be moved into the Spring context, the static methods on Components can be removed, and all dependenty injection is then done by Spring rather than having to code this as a special case.
E.g. I imagine something like
<bean name="componentData" class="DeserializingFactoryBean">
<constructor-arg value="/data/componentData1.dat"/> <!-- resource -->
</bean>
<bean name="component" class="Component">
<constructor-arg ref="componentData"/>
</bean>
When I originally posted, I thought that this might exist, but I that I might have missed in in the vast spring javadocs. It seems from the initial responses Spring does not have a deserializing factory bean.
If a DeserializingFactoryBean is not the right approach, what alternatives exist?