宠物店做网站的论文/青岛神马排名优化
文章目录
- 文章参考
- 加载文件路径顺序
- 默认配置文件名(application.yml 或者 application.properties)
- 配置文件的原则
- 项目打包运行后可通过命令指定配置文件位置
- 配置文件yaml
- 关于配置的注解
- 案例
文章参考
- boot-features-external-config
加载文件路径顺序
- file:./config/ - 优先级最高(项目根路径下的config)
- file:./ - 优先级第二 -(项目根路径下)
- classpath:/config/ - 优先级第三(项目resources/config下)
- classpath:/ - 优先级第四(项目resources根目录)
个人建议使用:
classpath:/config/ - 优先级第三(项目resources/config下)
classpath:/ - 优先级第四(项目resources根目录)
这样打包的时候,配置文件也能打包进去
默认配置文件名(application.yml 或者 application.properties)
SpringBoot项目启动会去扫面项目以上目录位置的 application.yml 或者 application.properties
如果同一个目录下,有application.yml也有application.properties,默认先读取application.properties。
配置文件的原则
- 高优先级配置会覆盖低优先级配置
- 多个配置文件互补
特殊使用应用场景
1、如果同一个目录下,有application.yml也有application.properties,默认先读取application.properties。
2、如果同一个配置属性,在多个配置文件都配置了,默认使用第1个读取到的,后面读取的不覆盖前面读取到的。
3、创建SpringBoot项目时,一般的配置文件放置在“项目的resources目录下”
项目打包运行后可通过命令指定配置文件位置
引入外部配置文件
java -jar demo-xxx.jar --spring.config.location=d:/application.properties
配置文件yaml
-
语法规则
- key: value; key和value 之间有空格
- 大小写敏感
- 使用缩进表示层级关系
- 缩进不允许使用tab,只允许空格
- 缩进的空格数不重要,只要相同层级的元素左对齐即可
- # 表示注释
- ‘’ 和 “” 表示字符串
-
三种数据结构
- 字面量:普通的值。(数字,字符串,布尔)
- 对象:键值对的集合。(Map)
- 数组:一组按次序排列的值。(List,Set)
-
例子说明:
person:userName: huangbiaoboss: truebirth: 2019/12/9age: 18# 数组的表示方式interest:- 篮球- 足球- 乒乓球# 数组的表示方式animal: [老虎, 狮子]# 对象的表示方式score: {english: 80, math: 90}salarys:- 9999- 8888allPets:# 数组对象sick:- {name: 阿狗, weight: 88}- name: 阿猫weight: 99- name: 阿花weight: 66health:- {name: 阿狗1, weight: 8.8}- {name: 阿狗2, weight: 88.9}
关于配置的注解
@ConfigurationProperties 当需要对整个对象的整体进行赋值时
,使用@ConfigurationProperties。
@Value 当只需要为某个值提供注入时
,推荐使用@Value方式。
案例
- java类与配置文件对应
@ConfigurationProperties 将java类 PersonProperties与 配置文件关联
package com.huangbiao.springbootweb.hello.bean;import lombok.Data;
import lombok.ToString;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;// 跟配置文件关联,前缀是person
@ConfigurationProperties(prefix = "person")
@Component // 交给spring 容器管理
@Data
@ToString
public class PersonProperties {private String userName;private Boolean boss;private Date birth;private Integer age;private Pet pet;private String[] interests;private List<String> animal;private Map<String, Object> score;private Set<Double> salarys;private Map<String, List<Pet>> allPets;
}
@Autowired将 配置类PersonProperties注入到 Controller 中
package com.huangbiao.springbootweb.hello.controller;import com.huangbiao.springbootweb.hello.bean.PersonProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;@RestController
@Slf4j
public class ConfigYamlController {// 因为 PersonProperties 类交给了容器管理,因此可以直接注入@AutowiredPersonProperties personProperties;@Value("${server.port}")int serverPort;//java中取值最好也写上默认值@Value("${province.city:beijing}")public String city;@GetMapping("/person/properties")public PersonProperties GetUser(){System.out.println(this.serverPort);log.info("我进来了");log.info(this.city);return personProperties;}
}
- 没有自定义配置,设置默认值
@Value("${province.city:beijing}")
public String city;
- 直接获取配置文件中的属性值
// 获取配置文件中 server.port 的值
@Value("${server.port}")