Announcement Announcement Module
No announcement yet.
Retrieving specific configured object instances generically Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Retrieving specific configured object instances generically

    I want to be able to retrieve transport-specific instances of connection objects to different business servers (call them A, B, ...). Call the transports (such as CORBA) X, Y, ... .

    There will be a bean declaration for each business server connection, for each transport type. I thus have bean declarations as follows:
    <bean id="XTransportForA" class="XTransport">
    		<properties specific to transport X for business server A connection that are set on XTransport...>
    <bean id="XTransportForB" class="XTransport">
    		<properties specific to transport X for business server B connection that are set on XTransport...>
    <bean id="YTransportForA" class="YTransport">
    		<properties specific to transport Y for business server A connection that are set on YTransport...>
    <bean id="YTransportForB" class="YTransport">
    		<properties specific to transport Y for business server B connection that are set on YTransport...>
    Is it possible, for example, to call a generic bean that is transport-independent such as
    <bean id="TransportForA" class="Transport"/>
    (where XTransport and YTransport extend Transport) and be returned the transport-specific bean XTransportForA if X was the specified transport (from a properties file)? i.e. can Spring return beans conditionally? Or do I need to write a special factory class?

  • #2
    So in essence: you want to have a bean that is created in the appcontext, based on some input argument?

    Object bean = appContext.getBean("transport","X");


    • #3
      Sort of...

      Ideally I'd like to pass the business server type in getBean(), but I want Spring to choose the correct bean instance based on this AND a setting for the transport, which Spring reads from a properties file (I want the transport type to be transparent to the code getting the bean). Is this possible?


      • #4
        One way I can think of is:
        0. Declare all the transports as you have already done in the OP.
        1. Write a BeanFactoryPostProcessor that reads the transport type from a properties file, and registers some transport-type-agnostic aliases for the transports. E.g., if the transport type, "X" or "Y", is read into variable transportType, you'd do:
        factory.registerAlias&#40; transportType + "TransportForA", "transportForA"&#41;;
        factory.registerAlias&#40; transportType + "TransportForB", "transportForB"&#41;;
        2. Now the code getting the bean can do:
        appContext.getBean&#40;"transportFor" + businessType&#41;;
        Oh well, maybe I have just made it overly complicated...


        • #5

          Interesting suggestion, would probably work, but would be a bit inelegant, when I think Spring should probably have some mechanism to do this properly. I also find it frustrating not being able to pass parameters when getting beans. Because I can't, I have to use a manual factory... correct me if I'm wrong.


          • #6
            Why can't you just have a bean which takes a map of names:transportBeans?

              <bean id="transportRegistry" class="TransportRegistry">
                <constructor-arg index="0">
                    <entry key="x"><ref bean="transportX"/></entry>
                    <entry key="y"><ref bean="transportY"/></entry>
            You transportRegistry would then have a "getTransport(final String key)" and return the corresponding transport?

            Have I missed something?


            • #7
              Check out this thread:

              It discusses the getBean with runtime arguments, which may be of relevance.
              Last edited by robyn; May 14th, 2006, 10:41 AM.


              • #8

                yatesco, thanks, I am actually using a PropertyPlaceholderConfigurer with BeanReferenceFactoryBean to retrieve the name of the transport, without a post-processor:

                <bean id="busserver1CorbaTransport" class="CorbaTransport">
                		<property name="orbFactory"><ref local="orbFactory" /></property>
                		<property name="corba_locator_ior" value="$&#123;corba_locator_ior&#125;" />
                		<property name="corba_service_name" value="$&#123;corba_service_name&#125;" />
                	<bean id="busserver1Transport" class="org.springframework.beans.factory.config.BeanReferenceFactoryBean">
                		<property name="targetBeanName" value="busserver1$&#123;transport.type&#125;Transport"/>
                I then simply get the bean "busserver1Transport", which will resolve to "busserver1CorbaTransport" if transport.type = Corba ;-)

                jbetancourt, thanks for the link, I'll check it out. Looks promising.


                • #9
                  Having separate context xml files for different transports (defining public beans under the same name) and then loading only the appropriate one at the startup sounds like a viable solution to me.