Results 1 to 9 of 9

Thread: Retrieving specific configured object instances generically

  1. #1
    Join Date
    Nov 2004
    Location
    Cape Town, South Africa
    Posts
    33

    Default 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. #2
    Join Date
    Nov 2004
    Location
    Hilversum - The Netherlands
    Posts
    1,054

    Default

    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. #3
    Join Date
    Nov 2004
    Location
    Cape Town, South Africa
    Posts
    33

    Default 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. #4
    Join Date
    Oct 2004
    Location
    Herndon, VA, US
    Posts
    648

    Default

    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...
    --Jing Xue

  5. #5
    Join Date
    Nov 2004
    Location
    Cape Town, South Africa
    Posts
    33

    Default 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.

  6. #6
    Join Date
    Aug 2004
    Posts
    1,905

    Default

    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?

  7. #7
    Join Date
    Aug 2004
    Location
    u.s.a
    Posts
    399

    Default

    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 at 10:41 AM.

  8. #8
    Join Date
    Nov 2004
    Location
    Cape Town, South Africa
    Posts
    33

    Default 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.

  9. #9
    Join Date
    Sep 2004
    Posts
    1,086

    Default

    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.

Similar Threads

  1. Replies: 2
    Last Post: Oct 10th, 2005, 05:12 PM
  2. Beandoc crashing (on its samples!)
    By aaime in forum Container
    Replies: 17
    Last Post: Oct 7th, 2005, 07:21 AM
  3. Spring container fails with no exception
    By naor in forum Container
    Replies: 9
    Last Post: Oct 1st, 2005, 03:39 PM
  4. EHCaching Hibernate
    By dencamel in forum Data
    Replies: 3
    Last Post: Sep 6th, 2005, 09:03 PM
  5. Loosing my SecureContext
    By sklakken in forum Security
    Replies: 3
    Last Post: Jul 21st, 2005, 01:44 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •