application.properties / application.yaml
the priority of properties is higher than yaml
yaml style
server:
port: 8080
properties style
server.port=8080
xml style
<server>
<port>8080</port>
</server>
yaml
实体类中属性的注入
@Value("Dog")
private String name;
@Value("3")
private int age;
yaml属性值注入
step1: 在application.yaml 配置实体类的对象属性
user:
name: user1
age: 1
happy: false
birth: 2000/10/5
maps: {k1: v1,k2: v2}
lists:
- hobby1
- hobby2
dog:
name: dog3
age: 3
step2: 在实体类上添加注解@ConfigurationProperties(prefix = "user")
@ConfigurationProperties(prefix = "user") 将application.yaml中对象的属性值注入 到实体类中
step3:使用时
@Autowired
private User user;
特点:
- 大小写敏感
- 使用缩进表示层级关系
特定配置文件
@PropertySource("classpath:specific.properties") //指定配置文件
specific.properties与application.properties同级目录
在属性上使用 注解 @Value("${name1}") 进行注入 //name1是specific.properties的键值对
@Validated
@Validated: @Validated下的注解 判断实体类中的属性是否符合数据格式,不符合会报错
//组合注解 JSR-303
@Length(min = 6, max = 19, message = "用户名长度是6-18位")
@ApiModelProperty(value = "用户名", required = true)
private String username;
@ApiModelProperty(value = "密码", required = true)
private String password;
//不能是空的
@NotEmpty
@ApiModelProperty(value = "用户头像")
private String icon;
@Email(message = "邮箱格式错误")
@ApiModelProperty(value = "邮箱")
private String email;
@NotNull
@ApiModelProperty(value = "用户昵称")
private String nickName;
@ApiModelProperty(value = "备注")
private String note;
————————————————
版权声明:本文为CSDN博主「不知所终,不知所起」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_44440642/article/details/106335653
配置文件加载位置
- file:./config/ equal springboot-02-config/config/application.yaml priority4 high
- file:./ equal springboot-02-config/application.yaml priority3
- classpath:/config/ equal resources/config/application.yaml priority2
- classpath:/ equal resources/application.yaml priority1 default
多种配置文件 为了不同阶段的开发
命名格式:application-xxx.yaml
配置文件的切换:在application.yaml中 用 spring.profiles.active=xxx 进行切换
多种文档模块
application.yaml
server:
port: 8080
---
server:
port: 8081
spring:
profiles: dev
---
application.properties与spring.factories的联系
HttpEncodingAutoConfiguration 已经在 spring.factories 注册
例子:HttpEncodingAutoConfiguration
@Configuration(proxyBeanMethods = false) //表示配置类
@EnableConfigurationProperties(ServerProperties.class) //自动配置属性
ServerProperties.class
@ConfigurationProperties(prefix = "server", ignoreUnknownFields = true) //ServerProperties.class 上的注解
private Integer port; //ServerProperties some attribute
private InetAddress address;
private ForwardHeadersStrategy forwardHeadersStrategy;
application.properties style
server.port
server.address
server.forwardHeadersStrategy
//ConditionalOn 判断当前配置是否生效
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET)
@ConditionalOnClass(CharacterEncodingFilter.class)
@ConditionalOnProperty(prefix = "server.servlet.encoding", value = "enabled", matchIfMissing = true)
public class HttpEncodingAutoConfiguration {
在spring.factories中配置的属性,都会有对应的xxxProperties.class
xxxProperties.class 由 yyyAutoConfiguration.class 装配且使用xxxProperties.class的默认值
可以通过application.properties改变默认值
application.properties
#可以理解 那些配置的生效
debug = true