Spring 可变数量的实例
我现在正在进入 Spring,我有一个问题。假设我们有一个接口IControl
,并且有它的三个实现 - 例如Button
、Label
、Checkbox
。 用户将在运行时输入一个数字N
,我应该创建特定类型的N
控件(Button
,Label 或
复选框
)。问题是我希望在程序运行之前配置此特定类型。例如,我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,请允许我声明 Spring 无意这样做。 Spring 希望采用一种非侵入式的方法将组件连接在一起。它并不意味着用于在运行时生成对象。
正确的方法
正确的方法是设计一个工厂类。您可以在启动时使用 Spring 设置默认值,然后在运行时向工厂询问实例。
侵入式方法
如果您确实想使用 Spring,您可以通过向 applicationContext 询问控件的实例来进行侵入式操作。
默认情况下,所有 Spring bean 都是单例,但您可以将它们制作成原型。
免责声明:代码未经测试,但应该是这样的:
Applicationcontext.xml
代码
非侵入式方法
如果您确实想使用 Spring,并且您确实想以非侵入式方式使用它,您将必须使用方法注入。将工厂方法注入到您的类中。
代码如下:
Applicationcontext.xml
代码
这将在调用查找方法时返回 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
Code
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
Code
This will return the bean when the lookup method is called. Since the bean is prototype, it will create a new one.
您不能创建同一个 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.