使用System属性更改spring bean别名 [英] Change spring bean alias with System property
问题描述
我试图弄清楚是否可以通过系统属性更改spring别名配置.
I try to figure out whether it's possible to change a spring alias configuration through a system property.
这是配置:
<beans>
<bean id="beanOne" ... />
<bean id="beanTwo" ... />
<bean id="beanThree" ... />
<alias name="beanOne" alias="beanToUse" />
<bean id="consumer" ...>
<constructor-arg ref="beanToUse" />
</bean>
</beans>
我希望能够使用JVM属性,例如用-Duse=beanThree
选择另一个bean作为别名.
I'd like to be able to use a JVM property e.g. with -Duse=beanThree
to select another bean for the alias.
不幸的是,使用直接解决方案<alias name="#{systemProperties.use}" alias="beanToUse" />
会引发NoSuchBeanDefinitionException
异常:(
Unfortunately using the straight forward solution <alias name="#{systemProperties.use}" alias="beanToUse" />
throws a NoSuchBeanDefinitionException
exception :(
有什么建议吗?
推荐答案
您尝试使用spring 3.1配置文件吗?
Did you try to use spring 3.1 profiles?
<beans>
<bean id="beanOne" ... />
<bean id="beanTwo" ... />
<bean id="beanThree" ... />
<beans profile="A">
<alias name="beanOne" alias="beanToUse" />
</beans>
<beans profile="B">
<alias name="beanTwo" alias="beanToUse" />
</beans>
<bean id="consumer" ...>
<constructor-arg ref="beanToUse" />
</bean>
</beans>
,然后通过系统属性-Dspring.profiles.active=A
选择.我没有在配置文件中尝试过别名,但是每个配置文件中都可以有替代的beanToUse
定义:
and choose through system property -Dspring.profiles.active=A
. I haven't tried aliases in profiles but you could just have alternative beanToUse
definitions in each profile:
<beans>
<beans profile="A">
<bean id="beanToUse" ... defined as beanOne ... />
</beans>
<beans profile="B">
<bean id="beanToUse" ... defined as beanTwo .../>
</beans>
<bean id="consumer" ...>
<constructor-arg ref="beanToUse" />
</bean>
</beans>
这篇关于使用System属性更改spring bean别名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!