Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use application configuration File in SpringBoot

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/03 Report--

Now we know that we can customize the default settings made by jar for us by providing a series of parameters when running SpingBoot. What should I do then?

SpringBoot provides special property profiles and configuration interfaces.

The default property configuration file loaded by SpringBoot is called application, just like you do with the Spring framework, there is also a global configuration file.

You can create an application.properties in the resource directory and write the SpringBoot attribute that needs to be customized into it

For example, if you want to modify the default port started by SpringBoot, add the server.port attribute; to modify the default port of redis, you can add the spring.redis attribute; to modify the connection address of rabbitmq, add the spring.rabbitmq.host attribute, like this:

After restarting the project, you can see that the default port has been changed to 8081. This is the same as using the startup command java-jar, except that it 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 content that needs to be set up in the actual project is often many, even complicated.

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

And the role of application.properties is much more than that.

It also makes it easy to distinguish and manage the configuration of different environments. Developers often switch between local, test, or even production environments to better develop or troubleshoot problems. In the past, we might have switched between different environments by commenting out part of it and writing another part of it in application.properties. Now, however, there is a more elegant way.

You can distinguish different project environments by adding different suffix names after application, instead of mixing 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. The configuration of different environments is clear at a glance through different suffix names.

Finally, add spring.profiles.active=dev to the root configuration file, that is, application.properties, to specify which configuration to enable. For example, the dev here corresponds to application-dev.properties, which is the agreed development environment configuration.

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

The answer is that the spring.profiles.active corresponds to which takes effect. Unless there are no properties defined in that configuration file, it will be overridden by the definition in application.properties. If not in both files, the default setting of springboot will be used.

In addition, incidentally, the springboot configuration file also has a yaml format. For example, the above application.properties can be named application.yml, which has exactly the same effect, except that the yaml file uses a tree structure to write properties, like this:

When both application.properties and application.yml exist in the project, it is application.properties that plays a role. In the same directory, properties configuration priority is higher than YAML configuration priority.

Use suggestion

1. Try to unify the configuration file in one format, and try not to mix the two formats.

2. Although properties is traditional and easy to use, it is officially recommended to use yaml format because the tree structure is more readable, which is the trend of many programming languages. 3. In general, we will put it under the resource root directory or under the resource/config directory. In this case, the configuration file in resource/config is the highest priority, which is also the official recommended method. Therefore, it is recommended that you put the configuration file of springboot itself in the resource/config directory.

Finally, attach a complete list of configuration parameters, which default settings need to be modified, find them against this list, and then overwrite the default property interface in the application file. Many of these properties of springboot can be customized using these features and parameters by adding related starter to the pom.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report