Property Placeholder for Imports/Bean Refs

15,382

This is a similar question to this one.

Imports are resolved before bean (property-placeholder) creation, so you can not use the property file to define properties which you want to use in an import statement. In this case you have to set the property as system property (-Djdbc.ctxType=JTA) (have a look at the link - paragraph Note).

But using the property file properties in bean definitions works fine - that's what they are for :-)

Update: Since Spring 3.1 the Unified Property Management allows to use properties even in imports (thanks @Jay Blanton for mentioning this in the comments).

Share:
15,382

Related videos on Youtube

Jay Blanton
Author by

Jay Blanton

Updated on May 25, 2022

Comments

  • Jay Blanton
    Jay Blanton almost 2 years

    Can I use a property loaded from property-placeholder to make a context import dynamic?

    <context:property-placeholder location="classpath*:/enterprise.properties"/>
    <import resource="classpath*:/Fsb${jdbc.ctxType?:Jdbc}-context.xml"/>
    

    Properties File

    jdbc.ctxType=JTA
    

    So this way I could change the type of context file that is loaded based on a property.

    Also, can I do the same thing to make a bean ref name dynamic?

    <bean id="personBusinessService" class="com.foo.PersonBusinessServiceImpl"
              p:personUidDataService-ref="personUidDataService${personUidDataService.sib?:Api}" 
              p:identifierLookupSearchService-ref="identifierLookupSearchService${identifierLookupSearchService.sib?:Api}"  
              p:contactPointBusinessService-ref="contactPointBusinessService${contactPointBusinessService.sib?:Api}"
    />
    

    Properties File

    personUidDataService.sib=Stub
    

    Jay

    --------------------Update example of property for ref-------------------------

    I created a property file with the following entry:

    addressLookupSearchService.sib=DaoMock
    

    Then I have the following configuration in a Spring Context File:

    <context:property-placeholder location="classpath*:/simple.properties"/>
    
    <!-- EntityManager will be injected into DAO by JPA annotations -->
    <bean id="addressSearchDao" class="com.foo.AddressSearchDaoImpl"/>
    
    <bean id="addressSearchDaoMock" class="com.foo.MockAddressSearchDaoImpl"/>
    
    <bean id="addressLookupSearchService" class="com.foo.AddressLookupSearchServiceImpl"
        p:baseDao-ref="addressSearch${addressLookupSearchService.sib?:Dao}"/>
    

    And addressSearch${addressLookupSearchService.sib?:Dao} doesn't work, it always defaults to the bean id of addressSearchDao even if my property says it should set to addressSearchDaoMock.

    Any thoughts as to what I am doing wrong?

  • Jay Blanton
    Jay Blanton about 13 years
    Thanks for the suggestion...I am looking at the import information. As far as the bean reference using a property, I updated the question to show an example of what I am trying to do...and it doesn't seem to be working for me.
  • Jay Blanton
    Jay Blanton about 13 years
    Looks like I found the answer. I thought the syntax was ?: for default values. But the syntax that worked was, ${addressLookupSearchService.sib:Dao} without the question mark. Based on this JIRA - jira.springsource.org/browse/SPR-4785.
  • FrVaBe
    FrVaBe about 13 years
    Good that it works now. Your update question changed the focus of the question a bit - I did not notice your special problems (with using the default value) first.
  • Jay Blanton
    Jay Blanton about 13 years
    Very cool feature in 3.1 - which looks like it can solve the property replacement for imports...blog.springsource.com/2011/02/15/…
  • sibidiba
    sibidiba over 10 years