开发者

Using the instance factory method to create prototype beans dynamically

开发者 https://www.devze.com 2023-02-24 21:31 出处:网络
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 d

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!


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号