本文最后更新于 621 天前,其中的信息可能已经有所发展或是发生改变。
构建基础项目
编写代码
io.jbnrz.demo.DemoApplication.java
package io.jbnrz.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);
}
}
io.jbnrz.demo.Controller.Controller.java
package io.jbnrz.demo.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class Controller {
@RequestMapping("/")
public String root(){
return "This is the root page";
}
@RequestMapping("/hello")
public String hello(){
return "This is the hello page";
}
}
配置文件
优先级
properties > yml > yaml
转义字符
单引号 ” 不转义
双引号 “” 转义
读取配置
以 properties 为例
application.properties
# 应用名称
spring.application.name=demo
# 应用服务 WEB 访问端口
server.port=8080
name=JBNRZ
website=https://jbnrz.github.io/
jbnrz.name=CRZJBN
jbnrz.website=https://jbnrz.netlify.app/
test=${name}
web[0]=https://jbnrz.github.io/
web[1]=https://jbnrz.netlify.app/
Controller.java
package io.jbnrz.demo.Controller;
import io.jbnrz.demo.env.Info;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class Controller {
@Value("${name}")
private String name;
@Value("${website}")
private String website;
@Value("${test}")
private String test;
@Value("${web[0]}")
private String web1;
@Value("${web[1]}")
private String web2;
@Autowired
private Environment env;
@Autowired
private Info info;
@RequestMapping("/")
public String root(){
return "This is the root page";
}
@RequestMapping("/hello")
public String hello(){
return "This is the hello page";
}
@RequestMapping("/info")
public String info(){
return this.name + "<br>" + this.website + "<br>" + this.test;
}
@RequestMapping("/web")
public String web(){
return this.web1 + "<br>" + this.web2;
}
@RequestMapping("/env")
public String env(){
return env.getProperty("name") + "<br>" + env.getProperty("website");
}
@RequestMapping("/info2")
public String info2(){
return info.getName() + "<br>" + info.getWebsite();
}
}
io.jbnrz.demo.env.Info.java
package io.jbnrz.demo.env;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix = "jbnrz")
/*
@ConfigurationProperties
-> private String name = JBNRZ;
-> private String website = https://jbnrz.github.io/;
@ConfigurationProperties(prefix = "jbnrz")
-> private String name = CRZJBN;
-> private String website = https://jbnrz.netlify.app/;
*/
public class Info {
private String name;
private String website;
// Alt + Insert 快捷生成
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getWebsite() {
return website;
}
public void setWebsite(String website) {
this.website = website;
}
@Override
public String toString() {
return "Info{" +
"name='" + name + '\'' +
", website='" + website + '\'' +
'}';
}
}
pom.xml
<?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/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.11</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>io.jbnrz</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>demo</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
server.servlet.context-path
- 当在 application.properties 中设置
server.servlet.context-path=/hello
- 则网站根目录从 http://127.0.0.1:8081/ 变为 http://127.0.0.1:8081/hello/
profile 配置
- application.properties
spring.application.name=demo-profile
server.port=8080
- application-dev.properties
# 开发环境
server.port=8081
- application-test.properties
# 测试环境
server.port=8082
- application-pro.properties
# 生产环境
server.port=8083
- 当 application.properties 中设置 spring.profiles.active=dev
o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8081 (http) with context path ''
- 未设置
o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
profile 激活方式
- 配置文件中设置:spring.profiles.activate=dev
- 虚拟机参数:VM options > -Dspring.profiles.activate=dev
- 命令行参数:java -jar xxx.jar –spring.profiles.activate=dev
内部配置加载顺序
application.properties < config/application.properties < /application.properties < /config/application.properties