Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I define System Properties within Spring Boot configuration files?

I have a single application.yml configuration file for my Spring Boot app that defines two profiles (as described in the documentation).

When the production profile is enabled, I would like to set the http.maxConnections system property to a custom value, e.g.

spring:     profiles:         active: dev --- spring:     profiles: dev --- spring:     profiles: production http:     maxConnections: 15 

But this doesn't actually set the system level property; it appears to just create an application-level property. I've verified this through both http://locahost:8080/env and a JMX Console when comparing launching by

java -jar -Dspring.profiles.active=production myapp.jar 

versus

java -Dhttp.maxConnections=15 myapp.jar 

I suppose I could create a bean that's @Conditional on the "production" profile that programmatically callsSystem.setProperty based on my application.yml-defined property, but is there a simpler way through configuration files alone?

like image 445
bdkosher Avatar asked Apr 27 '16 16:04

bdkosher


People also ask

Where do you define properties in Spring Boot application?

Spring Boot Framework comes with a built-in mechanism for application configuration using a file called application. properties. It is located inside the src/main/resources folder, as shown in the following figure.

Can we have both application properties and application Yml in Spring Boot?

Overview. A common practice in Spring Boot is using an external configuration to define our properties. This allows us to use the same application code in different environments. We can use properties files, YAML files, environment variables and command-line arguments.

What is the name of the default environment configuration file of Spring Boot application properties?

spring. config. location ( SPRING_CONFIG_LOCATION ) is the file to load (e.g. a classpath resource or a URL). A separate Environment property source is set up for this document and it can be overridden by system properties, environment variables or the command line.

Where are system properties stored in java?

properties file located in users->appdata->locallow->sun->java>deployment and also directly putting key=value in runtime parameter in java control panel but not working.


2 Answers

You may try.

@Profile("production") @Component public class ProductionPropertySetter {     @PostConstruct    public void setProperty() {       System.setProperty("http.maxConnections", "15");    }  } 
like image 75
TheKojuEffect Avatar answered Sep 23 '22 05:09

TheKojuEffect


I suppose I could create a bean that's @Conditional on the "production" profile that programmatically callsSystem.setProperty based on my application.yml-defined property, but is there a simpler way through configuration files alone?

I think that's your best bet here. Spring Boot does that itself in its LoggingSystem where various logging.* properties are mapped to System properties.

Note that you'll probably want to set the system properties as early as possible, probably as soon as the Environment is prepared. To do so, you can use an ApplicationListener that listens for the ApplicationEnvironmentPreparedEvent. Your ApplicationListener implementation should be registered via an entry in spring.factories.

like image 43
Andy Wilkinson Avatar answered Sep 25 '22 05:09

Andy Wilkinson