We now know that we can customize the Settings SpingBoot makes for us by default by providing a set of parameters when we run the JAR. If we were to customize a lot of properties, in real development you would probably need to override hundreds of SpringBoot defaults, which would not be elegant or maintainable if written in a Java-JAR run command. So what?

SpringBoot provides a dedicated property profile and configuration interface.

The default SpringBoot property configuration file is called Application, and just like you would with the Spring framework, there is a global configuration file.

You can create application.properties in the Resource directory and add the SpringBoot properties you want to customize. For example, if you want to change the default SpringBoot port, add the server.port property. To change the default port of redis, add the spring.redis attribute. To change the rabbitMQ connection address, add the spring.rabbitmq.host property like this:

When you restart the project, you see that the default port has been changed to 8081. This is the same as running with the start command java-jar, but looks more maintainable. If you think there are only a few parameters, wouldn’t it be easier to write them directly in the startup command? The amount of stuff that needs to be set up in a real project is often quite large and even complex.

Therefore, it is important to define your own application.properties to manage the framework’s custom properties.

And there’s more to application.properties than that.

It also makes it easy to distinguish and manage configurations for different environments. Developers often have to switch between local environments, test environments, and even production environments for better development or troubleshooting. Previously, we might have switched between different environments by commenting out one part and writing another in application.properties. Now, however, there is a more elegant way.

You can add different suffix names after application to distinguish different project environments without having to mix them all in one configuration file. Like this:

Generally speaking, dev represents the development environment, test represents the test environment, and PROd represents the production environment. Different environment configurations can be easily identified by different suffix names.

Finally, add spring.profiles. Active =dev to the root configuration file, application.properties, to specify which configuration to enable, for example, dev corresponds to application-dev.properties, This is our agreed development environment configuration.

What if both application.properties and application-dev.properties have the same configuration properties?

The answer is that spring.profiles.active takes effect whichever it corresponds to, unless properties that are not defined in that configuration file are overwritten by those defined in application.properties, if not, the default springBoot Settings are used.

As a side note, springBoot configuration files also have a YAML format, such as the above application.properties can be named application.yml, which does exactly the same thing, except yamL files use a tree structure to write properties, like this:

When both Application.properties and Application.yml exist in a project, application.properties is in effect. In the same directory, the properties configuration takes precedence over the YAML configuration.

Use advice

1. Try to use configuration files in one format instead of mixing the two formats.

While traditional and useful, yamL is officially preferred because tree structures are more readable, which is a trend in many programming languages. (YamL writing syntax and specifications will be covered later)

In this case, the configuration file in the resource/config directory has the highest priority, which is officially recommended. Therefore, you are advised to save the SpringBoot configuration file in the resource/config directory.

Finally, attach a complete list of configuration parameters, which default Settings need to be modified, and then overwrite the default properties interface in the application file. Many of these springBoot properties require the addition of an associated starter in the POM to use these features and parameter customization.

Custom parameter list:

Docs. Spring. IO/spring – the boot…