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

Time:2021-5-12

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”>
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.2.RELEASE</version>
<relativePath/> <!– lookup parent from repository –>
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

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

Customize configuration items in configuration file

myName=zhangsan

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

How to read custom configuration items

@Controller
@RequestMapping(“/properties”)
public class TestController {

@Value(“${myName}”)
private String myName;
@GetMapping(“/test”)
@ResponseBody
public String test() {
System.out.println(myName);
return myName;
}
}

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

Request:http://localhost:8080/properties/test

output

zhangsan

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

wangzhan.url=htt://abc.com
wangzhan.userName=root
wangzhan.password=123456

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

use

@RestController()
public class TestController {
@Resource
private WangzhanConfigBean wangzhanConfigBean;
@GetMapping(“/test”)
public String test() {
String url = wangzhanConfigBean.getUrl();
System.out.println(url);
String userName = wangzhanConfigBean.getUserName();
System.out.println(userName);
String password = wangzhanConfigBean.getPassword();
System.out.println(password);
return “ok”;
}
}

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

output

htt://abc.com
root
123456

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.

content

Custom profile

user.userName=zhangsan
user.age=22
user.id=1000001

How to read it?

@Configuration
@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

@RestController()
public class UserController {
@Resource
private UserConfig userConfig;

@GetMapping(“/user/{id}”)
public String queryUserNameById(@PathVariable(“id”) Integer id) {
System.out.println(userConfig.getUserName());
return “ok”;
}
}

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

output

zhangsan

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

spring.profiles.active=dev

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

15

Previous, next

Synchronize this article