application.yml
person:
name: zhangsan
age: 11
application.properties
person.name=zhagnsan
person.age=112222222
通过注解@ConfigurationProperties(prefix = “person”)绑定yaml文件/properties文件
在pom.xml文件中添加如下配置
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
若配置文件中未配置相关属性,则取值为@Value属性值,优先级配置文件属性大于@Value
@Component
public class Animal {
@Value("草泥马")
private String name;
private String desc;
// seter geter
}
此时name=草泥马
若出现配置文件,则值来自于配置文件,例
application.properties
aa.name=hello
java文件
@Component
@ConfigurationProperties(prefix = "aa")
public class Animal {
@Value("草泥马")
private String name;
private String desc;
// seter geter
}
此时name=hello
如驼峰命名法xxxYyyZzz,可等价于xxx-yyy-zzz
application.yml
s:
user-name: hehe
java文件
@Component
@ConfigurationProperties(prefix = "s")
public class Student {
private String userName;
// seter geter
}
application.yml
x:
y:
z: zzzz
application.properties
a.b.c=aaaaaa
java文件
@Component
public class Animal {
@Value("${a.b.c}")
private String name;
@Value("${x.y.z}")
private String desc;
// seter geter
}
校验属性值是否正确
在pom.xml文件中添加如下配置
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
@Component
@ConfigurationProperties(prefix = "p")
@Validated
public class Person {
@Max(100L)
private int age
// seter geter
}
如图,校验age是否超过最大值100
默认会加载application.properties/application.yml文件,加载其他配置文件需要使用注解@PropertySource可以加载其他properties文件,不支持yml文件
例
config.properties
ppp.userName=abcdefg
java文件
@Component
@ConfigurationProperties(prefix = "ppp")
@PropertySource(value = {
"classpath:config.properties"})
public class Student {
private String userName;
// seter geter
}
spring.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<bean id="personService" class="com.example.studyRest.service.PersonService">
<property name="name" value="12345"></property>
</bean>
</beans>
PersonService
public class PersonService {
private String name;
// setter getter
}
启动类添加注解@ImportResource(“spring.xml”),即可将原spring xml配置对象注入到IOC容器中
注意:不推荐使用这种方式,springboot不需要多余的配置,这是个初衷
使用配置类方式代替
@Configuration
public class AppConfig {
@Bean
public PersonService getPersonService() {
PersonService p = new PersonService();
p.setName("zhangsan");
return p;
}
}
application.properties
test.name=$(random.uuid}
常用有
$(random.uuid}:uuid
$(random.value}随机字符串
${
random.int}:随机整型数
${
random.long}:随机长整型数
${
random.int(10)}:10以内的整型数
${
random.int[1024,65536]}:指定随机数范围
test.name=${
a.b.c}
a.b.c=aaaaaabbbbbb
m:
n: zzzz
xxxx: ${
m.n}
application.properties
test.name=${
a.b.c}
a.b.c=aaaaaabbbbbb
application.yml
m:
n: zzzz
xxxx: ${
test.name}