构建基础项目
编写代码
io.jbnrz.demo.DemoApplication.java
1 2 3 4 5 6 7 8 9 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 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
1 2 3 4 5 6 7 8 9 10 11 12 13 # 应用名称 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 package io.jbnrz.demo.env;import org.springframework.boot.context.properties.ConfigurationProperties;import org.springframework.stereotype.Component;@Component @ConfigurationProperties(prefix = "jbnrz") public class Info { private String name; private String website; 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 <?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 /> </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 中设置
1 server.servlet.context-path =/hello
则网站根目录从 http://127.0.0.1:8081/ 变为 http://127.0.0.1:8081/hello/
profile 配置
application.properties
1 2 spring.application.name=demo-profile server.port=8080
application-dev.properties
application-test.properties
application-pro.properties
当 application.properties 中设置 spring.profiles.active=dev
1 o.s .b .w .embedded .tomcat .TomcatWebServer : Tomcat started on port(s): 8081 (http) with context path ''
未设置
1 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
如果您喜欢此博客或发现它对您有用,则欢迎对此发表评论。 也欢迎您共享此博客,以便更多人可以参与。 如果博客中使用的图像侵犯了您的版权,请与作者联系以将其删除。 谢谢 !