SpringBoot项目读取properties文件信息

1.properties内容:

check.statisticsRegiona=四川省

2.config配置

@Component
@PropertySource("classpath:check.properties")
@ConfigurationProperties(prefix="check")
public class CheckConfig {
    private String statisticsRegiona;
  
    public String getStatisticsRegiona() throws UnsupportedEncodingException {
        return changeEncoding(statisticsRegiona);
    }

    public void setStatisticsRegiona(String statisticsRegiona) {
        this.statisticsRegiona = statisticsRegiona;
    }

    /**
     * 将string的encoding改成UTF-8,解决中文乱码
     * @param str
     * @return
     * @throws UnsupportedEncodingException
     */
    private String changeEncoding(String str) throws UnsupportedEncodingException {
        return new String(str.getBytes("ISO-8859-1"),"UTF-8");
    }
}

3.获取信息实体类

@Autowired
private CheckConfig checkConfig;

/*
* 打印配置的信息
*/
public void printProperty() {
    System.out.print(checkConfig.getStatisticsRegiona());
}

备注:属性较多,手动修改配置类的getter方法比较繁琐,通过修改getter生成规则自动生成
SpringBoot项目读取properties文件信息