[original] have you ever used these configuration file methods of springboot?


followFull stack of Java back end technology”**

Reply to “interview” to get a full set of interview materials of large factories

There are several ways to operate configuration files in springboot. Have you used them all?

Compared with spring MVC, springboot has a lot less configuration files. However, in log development, there are still many configuration items that need to be configured.

There are several scenarios:

  • Custom configuration item
  • Encapsulating a configuration item as an object
  • Custom profile
  • Each environment corresponds to a set of configurations

Prepare for a spring boot project

<?xml version=”1.0″ encoding=”UTF-8″?>
<project xmlns=”http://maven.apache.org/POM/4.0.0″ xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”
xsi:schemaLocation=”http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/…d”>
<relativePath/> <!– lookup parent from repository –>
<description>Demo project for Spring Boot</description>

Configuration file application. Properties (can be application. YML or application. Yaml). Everyone’s preferences are different.

Customize configuration items in configuration file


package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);

How to read custom configuration items

public class TestController {

private String myName;
public String test() {
return myName;

Then start the main method of the project demoapplication. Java.




Encapsulating multiple configuration items as one object

Usually, for example, third-party information needs to be configured into the configuration file. For example, the URL, user name and password of a website.

Add configuration items to the application.properties file


@ConfigurationProperties(prefix = “wangzhan”)
public class WangzhanConfigBean {
private String url;
private String userName;
private String password;
//Get set ellipsis


public class TestController {
private WangzhanConfigBean wangzhanConfigBean;
public String test() {
String url = wangzhanConfigBean.getUrl();
String userName = wangzhanConfigBean.getUserName();
String password = wangzhanConfigBean.getPassword();
return “ok”;

Request address:http://localhost:8080/test



In this way, when we need to customize the configuration items, we can use the above two methods to solve the problem.

But there is another way, that is, if I want to customize the configuration file, I want to configure it in the custom configuration file. So what should we do?

How to read the content of custom configuration file

For example, I customize a configuration file test. Properties here.


Custom profile


How to read it?

@PropertySource(value = “classpath:test.properties”)
@ConfigurationProperties(prefix = “user”)
public class UserConfig {
private String userName;
private Integer age;
private Integer id;
//Omit get set

Using userconfig

public class UserController {
private UserConfig userConfig;

public String queryUserNameById(@PathVariable(“id”) Integer id) {
return “ok”;

Request address:http://localhost:8080/user/1



Is it very simple?

In the work, there will be development environment, test environment and online environment. These three are more conventional, and some have gray environment. At this time, you need to configure multiple sets of configurations. How can it be handled more conveniently?

Each environment has its own configuration file

[original] have you ever used these configuration file methods of springboot?

  • The dev port is changed to 8081
  • Test port changed to 8082
  • The prod port is changed to 8083

Now is the development environment, how to use our configuration file is dev?

Just empty the application. Properties file and write the content


Then start the project. Careful people will find that there is such a log in the startup log:

[original] have you ever used these configuration file methods of springboot?

This indicates that we are using the dev configuration file. Then let’s see if the port is the 8081 we mentioned earlier

[original] have you ever used these configuration file methods of springboot?

Check out the link to the custom profile above

[original] have you ever used these configuration file methods of springboot?

The previous port 8080 is inaccessible. So let’s switch to 8081

[original] have you ever used these configuration file methods of springboot?

success. And output

[original] have you ever used these configuration file methods of springboot?

OK。 It’s done.

Recommended reading

How to quickly build a spring boot foundation project?

Analysis of a more complete spring boot project

[original] have you ever used these configuration file methods of springboot?

Included in the topic: the actual combat series of spinning boot


Previous, next

Synchronize this article

Recommended Today

Looking for frustration 1.0

I believe you have a basic understanding of trust in yesterday’s article. Today we will give a complete introduction to trust. Why choose rust It’s a language that gives everyone the ability to build reliable and efficient software. You can’t write unsafe code here (unsafe block is not in the scope of discussion). Most of […]