Joseph Gagnon
Joseph Gagnon

Reputation: 2135

Spring Boot configuration overrides are being ignored

I have written a number of services that communicate via REST API calls. The services can be configured to use HTTP or HTTPS. Any given client has security configuration that defines the connection to a server. "Default" configuration properties are set by values in application.yml and this has worked well up to this point.

I've come to realize, however, that this does not work for a more realistic situation. The problem is that I'm attempting to set specific arguments, like a server host/port when a client is launched, and the values I'm setting are being ignored.

An example:

Service A (client) will communicate with Service B (server) for some purpose. Service A has security configuration set up like the following:

@Configuration
@EnableConfigurationProperties({ClientConnectionProperties.class,
    SecurityCertificateProperties.class})
public class ClientSecurityConfiguration {
  ...
}

@ConfigurationProperties("client.connection")
public class ClientConnectionProperties {
  ...
}

@ConfigurationProperties("server.ssl")
public class SecurityCertificateProperties {
  ...
}

application.yml has defnitions for the various properties that are assigned to values in the configuration objects.

client:
  connection:
    scheme: https
    host: localhost
    port: 8443

server:
  port: 7081
  ssl:
    enabled: true
    protocol: TLS
    trust-store-type: JKS
    trust-store: classpath:server.truststore
    trust-store-password: <password>
    key-store-type: JKS
    key-store: classpath:server.keystore
    key-store-password: <password>

This is all well and good when both client and server are running on localhost. However, I'm testing running them where they reside on different hosts, so I need to override client.connection.host from localhost to the actual host. I do this by specifying -Dclient.connection.host=<host> as a VM argument when launching the client. Unfortunately, this does not work. The client ends up trying to connect to localhost and fails (for obvious reasons).

How can I get my override values set into these configuration items? Is there a way to defer or delay the "default" loading so that my values take effect? Is there some other technique to use to get them in there?

Upvotes: 0

Views: 630

Answers (1)

Pieterjan Deconinck
Pieterjan Deconinck

Reputation: 552

You can override Spring Boot properties by using Spring arguments instead of JVM arguments.

Example:

java -jar your-app.jar --client.connection.host=<host>

Related question: https://stackoverflow.com/a/37053004/12355118

Upvotes: 1

Related Questions