@PropertySource and UTF-8 properties file

前端 未结 6 622
广开言路
广开言路 2020-12-20 23:11

Is it possible, using @PropertySource annotation, to configure the encoding that has to be used to load the property file?

An example to clarify my prob

6条回答
  •  轮回少年
    2020-12-20 23:45

    Or you can use the PropertiesFactoryBean that have the setEncoding method. Here an example from one of my projects

    @Bean
    public PropertiesFactoryBean cvlExternalProperties() {
        PropertiesFactoryBean res = new PropertiesFactoryBean();
        res.setFileEncoding("UTF-8");
        res.setLocation(new ClassPathResource("conf/external-test.properties"));
        return res;
    }
    

    and then you can use in the project with the following notation

    @Value("#{cvlExternalProperties['myProperty']}")
    private String p;
    

提交回复
热议问题