【发布时间】:2012-02-15 13:20:24
【问题描述】:
我有一个独立应用程序,该应用程序计算一个值(属性),然后启动一个 Spring 上下文。
我的问题是如何将该计算的属性添加到 spring 上下文中,以便我可以像从属性文件 (@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();
}
ApplicationContext.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"/>
这是一个 Spring 3.0 应用程序。
【问题讨论】: