进口/豆类参考物业的占位符

Jay*_*ton 11 spring

我可以使用从property-placeholder加载的属性来使上下文导入动态吗?

<context:property-placeholder location="classpath*:/enterprise.properties"/>
<import resource="classpath*:/Fsb${jdbc.ctxType?:Jdbc}-context.xml"/>
Run Code Online (Sandbox Code Playgroud)

属性文件

jdbc.ctxType=JTA
Run Code Online (Sandbox Code Playgroud)

所以这样我就可以改变基于属性加载的上下文文件的类型.

另外,我可以做同样的事情来使bean引用名称动态吗?

<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}"
/>
Run Code Online (Sandbox Code Playgroud)

属性文件

personUidDataService.sib=Stub
Run Code Online (Sandbox Code Playgroud)

松鸦

--------------------更新ref的属性示例------------------------ -

我使用以下条目创建了一个属性文件:

addressLookupSearchService.sib=DaoMock
Run Code Online (Sandbox Code Playgroud)

然后我在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}"/>
Run Code Online (Sandbox Code Playgroud)

并且addressSearch $ {addressLookupSearchService.sib?:Dao}不起作用,它总是默认为addressSearchDao的bean id,即使我的属性说它应该设置为addressSearchDaoMock.

关于我做错了什么的任何想法?

FrV*_*aBe 13

这是一个类似的问题一个.

在创建 bean(property-placeholder)之前解析导入,因此您无法使用属性文件来定义要在import语句中使用的属性.在这种情况下,您必须将属性设置为system property(-Djdbc.ctxType=JTA)(请查看链接 - 段落注释).

但是在bean定义中使用属性文件属性可以正常工作 - 这就是它们的用途:-)

更新:自Spring 3.1以来,统一物业管理允许甚至在导入中使用属性(感谢@Jay Blanton在评论中提到这一点).

  • 3.1中的非常酷的功能 - 看起来它可以解决导入的属性替换... http://blog.springsource.com/2011/02/15/spring-3-1-m1-unified-property-management/ (2认同)