Announcement Announcement Module
Collapse
No announcement yet.
Selecting one of two things Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Selecting one of two things

    I have a situation something like this:

    <bean id="type1" .../>
    <bean id="type2" .../>

    Then there is another bean that needs one or the other of the "type" beans (I show it with type 1):

    <bean id="middle" ...>
    <property name="a" ref="type1">
    </bean>

    Then there are two classes that want to get the "middle" bean from the context. They are parts of different applications or maybe an real application and a test.

    middle = context.getBean("middle");

    I'm trying to leave out all the stuff like how the middle bean's property is some interface and the two type beans implement it (or something like that).

    The question (finally) is caused by the fact that the two classes are different in that they each want the "middle" bean that they get to have different "type" parameters.

    Now, scatter the use of "type" beans all through a reasonable system. (Perhaps one is a datasource you get from jndi and the other is a datasource where you give it the url, user, password and driver class name.) I say "scatter" so someone won't say "Oh ... you just make two middle beans with different "types" and get the one you want."

    What I would prefer is putting something in the a property file for each application to pick the "type" needed but I could put some code in each app to make it pick the right "type".

    And there have got to be some more ways to do this. Spring is so darn full of options ...

    Thanks.

  • #2
    Lee,

    What I've found useful in this scenario is to have an intermediate bean that has a default setting - for use in the main application - and provide an override for other applications, or test scenarios.

    So, for example, let's say that your main application uses 'type1' - set up you main config file like this...

    Code:
    <bean id="type1" .../>
    <bean id="type2" .../>
    
    <bean id="actualType" parent="type1"/>
    
    <bean id="middle" ...>
    <property name="a" ref="actualType">
    </bean>
    Then, in your test environment, have and additional config file that is added to (the end of) the list of config file that you load...

    Code:
    <bean id="actualType" parent="type2"/>
    Job done!

    The same technique works just as well for applications that need to use something other than 'type1'

    Hope this makes sense.

    Comment


    • #3
      Placeholder and BeanFactory solution

      Nothing wrong with the previous solution, but it might be more robust to do this with a placeholder configurer and switch the target of the "middle" object at runtime with a properties file (or similar). E.g.

      Code:
      <bean id="middle" ...>
        <property name="a">
          <bean class="org.springframework.beans.factory.config.BeanReferenceFactoryBean">
              <property name="targetBeanName" value="${middle.type}"/>
          </bean>
        </property>
      </bean>
      The placeholder ${middle.type} is replaced at runtime with the value from your configuration file if you include a PropertyPlaceholderConfigurer in your bean factory.

      Comment


      • #4
        Originally posted by david_syer
        Nothing wrong with the previous solution, but it might be more robust to do this with a placeholder configurer and switch the target of the "middle" object at runtime with a properties file (or similar). E.g.

        Code:
        <bean id="middle" ...>
          <property name="a">
            <bean class="org.springframework.beans.factory.config.BeanReferenceFactoryBean">
                <property name="targetBeanName" value="${middle.type}"/>
            </bean>
          </property>
        </bean>
        The placeholder ${middle.type} is replaced at runtime with the value from your configuration file if you include a PropertyPlaceholderConfigurer in your bean factory.
        As the saying goes...

        "There's more than one way to skin a cat!"

        Comment

        Working...
        X