Springboot项目配置文件中配置项内容为中文读取乱码

application.properties中存在配置项,配置项的内容为中文

#自动拨测短信内容
test.message=中文测试

 

使用@Value读取乱码,如何解决?

 

方案一:修改idea配置

(不推荐)

设置utf-8编码,并勾上 Transparent native-to-ascii conversion

 

 

 

 设置之后,通过文本编辑器打开配置文件内容显示ascii编码,不方便修改配置内容

 

 

 

 

方案二:继承PropertiesPropertySourceLoader类

参考此博客的方案:https://blog.csdn.net/qq_34644183/article/details/117617539

1.创建一个类继承PropertiesPropertySourceLoader

 

public class SelfPropertySourceLoader  extends PropertiesPropertySourceLoader {
    @Override
    public List<PropertySource<?>> load(String name, Resource resource)
            throws IOException {
        //Map<String, ?> properties = loadProperties(resource);
        Map<String, ?> properties = (Map)loadUseUtf8(resource);
        if (properties.isEmpty()) {
            return Collections.emptyList();
        }
        return Collections
                .singletonList(new OriginTrackedMapPropertySource(name, properties));
    }
    private Properties loadUseUtf8(Resource resource) throws IOException {
        Properties props = new Properties();
        InputStream is = resource.getInputStream();
        try {
            String filename = resource.getFilename();
            if (filename != null && filename.endsWith(".xml")) {
                props.loadFromXML(is);
            } else {
                props.load(new InputStreamReader(is, "utf-8"));
            }
        } finally {
            is.close();
        }
        return props;
    }
}

 

2、在resource目录下创建目录META-INF,在META-INF目录下创建文件spring.factories

 

org.springframework.boot.env.PropertySourceLoader=org.config.SelfPropertySourceLoader

 

 

方案三:内容转码

在set方法上加@Value注解,set方法中将内容转码

@Component
public class ConfigConstant {

    public static String message;

 

@Value("${test.message}")
    public void setMessage(String message) {
        ConfigConstant.message = new String(message.getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8);
    }
}

 

问题:为什么要转码?

答:用@Value注解读取application.properties文件时,编码默认是ISO-8859-1,源码中可以看到编码是ISO-8859-1

 

 

 

 

 

方案四:@PropertySource指定编码

参考地址:https://www.cnblogs.com/zxf330301/p/9282402.html

 

1、添加一个新的配置文件如test.properties,将配置内容是中文的配置项添加在test.properties文件中

2、application.properties中添加配置项

#test.properties文件的绝对路径
config.path=E:/2021/test/src/main/resources/test.properties

3、@PropertySource设置配置文件路径

@Component
@PropertySource(value = {"file:${config.path}"},encoding = "UTF-8")
public class ConfigConstant {


  private static String message;
  @Value("${test.message}")
  public void setMessage(String message) {
        ConfigConstant.message = message;
  }

}
  • 问题1

为什么不直接设置classpath路径

@PropertySource(value = "classpath:test.properties",encoding = "UTF-8")

答:这样设置在idea中可以正常运行,打成jar包后test.properties的配置内容固定了,只能进入jar包中修改,无法在jar包外面添加test.properties进行配置。

  • 问题2

为什么不直接设置

@PropertySource(value = "classpath:application.properties",encoding = "UTF-8")          

答:application.properties文件通过@PropertySource注解指定encoding为UTF-8无效,还是会使用ISO-8859-1编码加载

 

 

 

 

 

 

posted @ 2021-08-25 17:44  harara  阅读(1322)  评论(0编辑  收藏  举报