How can I pass VM args to applicationContext in spring

2.9k Views Asked by At

How can I pass vm args (java -Dport = 5) to the application context?

I want to configure the port dynamically by the user, using the application context.

2

There are 2 best solutions below

1
On

In case you trying to access the VM arguments in spring application context, then just add the below bean definition in context file:

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
  <property name="ignoreUnresolvablePlaceholders" value="true"></property>
</bean> 

And access the VM args as ${vm_property}.

e.g if you passed the –Dport=5, which can be accessed as

<bean class="com.testing.test.MyClass">  
  <property name="myProperty" value="${port}"></property>  
</bean>
0
On

I think There is only one way in spring using injection. and get the property value from any property file