Results 1 to 4 of 4

Thread: Using xsl conditionals in applicationContext.xml

  1. #1

    Default Using xsl conditionals in applicationContext.xml

    What would it take to be able to write and process an applicationContext.xml file that looks something like this:

    Code:
    <bean id="propertyConfigurer"/>
    <bean id="perfTunedBeanA"/>
    <bean id="perfTunedBeanB"/>
    <bean id="itsComplicated">
     <xsl:choose>
      <xsl:when test="${somePropertyConfigurerProperty} &gt; 10">
       <property name="useMe" ref="perfTunedBeanA" />
      </xsl:when>
      <xsl:otherwise>
       <property name="useMe" ref="perfTunedBeanB" />
      </xsl:otherwise>
      </xsl:choose>
    </bean>
    I'm looking for a marriage of the conditionals offered by XSLT and the property values that any user may configure ... to result in how the applicationContext is ultimately used.

    This may make it more difficult to figure out what-is & what-is-not configured in a given appContext when looking at the deployment ... but its a decent trade off when I consider the fact that it keeps the users from mucking about in the xml file and allows me to tweak what's needed by just having the users specify their configuration via a properties file.

    I'm not sure if extending GenericApplicationContext or BeanDefinitionDocumentReader or something else is the right way to get started on something like this ... so I would appreciate any pointers anyone can give on how to go about dissecting spring classes in order to implement such a feature?

    Thanks!

  2. #2
    Join Date
    Jul 2010
    Location
    Venice, Italy
    Posts
    709

    Default

    You can use SpEL + utilroperties to read from an external configuration file the ids of the beans to inject.

  3. #3

    Default

    Thanks Enrico, can you give a quick example please?

  4. #4
    Join Date
    Jul 2010
    Location
    Venice, Italy
    Posts
    709

    Default

    Say you have a properties file in /home/myName/myConfigDir called myProps.properties with following entry:
    Code:
    beanToUse=perfTunedBeanB
    Then with following configuration:
    Code:
    <context:property-placeholder location="file:/home/myName/myConfigDir/myProps.properties"/>
    <bean id="perfTunedBeanA" class="com.mypackage.AClass"/>
    <bean id="perfTunedBeanB" class="com.mypackage.BClass"/>
    <bean id="itsComplicated" class="com.mypackage.CClass">
        <property name="useMe" ref="${beanToUse}" />
    </bean>
    B bean would be injected. If you changed the prop file with beanToUse=perfTunedBeanA and then redeployed your app or refreshed the Spring application context then A bean would be injected.

Tags for this Thread

Posting Permissions

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