Spring environment reads Properties file and encoding problem

    At present, there are two basic technology stacks under the Spring system: SpringMVC, SpringBoot (and SpringCloud). There are some differences in the way of reading configuration files, but the whole is the same. Recently, I encountered the problem of garbled characters when reading the configuration file. In fact, the solution is relatively simple.

    1) For the properties file, regardless of the configuration method, Spring uses the Resource method to load the bottom layer, and finally reads the information based on the "classLoader.getResourceAsStream()" of JAVA itself. The encoding defaults to "ISO-8859-1". The encoding method Can be specified via configuration.

    See ConfigurationClassParser.java, ClassPathResource.java, etc.

    2) For yml and xml files, that is, configuration files whose suffix ends with "yml", "yaml", "xml" (including "properties"), this type of configuration can only be supported in the springboot technology stack at present , the encoding method is "UTF-8", see PropertiesPropertySourceLoader.java, YamlPropertySourceLoader.java.

 

    Therefore, for the encoding problem, we should pay special attention to the properties file, and we need to force the encoding to be "UTF-8", otherwise there may be a strange problem that the local environment is running normally, but the publishing environment is always garbled.

 

1. Based on XML

    In the SpringMVC environment, Bean-XML and annotations can usually be used to obtain Properties properties. We assume that the "test.properties" file already exists in the resources directory:

test.name=Hi, hello world!

 

    Bean XML method (1)

    1、Spring-Context.xml

<context:annotation-config />
<context:property-placeholder location="classpath:test.properties" ignore-unresolvable="true" file-encoding="UTF-8"/>

 

    2、TestService.java

@Service
public class TestService {

    @Value("${test.name}")
    private String testName;
    ...
}

 

    Bean XML method (2)

    1、Spring-Context.xml

<bean id="globalProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
    <property name="fileEncoding" value="UTF-8"/>
    <property name="locations">
        <array>
            <value>classpath:test2.properties</value>
        </array>
    </property>
</bean>

 

    2、TestService.java

@Service
public class TestService {

    @Value("#{globalProperties['test.name']}")
    private String testName;
    ...
}

 

    We need to pay special attention, if the @Controller layer in the SpringMVC Web project needs to use @Value to inject attributes, we need to put the above configuration in the Servlet-related Spring file. This file name is usually the same as the subordinate configuration:

<servlet>
    <servlet-name>spring-mvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring-web.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

    In this example, when @Controller needs to use the Properties attribute, their configuration information should be placed in spring-web.xml. It is not feasible for you to place similar files such as Spring-context.xml, because "Servlet" in SpringMVC "Container" and "Spring Component Container" come from different BeanFactory, so @Controllor needs to be treated differently from @Service, @Component, @Resource, etc.

 

    In order to avoid garbled characters, we need to force encoding to be specified in all places where properties are loaded. In addition, it is recommended that you upgrade the spring baseline version to 4.3.X+.

 

2. Based on annotations

    Spring 3.1+ provides a convenient annotation, @PropertySource, which can help us use it directly in springMVC and springBoot environments.

@PropertySource(value = "classpath:test3.properties",encoding = "UTF-8")
@Controller
public class IndexController {

    @Value("${test.name}")
    private String testName;
    ....
}

 

    Note that @PropertySource can be used in any Spring Bean component; but again, @Controller, @Service, @Component, etc. may need to be configured separately; in addition, it is recommended to create a configuration management class for @Configuration, and load these Properties only once to Bean way to export for shared use by all components. Don't forget the "encoding" property.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326048554&siteId=291194637
Recommended