her~~llo,我是你们的好朋友Lyle,是名梦想成为计算机大佬的男人!
博客是为了记录自我的学习历程,加强记忆方便复习,如有不足之处还望多多包涵!非常欢迎大家的批评指正。
今天我们来学习SpringBoot的基础配置。
目录
SpringBoot提供了多种属性配置方式
- application. properties
server.port=80
- application.yml
server: port: 81
- application.yaml
server: port: 82
SpringBoot配置文件加载顺序(了解):
application.properties > application.yml > application.yaml
之后选中配置文件OK即可。再次回到配置文件中进行测试。
YAML (YAML Ain't Markup Language) ,一种数据序列化格式
优点:
- 容易阅读
- 容易与脚本语言交互
- 以数据为核心,重数据轻格式
YAML文件扩展名:
- yml(主流)
- yaml
yaml语法规则:
- 大小写敏感
- 属性层级关系使用多行描述,每行结尾使用冒号结束
- 使用缩进表示层级关系,同层级左侧对齐,只允许使用空格(不允许使用Tab键)
- 属性值前面添加空格(属性名与属性值之间使用冒号+空格作为分隔)
- #表示注释
核心规则:数据前面要加空格与冒号隔开
yaml数组数据:
数组数据在数据书写位置的下方使用减号作为数据开始符号,每行书写一个数据,减号与数据间空格分隔
enterprise: name: ylm age: 20 tel: 000000000 subject: - Java - 前端 - 大数据yaml数据读取
application.yml中
lesson: JavaSE enterprise: name: ylm age: 20 tel: 000000000 subject: - Java - 前端 - 大数据第一种:
使用@Value读取单个数据,属性名引用方式:${一级属性名.二级属性名...….
@Value("${lesson}") private String lesson; @Value("${enterprise.name}") private String name; @Value("${enterprise.subject[0]}") private String subject;第二种:
封装全部数据到Environment对象
@Autowired private Environment environment; System.out.println(environment.getProperty("enterprise.name")); System.out.println(environment.getProperty("enterprise.age"));第三种:
自定义对象封装指定数据
@Component @ConfigurationProperties(prefix = "enterprise") public class Enterprise { private String name; private Integer age; private String tel; private String[] subject; @Override public String toString() { return "Enterprise{" + "name='" + name + '\'' + ", age=" + age + ", tel='" + tel + '\'' + ", subject=" + Arrays.toString(subject) + '}'; } //此处省略get和set方法 }
@Autowired private Enterprise enterprise; System.out.println(enterprise);
yml格式
#设置使用哪个环境 spring: profiles: active: test --- #开发环境 #推荐格式 spring: config: activate: on-profile:dev server: port: 80 --- #生产环境 #过时格式 spring: profiles: pro server: port: 81 --- #测试环境 spring: profiles: test server: port: 82properties文件多环境启动
主启动配置文件application.properties
spring.profiles.active=pro环境分类配置文件application-pro.properties
server.port=80环境分类配置文件application-dev. properties
server.port=81环境分类配置文件application-test.properties
server.port=82
带参数启动SpringBoot
注意:打包时要注意文件格式都设置为UTF-8,有乱码解决之后再打包
java -jar springboot.jar --spring.profiles.active=testjava -jar springboot.jar --server. port=88java -jar springboot.jar --server. port=88 --spring. profiles.active=test参数加载优先顺序
参看
①Maven中设置多环境属性
<profiles> <profile> <id>devid> <properties> <profile.active>devprofile.active> properties> profile> <profile> <id>proid> <properties> <profile.active>proprofile.active> properties> <activation> <activeByDefault>trueactiveByDefault> activation> profile> <profile> <id>testid> <properties> <profile.active>testprofile.active> properties> profile> profiles>②SpringBoot中引用Maven属性
#设置使用哪个环境 spring: profiles: active: ${profile.active} --- #开发环境 spring: config: activate: on-profile:dev server: port: 80 --- #生产环境 spring: profiles: pro server: port: 81 --- #测试环境 spring: profiles: test server: port: 82Maven指令执行完毕后,生成了对应的包,其中类参与编译,但是配置文件并没有编译,而是复制到包中
解决思路:对于源码中非java类的操作要求加载Maven对应的属性,解析${}占位符
③对资源文件开启对默认占位符的解析
<plugin> <groupId>org.apache.maven.pluginsgroupId> <artifactId>maven-resources-pluginartifactId> <version>2.6version> <configuration> <encoding>UTF-8encoding> <useDefaultDelimiters>trueuseDefaultDelimiters> configuration> plugin>Maven打包加载到属性,打包顺利通过
配置文件分类
SpringBoot中4级配置文件
1级: file : config/application.yml 【最高】
2级: file : application.yml
3级: classpath: config/ application.yml4级: classpath: application.yml 【最低】
注:classpath代表在IDEA中编写的,file代表打包后在包里的添加的
作用:
- 1级与2级留做系统打包后设置通用属性
- 3级与4级用于系统开发阶段设置通用属性
结语:
SpringBoot基础配置的学习到此结束,我是Lyle,我们下次继续学习用SpringBoot最后一小部分整合第三方技术。