使用实例工厂方法动态创建原型bean

发布于 2024-11-01 03:30:22 字数 1693 浏览 0 评论 0原文

我有一种情况,我想通过工厂对象动态创建一个对象,但该对象需要通过 spring 上下文创建,以允许自动装配依赖项。我知道有很多其他方法可以解决这个问题 - 例如使用服务定位器模式 - 但如果可能的话我想这样做。

想象一下我有两个对象:

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

我想创建一个工厂来执行以下操作:

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;
    }
}

我的 spring-context.xml 看起来像:

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

但是,这不起作用。只创建了一个内部对象,我希望它的行为就像它具有范围=“原型”。如果我将scope =“prototype”添加到bean定义中:

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

那么它似乎创建了许多innerObject,但它们没有正确连接。我的同事认为文档发现 here 暗示工厂 bean 仅用于初始化 bean,但我认为这并不明显。

如果有人能在这里澄清我的理解,甚至可能提出一种比我正在做的更好的通过布线对工厂模式进行建模的方法,我将不胜感激。

谢谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

萤火眠眠 2024-11-08 03:30:23

我认为你的意思是你有一个单例工厂,你希望它创建新的对象,你每次都需要一个新的对象并完全依赖注入。旧的方法是方法注入,您可以在上面链接到该方法。新的(可以说是更干净的方法)是使用作用域代理。您可以使用 注释常规配置但想法是你围绕 bean 创建一个代理(例如 InnerObject)。当您需要引用它时,spring 会自动为您提供一个插入了适当依赖项的新副本。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文