PDA

View Full Version : Bean constructor parameters



IvanLatysh
Sep 1st, 2004, 11:13 AM
Hi.
I am using Spring for my Swing application.
And I came across a small problem.
I have a bean than extends JDialog class.


<bean id="infoDialog" class="InfoDialog" singleton="false" init-method="initUI">
<constructor-arg index="0"><ref bean="mainWindow"/></constructor-arg>
</bean>

That works fine. But I need to instantiate this bean with a different constructor argument.
Basically I need to create a dialog and pass another dialog as constructor parameter and need to do so from my code, not from a config.
Something like this bean_factory.getBean("infoDialog", new Object[]{MyParentDialog})

How I can do it ?

Alef Arendsen
Sep 1st, 2004, 03:37 PM
At the moment, application contexts are kind of static. Once you configure them (and there are plenty of option to customize configuration), you're done. Work is being done reconfiguration at runtime, but it's not part of any current release.

You could use the FactoryBean.

If you need to programmatically create object (requiring more code than just calling a constructor and setting a couple of properties) use the FactoryBean. It's similar to the factory pattern and you can reference it like any other bean. Your original bean will still receive a Dialog (i.e. the Dialog created by the DialogFactoryBean).

Have you taken a look already at the Spring RCP project by the way?

Alef