EuanMilton EuanMilton - 1 year ago 336
Java Question

Spring Boot command line property not overriding property defined in

I have created a Spring Boot app which uses a legacy library. This legacy library defines a number of Spring Beans in XML. One of which take in a property value as a constructor argument:

<beans xmlns="" xmlns:xsi=""

<bean id="myBean" class="com.em.MyBean">
<constructor-arg name="url" value="${my.url}"/>

In my Spring Boot app, I have an
which defines this property as follows:


I use the Maven Spring Boot plugin to run my app locally as follows:

mvn spring-boot:run

And the property value is injected into the bean as expected.

If I try and override the
property on the command line like this:

mvn spring-boot:run -Dmy.url=

The overriden value is not used and instead, the value inside
is used.

According to the Spring Boot docs, values from the command line should be picked up as the first priority: That does not appear to be the case here because if I remove the property from
then the value passed in on the command line is used so it is not a case of the command line value being ignored altogether. It seems like the
value is overriding the command line value.

Does anyone have any ideas as to what is going on?

Answer Source

I eventually resolved this by changing the way in which the beans from the legacy library were defined for the Spring Boot application. Rather than using the applicationContext.xml of the legacy application where the beans were defined, I added them to as @Beans in my configuration class. This resolved the issue.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download