java spring

How to add Properties to an Application Context

I have a Standalone Application, this application calculates a value (Property) and then starts a Spring Context.
My question is how can I add that calculated property to the spring context, so that I can use it like properties loaded from a property file (@Value("${myCalculatedProperty}"))?

To illustrate it a bit

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)


<bean id="propertyPlaceholderConfigurer"
<property name="locations" value="classpath:*.properties" />
<context:component-scan base-package=""/>

It is a Spring 3.0 Application.

In Spring 3.1 you can implement your own PropertySource, see: Spring 3.1 M1: Unified Property Management.

First, create your own PropertySource implementation:

private static class CustomPropertySource extends PropertySource<String> {
public CustomPropertySource() {super("custom");}
public String getProperty(String name) {
if (name.equals("myCalculatedProperty")) {
return magicFunction(); //you might cache it at will
return null;

Now add this PropertySource before refreshing the application context:

AbstractApplicationContext appContext =
new ClassPathXmlApplicationContext(
new String[] {"applicationContext.xml"}, false
new CustomPropertySource()

From now on you can reference your new property in Spring:

<bean class="com.example.Process">
<constructor-arg value="${myCalculatedProperty}"/>

Also works with annotations (remember to add <context:annotation-config/>):

private String magic;
public void init() {
System.out.println("Magic: " + magic);