动态改变春豆 [英] dynamically change spring beans

查看:94
本文介绍了动态改变春豆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用java spring在运行时动态更改bean的属性?
我有一个bean mainView,它应该用作属性classclass1或class2。
此决定应基于属性文件进行,其中属性withSmartcard为Y或N。

how do I dynamically change the properties of a bean at runtime using java spring? I have a bean mainView, which should use as property "class" either "class1" or "class2". This decision should be made on base of an property-file, where the property "withSmartcard" is "Y" or "N".

ApplicationContext:

ApplicationContext:

<bean id="mainView"
    class="mainView">
    <property name="angebotsClient" ref="angebotsClient" />
    <property name="class" ref="class1" />
</bean>



<bean id="class1"
    class="class1">
    <constructor-arg ref="mainView" />
</bean>

<bean id="class2"
    class="class2">
    <constructor-arg ref="mainView" />
</bean>

PropertyFile:

PropertyFile:

withSmartcard = Y

withSmartcard=Y

推荐答案

使用PropertyPlaceHolder管理属性文件..

Use a PropertyPlaceHolder to manage your properties file ..

<bean id="myPropertyPlaceHolder" 
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  <description>The service properties file</description> 
  <property name="location" value="classpath:/some.where.MyApp.properties" /> 
  </bean>

并更改您的ref属性如下:

and change your ref attribute as follow :

<bean id="mainView"
    class="mainView">
    <property name="angebotsClient" ref="angebotsClient" />
    <property name="class" ref="${withSmartCardClassImplementation}" />
</bean>

在属性文件some.where.MyApp.properties中,添加名为 withSmartCardClassImplementation <的键/ strong>其中包含class1或class2(您选择)的值。

In your properties file some.where.MyApp.properties, add a key named withSmartCardClassImplementation which will have class1 or class2 (you choose) for value.

withSmartCardClassImplementation=class1

这篇关于动态改变春豆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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