Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Property Placeholder for Imports/Bean Refs

Tags:

spring

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?

like image 471
Jay Blanton Avatar asked Mar 23 '11 18:03

Jay Blanton


1 Answers

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

like image 58
FrVaBe Avatar answered Oct 09 '22 21:10

FrVaBe