美文网首页
SpringBoot项目读取properties文件信息

SpringBoot项目读取properties文件信息

作者: 素雪浮尘 | 来源:发表于2019-02-14 16:44 被阅读0次

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生成规则自动生成

idea修改getter生成规则

相关文章

网友评论

      本文标题:SpringBoot项目读取properties文件信息

      本文链接:https://www.haomeiwen.com/subject/pyneeqtx.html