foreword
In the first chapter, we briefly learned what Springboot is and its features, and learned about it through a helloworld case. Next, let's take a look at how springboot's configuration file takes effect.
1. Create a Maven project and import the required dependencies
The Maven directory structure is as follows:
The pom.xml file is configured as follows:
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>springboot</groupId>
<artifactId>2_springboot_properties</artifactId>
<version>0.0.1-SNAPSHOT</version>
<!-- Spring Boot 启动父依赖 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.1.RELEASE</version>
</parent>
<dependencies>
<!-- Spring Boot Web 依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Spring Boot Test 依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- Junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test</artifactId>
<version>1.4.2.RELEASE</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
2. Create application-dev.properties development environment
## dev 开发环境
userq.name=惘
userq.age=20
userq.desc=dev: ${userq.name} : ${userq.age}
3. Create application-prod.properties production environment
## prop 生成环境
userq.name=顾
userq.age=19
userq.desc=prop: ${userq.name} : ${userq.age}
4. Create the application.properties configuration file
# Spring Profiles Active
spring.profiles.active=prop
5. Create User.java
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix="userq")
public class UserProperties {
private String name;
private Integer age;
private String desc;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
@Override
public String toString() {
return "UserProperties [name=" + name + ", age=" + age + ", desc=" + desc + "]";
}
}
6. Create Test.java
import java.io.UnsupportedEncodingException;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import com.wzw.springboot.properties.UserProperties;
@RunWith(SpringRunner.class)
@SpringBootTest
public class UserPropertiesTest {
private static final Logger LOGGER = LoggerFactory.getLogger(UserPropertiesTest.class);
@Autowired
private UserProperties userProperties;
@Test
public void fun1() throws UnsupportedEncodingException {
LOGGER.info("" + new String(userProperties.toString().getBytes("iso8859-1"), "utf-8"));
}
}
7. Run fun1()
Console print
UserProperties [name=Gu, age=19, desc=prop:Gu: 19]
Note: If in application-dev.properties or application-prop.properties
user.name=惘
user.age=19
user.desc=prop: ${user.name} : ${user.age}
The result of the final test will be
UserProperties [name=Administrator, age=20, desc=dev: Administrator : 20]
This is because user.name will automatically find the user name of the computer.
8. .yml file
- YAML is a language specially used to write configuration files, which is very concise and powerful, far more convenient than JSON format.
- YAML is easy for people to read.
- YAML data is portable between programming languages.
- YAML is expressive and extensible.
- YAML is easy to implement and use.
- Use space to indent to indicate layering. Different numbers of spaces can be used for indentation between different levels, but elements of the same layer must be left-aligned, that is, the number of spaces in front of them is the same (Tab cannot be used, and the number of Spaces corresponding to each system Tab may be different. , resulting in level confusion)
- '#' means comments, only single-line comments, from the beginning of # to the end of the line
9. Create application.yml file
## 书信息
book:
name: 《SpringBoot 入门》
writer: 惘
10. Create Book entity class
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix = "book")
public class BookComponent {
private String name;
private String writer;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getWriter() {
return writer;
}
public void setWriter(String writer) {
this.writer = writer;
}
@Override
public String toString() {
return "BookComponent [name=" + name + ", writer=" + writer + "]";
}
}
11. Write a test class and run fun1()
import java.io.UnsupportedEncodingException;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import com.wzw.springboot.properties.BookComponent;
@RunWith(SpringRunner.class)
@SpringBootTest
public class BookPropertiesTest {
private static final Logger LOGGER = LoggerFactory.getLogger(UserPropertiesTest.class);
@Autowired
private BookComponent bookComponent;
@Test
public void fun1() throws UnsupportedEncodingException {
LOGGER.info("====================" + bookComponent.toString());
}
}
Running result:
====================BookComponent [name="SpringBoot Introduction", writer=擘]