如何向应用程序上下文添加属性
我有一个独立的应用程序,这个应用程序计算一个值(属性),然后启动一个弹簧上下文。我的问题是如何将计算出的属性添加到弹簧上下文中,以便我可以像从属性文件()加载的属性一样使用它?@Value("${myCalculatedProperty}")
为了说明一下
public static void main(final String[] args) {
String myCalculatedProperty = magicFunction();
AbstractApplicationContext appContext =
new ClassPathXmlApplicationContext("applicationContext.xml");
//How to add myCalculatedProperty to appContext (before starting the context)
appContext.getBean(Process.class).start();
}
应用上下文.xml:
<bean id="propertyPlaceholderConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations" value="classpath:*.properties" />
</bean>
<context:component-scan base-package="com.example.app"/>
这是一个弹簧3.0应用程序。