有时候不想把所有的配置都写到application.yaml这个全局配置里面,因此有时候就需要自定义配置文件,例如
在resource目录下自定义一个sys.properties
sys.web-title=网站标题
sys.web-seo=网站优化
定义javaBean(配置组件)
系统配置组件:新建一个Sys类
package com.yfway.demo.bean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@PropertySource(value = {"classpath:sys.properties"})
@ConfigurationProperties(prefix = "sys")
@Data
public class Sys {
private String webTitle;
private String webSeo;
}
@PropertySource:引入外部配置,注意必须是properties后缀的文件才行
@ConfigurationProperties:读取文件里面前缀是sys的属性
在测试类中调用
@Autowired
Sys sys;
@Test
public void test(){
System.out.print(sys.getWebTitle());
}
读取properties文件,可能会出现乱码,如果出现乱码,则在idea设置一下即可
留言与评论(共有 0 条评论) “” |