1.配置yml文件

yml中配置不能用驼峰命名,要用-分割
2.写config类获取bean属性
@ConfigurationProperties(prefix = "rar-file")
@Data
@Component
public class RarConfig {
public String rarFilePath;
}
3.在使用属性的类上配置使当前类成为一个bean对象

4.写两个配置类的引用
@Autowired
private RarConfig rarConfig;
private static RarConfig rarConfig1;
5.写个@PostConstruct注解的方法
@PostConstruct
public void init(){
rarConfig1 = rarConfig;
}
在这个方法里把rarConfig的值赋给rarConfig1
6.在方法中直接使用

本文详细介绍了如何在Spring Boot项目中使用YML配置文件,并通过@ConfigurationProperties注解将配置映射到Java Bean对象。从YML配置规则、Config类创建到Bean对象的注入与引用,再到@PostConstruct注解的使用,一步步解析了整个配置过程。
1506

被折叠的 条评论
为什么被折叠?



