系列文章
第一节:创建SpringBoot项目并运行HelloWorld
Spring Boot 2.x 加载配置文件的时候会移除特殊字符并且还会将配置均用全小写的方式进行匹配和加载。
application.properties
com.rumenz.id-name=rumenz
com.rumenz.id_name=rumenz
com.rumenz.idName=rumenz
com.rumenz.idname=rumenz
测试
package com.rumenz.lession8.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
/**
* @className: RumenzController
* @description: TODO 类描述
* @author: 入门小站 rumenz.com
* @date: 2021/11/8
**/
@Controller
@RequestMapping("/rumenz")
public class RumenzController {
@Value("${com.rumenz.idname}")
private String comRumenzIdName;
@RequestMapping("/index")
@ResponseBody
public String index(){
return comRumenzIdName;
}
}
浏览器访问
http://127.0.0.1:8080/rumenz/index,返回rumenz以上4个配置项是等价的,推荐全小写中间用
-分割。
application.properties和application.yml文件可以在放在以下几个位置。
config子目录classpath:/config/classpath:/路径 | 参数值 |
|---|---|
/springboot-zhinan/lession8/config/application.properties | com.rumenz.level=1 |
/springboot-zhinan/lession8/application.properties | com.rumenz.level=2 |
/springboot-zhinan/lession8/src/main/resources/config/application.properties | com.rumenz.level=3 |
/springboot-zhinan/lession8/src/main/resources/application.properties | com.rumenz.level=4 |

application.properties中配置了com.rumenz.level
com.rumenz.level=1
测试
package com.rumenz.lession8.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
/**
* @className: RumenzController
* @description: TODO 类描述
* @author: 入门小站 rumenz.com
* @date: 2021/11/8
**/
@Controller
@RequestMapping("/rumenz")
public class RumenzController {
@Value("${com.rumenz.idname}")
private String comRumenzIdName;
@Value("${com.rumenz.level}")
private String comRumenzLevel;
@RequestMapping("/index")
@ResponseBody
public String index(){
return comRumenzIdName;
}
@RequestMapping("/index1")
@ResponseBody
public String index1(){
return comRumenzLevel;
}
}
使用
mvn clean package打包后,使用jar -jar lession8-0.0.1-SNAPSHOT.jar运行。如果上述4个地方都定义了application.properties,并且都配置了com.rumenz.level,当我们的业务里面需要用com.rumenz.level配置项。查找的优先级是:
application.properties中的com.rumenz.level,找到返回,否则下一步application.properties中的com.rumenz.level,找到返回否则下一步application.properties的com.rumenz.level,找到返回否则下一步application.properties的com.rumenz.level,找到就返回,找不到就报错,项目启动失败。springboot的
application.properties中可以配置一些参数,如端口号,账号,密码。如果我们想在运行的时候想临时修改运行的端口也是可以的。
java -jar lession8-0.0.1-SNAPSHOT.jar --server.port=9000
这样项目运行的端口就变成了
9000,--server.port=9000相当于在application.properties中配置server.port=9000
我们也可以在操作系统里面定义变量,然后通过
@Value注入到Spring容器中。
package com.rumenz.lession8.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
/**
* @className: RumenzController
* @description: TODO 类描述
* @author: 入门小站 rumenz.com
* @date: 2021/11/8
**/
@Controller
@RequestMapping("/rumenz")
public class RumenzController {
//读取系统环境变量
@Value("${PATH}")
private String path;
@RequestMapping("/index2")
@ResponseBody
public String index2(){
return path;
}
}
浏览器访问
http://127.0.0.1:8080/rumenz/index2,返回/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/go/bin:/usr/local/share/dotnet:~/.dotnet/tools:/usr/local/mysql/bin:/usr/local/mysql/bin
我们可以设置
VM arguments向Spring容器传递参数值。

image-20211109215812644

image-20211109215845066
测试
package com.rumenz.lession8.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
/**
* @className: RumenzController
* @description: TODO 类描述
* @author: 入门小站 rumenz.com
* @date: 2021/11/8
**/
@Controller
@RequestMapping("/rumenz")
public class RumenzController {
@Value("${spring.test.env}")
private String springTestEnv;
@RequestMapping("/index3")
@ResponseBody
public String index3(){
return springTestEnv;
}
}
浏览器访问
http://127.0.0.1:8080/rumenz/index3,返回test
VM argumentsjava -jar -Dspring.test.env=testvm lession8-0.0.1-SNAPSHOT.jar
浏览器访问
http://127.0.0.1:8080/rumenz/index3,返回testvm本小结源码地址: