Spring 可变数量的实例

发布于 2024-11-26 06:31:28 字数 629 浏览 0 评论 0原文

我现在正在进入 Spring,我有一个问题。假设我们有一个接口IControl,并且有它的三个实现 - 例如ButtonLabelCheckbox。 用户将在运行时输入一个数字N,我应该创建特定类型的N控件(ButtonLabel 或复选框)。问题是我希望在程序运行之前配置此特定类型。例如,我在 spring.xml 中配置我想要按钮,然后运行程序,用户输入 10 并创建 10 按钮。

<beans>
   <bean id="controlCreator" class="org.company.ControlCreator">
       <property name="controlType" ref="?!?!?!?!?!?!"/>
   </bean>   
</beans>

我应该如何配置这个?

PS这只是我编的一个学习例子。

此致, 佩塔尔

I am getting into Spring now and I have a question. Let's assume we have an interface IControl and we have three implementations of it - e.g. Button, Label, Checkbox.
The user will enter a number N at runtime and I should create N controls of a specific type(Button, Label or Checkbox). The problem is I want this specific type to be configured before the program is run. For example I configure in my spring.xml that I want buttons, then the program is run, the user enters 10 and I create 10 buttons.

<beans>
   <bean id="controlCreator" class="org.company.ControlCreator">
       <property name="controlType" ref="?!?!?!?!?!?!"/>
   </bean>   
</beans>

How should I go configuring this?

P.S This is just a learning example I made up.

Best regards,
Petar

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

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

发布评论

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

评论(2

半仙 2024-12-03 06:31:28

首先,请允许我声明 Spring 无意这样做。 Spring 希望采用一种非侵入式的方法将组件连接在一起。它并不意味着用于在运行时生成对象。

正确的方法

正确的方法是设计一个工厂类。您可以在启动时使用 Spring 设置默认值,然后在运行时向工厂询问实例。

侵入式方法

如果您确实想使用 Spring,您可以通过向 applicationContext 询问控件的实例来进行侵入式操作。

默认情况下,所有 Spring bean 都是单例,但您可以将它们制作成原型。

免责声明:代码未经测试,但应该是这样的:

Applicationcontext.xml

<beans>
   <bean id="myButton" class="org.company.Control" scope="prototype">
       <property name="controlType" value="button" />
   </bean>   

   <bean id="controlCreator" class="org.company.ControlCreator" scope="singleton">
   </bean>   

</beans>

代码

public class controlCreator implements ApplicationContextAware {

  private ApplicationContext appContext;

  public Control createButton(){
    // since this bean is prototype, a new one will be created like this
    return getApplicationContext().getBean("myButton");
  }


  // ... getter and setter for applicationContext

}

非侵入式方法

如果您确实想使用 Spring,并且您确实想以非侵入式方式使用它,您将必须使用方法注入。将工厂方法注入到您的类中。

代码如下:

Applicationcontext.xml

<beans>
   <bean id="myButton" class="org.company.Control" scope="prototype">
       <property name="controlType" value="button" />
   </bean>   

   <bean id="controlCreator" class="org.company.ControlCreator" scope="singleton">
       <lookup-method bean="myButton" name="createButton"/>
   </bean>   

</beans>

代码

public class controlCreator implements ApplicationContextAware {

  private ApplicationContext appContext;

  public abstract Control createButton();


  // ... getter and setter for applicationContext

}

这将在调用查找方法时返回 bean。由于该 bean 是原型,因此它将创建一个新的 bean。

First of all, allow me to state that Spring is not intended to do this. Spring wants to an unintrusive method of wiring your components together. It's not meant to be used for spawning objects at runtime.

The correct approach

The correct approach would be to design a factory class. You can set the defaults of this using Spring at startup, then ask the factory for instances at runtime.

The intrusive apporach

If you really want to use Spring, you can do intrusively by asking the applicationContext for an instance of your control.

By default, all Spring beans are singletons, but you can make them into prototypes.

Disclaimer: code not tested, but should be something like this:

Applicationcontext.xml

<beans>
   <bean id="myButton" class="org.company.Control" scope="prototype">
       <property name="controlType" value="button" />
   </bean>   

   <bean id="controlCreator" class="org.company.ControlCreator" scope="singleton">
   </bean>   

</beans>

Code

public class controlCreator implements ApplicationContextAware {

  private ApplicationContext appContext;

  public Control createButton(){
    // since this bean is prototype, a new one will be created like this
    return getApplicationContext().getBean("myButton");
  }


  // ... getter and setter for applicationContext

}

The unintrusive approach

If you really want to use Spring, and you really want to use it unintrusively, you will have to use Method Injection. Inject a factory method into your class.

The code would be the following:

Applicationcontext.xml

<beans>
   <bean id="myButton" class="org.company.Control" scope="prototype">
       <property name="controlType" value="button" />
   </bean>   

   <bean id="controlCreator" class="org.company.ControlCreator" scope="singleton">
       <lookup-method bean="myButton" name="createButton"/>
   </bean>   

</beans>

Code

public class controlCreator implements ApplicationContextAware {

  private ApplicationContext appContext;

  public abstract Control createButton();


  // ... getter and setter for applicationContext

}

This will return the bean when the lookup method is called. Since the bean is prototype, it will create a new one.

人生戏 2024-12-03 06:31:28

您不能创建同一个 spring bean 的 N 个实例,因为 bean 是单例,并且 ref 应该引用此 bean 单例。

您可以做的是将 controlCreator 抽象化,然后使用创建不同控件的不同实现进行配置。

You cannot create N instances of same spring bean, as beans are singletons, and ref should reference this bean singleton.

What you can do is to make controlCreator abstract and then configure with different implementations which create different controls.

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