Announcement Announcement Module
Collapse
No announcement yet.
Retrieving specific configured object instances generically Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • 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:
    Code:
    <bean id="XTransportForA" class="XTransport">
    		<properties specific to transport X for business server A connection that are set on XTransport...>
    	</bean>
    
    <bean id="XTransportForB" class="XTransport">
    		<properties specific to transport X for business server B connection that are set on XTransport...>
    	</bean>
    
    <bean id="YTransportForA" class="YTransport">
    		<properties specific to transport Y for business server A connection that are set on YTransport...>
    	</bean>
    
    <bean id="YTransportForB" class="YTransport">
    		<properties specific to transport Y for business server B connection that are set on YTransport...>
    	</bean>
    
    etc.
    Is it possible, for example, to call a generic bean that is transport-independent such as
    Code:
    <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");

    Comment


    • #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?

      Comment


      • #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:
        Code:
        factory.registerAlias&#40; transportType + "TransportForA", "transportForA"&#41;;
        factory.registerAlias&#40; transportType + "TransportForB", "transportForB"&#41;;
        2. Now the code getting the bean can do:
        Code:
        appContext.getBean&#40;"transportFor" + businessType&#41;;
        Oh well, maybe I have just made it overly complicated...

        Comment


        • #5
          Interesting

          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.

          Comment


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

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

            Have I missed something?

            Comment


            • #7
              Check out this thread: http://forum.springframework.org/showthread.php?t=12180

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

              Comment


              • #8
                Interesting

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

                Code:
                <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>
                	
                	<bean id="busserver1Transport" class="org.springframework.beans.factory.config.BeanReferenceFactoryBean">
                		<property name="targetBeanName" value="busserver1$&#123;transport.type&#125;Transport"/>
                	</bean>
                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.

                Comment


                • #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.

                  Comment

                  Working...
                  X