Hello,

Just revisiting the question of how to use CS to configure beans based on
something that is only known at runtime.  This has already been discussed in
http://www.mail-archive.com/[email protected]/msg00137.
html and
http://www.mail-archive.com/[email protected]/msg00090.
html.  My use case is almost identical to these two - do something different
based on a host header.

The two approaches discussed in these threads are:
1. Use a statically wired facade bean to get to the dynamic bean
2. Create a child bean factory on app startup and point it to the
appropriate XML config

The earlier thread dates from before factory-bean support, so I wonder if a
third method might have merit:

3. Use a factory bean to choose from a set of statically wired alternatives

This would look something like:

        <bean id="WidgetFactory" class="com.WidgetFactory" singleton="true">
                <property name="OZWidget"><ref bean="RedWidget" /></property>
                <property name="USWidget"><ref bean="BlueWidget" /></property>
        </bean>

        <bean id="Widget" factory-bean="WidgetFactory"
factory-method="getWidgetForCurrentHostHeader" singleton="true" />

        <bean id="RedWidget" class="com.Widget" singleton="true">
                <constructor-arg 
name="colour"><value>red</value></constructor-arg>
        </bean>

        <bean id="BlueWidget" class="com.Widget" singleton="true">
                <constructor-arg 
name="colour"><value>blue</value></constructor-arg>
        </bean>

In getWidgetForCurrentHostHeader:

if (CGI.SERVER_NAME is "widgetsite.com.au")
        return variables.OZWidget;
else
        return variables.USWidget;

I can then just inject "Widget" into any other bean that needs it.


This is a lot more config than the other alternatives, but wrt the app code
I think more transparent.  Thoughts?

Jaime Metcher





Reply via email to