jpganz18 jpganz18 - 2 years ago 113
Java Question

Is it possible to set properties for a Spring Boot application from a bean?

I was wondering if there is any way to do this.

I want to hardcode a property (I know might not be the best), let's say I want to run my application always on port XXX or any other configuration without using a


Is there any way I can do this from the main? or a configuration bean?


Answer Source

Take a look at this for how to configure a port: Spring Boot - how to configure port

Relevant code is this:

public class ServletConfig {
    public EmbeddedServletContainerCustomizer containerCustomizer() {
      return (container -> {

In general, most properties that can be configured via can also be configured through a Java bean. But, I would suggest using if you can. It allows you to change properties, without having to change source code.


Some other code from the posted link you might find useful:

HashMap<String, Object> props = new HashMap<>();
props.put("server.port", 9999);

new SpringApplicationBuilder()
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download