YAML
使用一个全局的配置文件,配置文件名是固定的SpringBoot 个人笔记-config:application.properties、application.yml
YAML语法:
1、以空格的缩进来控制层级关系;只要是左对齐的一列数据,都是同一个层级的
2、属性和值也是大小写敏感;
3、双引号;不会转义字符串里面的特殊字符:name: "zhangsan \n lisi":输出;zhangsan 换行 lisi
4、'':单引号;会转义特殊字符:name: ‘zhangsan \n lisi’:输出;zhangsan \n lisi
5、注意缩进,长度随意,同一级要对齐
map
friends:
lastName: zhangsan
age: 20
行内写法
friends: {lastName: zhangsan,age: 18}
list
pets:
‐ cat
‐ dog
‐ pig
行内
pets: [cat,dog,pig]
读取配置:
person:
lastName: hello
age: 18
boss: false
birth: 2017/12/12
maps: {k1: v1,k2: 12}
lists:
‐ lisi
‐ zhaoliu
dog:
name: 小狗
age: 12
@Component
@ConfigurationProperties(prefix = "person")
public class Person {
private String lastName;
private Integer age;
private Boolean boss;
private Date birth;
private Map<String,Object> maps;
private List<Object> lists;
private Dog dog;
@Value("${person.lastName}")
private String userName;
需要导入这个,在添加配置时,就会有提示
!‐‐导入配置文件处理器,配置文件进行绑定就会有提示‐‐>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring‐boot‐configuration‐processor</artifactId>
<optional>true</optional>
</dependency>
如果配置是中文,乱码。调整如图:
修改ideal 默认的配置(新工程默认配置)
//指定路径下的配置
@PropertySource(value = {"classpath:person.properties"})
@Component
@ConfigurationProperties(prefix = "person")
@ImportResource(locations = {"classpath:beans.xml"})
导入Spring的配置文件让其生效
随机数
${random.value}、${random.int}、${random.long}
${random.int(10)}、${random.int[1024,65536]}
占位符获取之前配置的值,如果没有可以是用:指定默认值
properties
person.last‐name=张三${random.uuid}
person.age=${random.int}
person.birth=2017/12/15
person.boss=false
person.maps.k1=v1
person.maps.k2=14
person.lists=a,b,c
person.dog.name=${person.hello:hello}_dog
person.dog.age=15
激活指定配置文件
1、在配置文件中指定 spring.profiles.active=dev
2、命令行:java -jar 0.0.1-SNAPSHOT.jar --spring.profiles.active=dev;
3、虚拟机参数;-Dspring.profiles.active=dev
server:
port= 8081
spring:
profiles:
active: prod
---
server:
port: 8083
spring:
profiles: dev
---
server:
port: 8084
spring:
profiles: prod #指定属于哪个环境
配置文件加载顺序:
在指定位置优先加载带profiles的配置
优先级由高到底,高优先级的配置会覆盖低优先级的配置,同时不同配置项会形成互补;
–file:./config/
–file:./
–classpath:/config/
–classpath:/
运行包指定配置文件
java -jar 0.0.1-SNAPSHOT.jar --spring.config.location=G:/application.properties
@PropertySource获取指定路径下的配置文件
@PropertySource(value = {"classpath:person.properties"})
获取到的是项目路径下 resources文件的路径
@PropertySource(value = "file:${user.dir}/config/tree.properties")
获取到的是项目包路径下的文件
Environment
通过注入Environment 也能获取默认配置文件下的配置信息
@Component
public class MyConf {
@Autowired
private Environment env;
public void show1() {
//获取字符串
System.out.println("String: " + env.getProperty("string.port"));
//获取整数
System.out.println("Interger: " + (env.getProperty("integer.port", Integer.class)));
System.out.println(env.getProperty("db.link.url"));
System.out.println(env.getProperty("db.link.driver"));
System.out.println(env.getProperty("db.link.username"));
System.out.println(env.getProperty("db.link.password"));
System.out.println("===========================================");
}
}
配置json文件,生成Java文件(静态类),使用xlsx生成配置文件 并动态读取
python 安装
python jdk
直接360安全中心下载查询 python
安装后配置jdk
这个要手动导入包
代码查看个人码云仓库 https://gitee.com/graycat/spring-boot.git
@PropertySource是把指定的配置导入spring上下文中,然后@Value才能获取指定值。(不同配置文件,但是相同路径@Value("${jdbc2.url}"),是只会获取到一个配置的)
@Component()
@PropertySource(value = {"classpath:db2.properties"})
public class DB2Config {
@Value("${jdbc2.url}")
private String url;
@Value("${jdbc2.username}")
private String username;
@Value("${jdbc2.password}")
private String password;
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}