我需要在我的applicationContext.xml設置參數值接口IP地址如何從applicationContext.xml中的bean讀取值?
我讀這個設置從屬性文件我用它這種方式:
<bean id="hazelcastInterface" class="com.hazelcast.config.InterfacesConfig">
<property name="interfaces">
<list>
<value>${interface.ip_address}</value>
</list>
</property>
<property name="enabled" value="true" />
</bean>
現在我需要得到該值一個命令行參數。我使用Apache Commons CLI解析器,解析參數並從中創建自己的bean commandLineConf,並將其設置爲ApplicationContext。
ExternalBeanReferneceFactoryBean.setInstance("commandLineConf", conf);
beanFactory.registerBeanDefinition(
"commandLineConf",
BeanDefinitionBuilder.rootBeanDefinition(
ExternalBeanReferneceFactoryBean.class)
.getBeanDefinition());
GenericApplicationContext rootAppContext = new GenericApplicationContext(
beanFactory);
rootAppContext.refresh();
但我不知道如何從這個bean在applicationContext.xml中獲得價值。我嘗試了很多方法,例如但它對我不起作用。
<bean id="hazelcastInterface" class="com.hazelcast.config.InterfacesConfig">
<property name="interfaces">
<list>
<value>#{commandLineConf.ipAddress}</value>
</list>
</property>
<property name="enabled" value="true" />
</bean>
我在做什麼錯?
您是否嘗試過這樣的:http://stackoverflow.com/questions/132231/dealing-with-command-line-arguments-and-spring? –
是的,我的源代碼的一部分是從這個問題... – user2148736