SpringBoot 加载自定义 properties文件

1、在我们的springboot启动类中,加上注解:

@PropertySource(value = "classpath:sms.properties",encoding = "utf-8")

如果是多个文件的话,可用多个@PropertySource注解也可以如下:

@PropertySource(value = {"classpath:mail.properties","classpath:sms.properties"},encoding = "utf-8")

2、在我们的使用类中添加@Value注解

@Value("${sms.code.model:123456}") // 如果配置文件中存在指定配置,则读取,若不存在则使用:后的默认值
private String codeModel;

3、为了防止中文乱码,请在PropertySource后制定encoding方式,然后系统的整体编码风格也需要指定一下

猜你喜欢

转载自blog.csdn.net/u013045878/article/details/81164187
今日推荐