Using the instance factory method to create prototype beans dynamically
Asked Answered
P

1

6

I have a situation where I would like to dynamically create an object through a factory object, but the object needs to be created through the spring context, to allow autowiring of dependencies. I know that there are lots of other ways that I can solve this problem - using a service locator pattern for example - but I'd like to do it this way if possible.

Imagine I have two objects:

class OuterObject {
    List<InnerObjectInterface> innerObjs;
    ...
}
class InnerObject implements InnerObjectInterface{
    @Autowired
    SomeDependency someDependency;
    ...
}

I want to create a factory that does something along the lines of:

class OuterObjectFactory {
    private innerObject = new InnerObject();

    public OuterObject construct(params){
         OuterObject o = new OuterObject();
         List<InnerObjectInterface> inners = new ArrayList<InnerObjectInterface>();
         ...
         for(some dynamic condition){
             ...
             inners.add(createInnerObject());
             ...
         }
    }
    public createInnerObject(){
         return innerObject;
    }
}

My spring-context.xml would looks something like:

<bean id="outerObjectFactory" class="path.OuterObjectFactory" />
<bean id="innerObject" class="path.InnerObject" factory-bean="outerObjectFactory" factory-method="createInnerObject" />

This however, doesn't work. Only one innerObject is ever created, where I want it to act like it has scope="prototype". If I add scope="prototype" to the bean definition:

<bean id="innerObject" class="path.InnerObject" factory-bean="outerObjectFactory" factory-method="createInnerObject" scope="prototype"/>

Then it seems to create many innerObjects, but they aren't correctly wired. My co-worker believes that the documentation found here implies that the factory bean is only used to initialize a bean, but I don't find that obvious.

I'd appreciate it if anyone could clear up my understanding here, and possibly even suggest a better way of modelling the factory pattern with wiring than what I am doing.

Thanks!

Pentode answered 13/4, 2011 at 21:8 Comment(0)
A
2

I think what you're saying is that you have a factory which is a singleton and you want it to create new objects of which you want a new one each time with full dependency injection. The old way of doing that was Method Injection which you link to above. The new (and arguably cleaner way) is to use a Scoped Proxy. You can either use annotations or regular config but the idea is that you create a proxy around the bean (e.g. the InnerObject). When ever you need a reference to it, spring will automatically provide you with a new copy with the appropriate dependencies inserted.

Alrich answered 13/4, 2011 at 21:36 Comment(1)
It turns out that this doesn't actually work for prototype beans. It did however lead me to method injection, which solves the problem - not quite as cleanly, but still nicely. Thanks doughPentode

© 2022 - 2024 — McMap. All rights reserved.