Alef:
The selector in my demo scenario is just a String, it comes from a db table.
While beginning to code a new FactoryBean subclass I stumbled
on something that already does something close to what I was discussing. Spring, what a great season!
ObjectFactoryCreatingFactoryBean
See: http://www.springframework.org/docs/...ctoryBean.html
Here are the changes:
Code:
<beans>
<!-- "FactoryBean which returns a value which is an ObjectFactory that returns a bean from the BeanFactory" -->
<bean id="kit" class="org.springframework.beans.factory.config.ObjectFactoryCreatingFactoryBean">
<property name="targetBeanName">
<!-- invoke the dialog bean to get the desired bean name -->
<bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="targetObject"><ref local="dialog"/></property>
<property name="targetMethod"><value>getOption</value></property>
</bean>
</property>
</bean>
<!-- DB stub; returns a bean name that user selects in a Swing dialog. -->
<bean id="dialog" class="proto.KitPromptDialog" singleton="false"> </bean>
<bean id="kitOne" class="proto.OptionOneKit" singleton="true">
<property name="msg"><value>actualOne</value></property>
</bean>
<bean id="kitTwo" class="proto.OptionTwoKit" singleton="true">
<property name="msg"><value>actualTwo</value></property>
</bean>
</beans>
And here is the demo driver:
Code:
public static void main( String[] args )
{
ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
// access the container, in a real app, use DI.
ObjectFactory factory = (ObjectFactory) ctx.getBean("kit");
IKit kit = (IKit) factory.getObject();
System.out.println("kit is: " + kit);
System.exit(0);
}
---- j. betancourt