导入/Bean引用的属性占位符 [英] Property Placeholder for Imports/Bean Refs

查看:182
本文介绍了导入/Bean引用的属性占位符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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"/>

属性文件

jdbc.ctxType=JTA

这样,我可以根据属性更改加载的上下文文件的类型.

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

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

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}"
/>

属性文件

personUidDataService.sib=Stub

周杰伦

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

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

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

I created a property file with the following entry:

addressLookupSearchService.sib=DaoMock

然后我在Spring上下文文件中具有以下配置:

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}"/>

并且addressSearch $ {addressLookupSearchService.sib?:Dao}不起作用,它始终默认为 即使我的属性说应将其设置为addressSearchDaoMock,也要使用addressSearchDao的bean ID.

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?

推荐答案

这与一个类似的问题.

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

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

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

更新:从Spring 3.1开始,

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

这篇关于导入/Bean引用的属性占位符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆