聯系我們 - 廣告服務 - 聯系電話:
您的當前位置: > 關注 > > 正文

全球熱點評!配置文件中文亂碼怎么解決?訪問多個配置文件的的處理

來源:CSDN 時間:2023-01-06 08:17:28


(相關資料圖)

package com.bojia.fund.proconfig;import org.springframework.beans.factory.annotation.Value;import org.springframework.context.annotation.PropertySource;import org.springframework.stereotype.Component;@Component@PropertySource(value={"classpath:application.properties","classpath:message.properties"},encoding="UTF-8")//設置編碼格式public class MessageProperty {@Value("${msg_nopay}")private String msg_nopay;public String getMsg_nopay() {return msg_nopay;}}

文件目錄結構為src/main/re/application.properties,

src/main/resources/message.properties.

我這里有兩個配置文件,如果要引用MessageProperty 這個類的話在server層或者controller層加上如下代碼:

@Controllerpublic class TestController {@AutowiredMessageProperty property;@RequestMapping(value = "/index.do",produces="text/plain;charset=UTF-8",method = RequestMethod.GET)public String index() {    //你的代碼return null;}}

我這里是controller,server層也是一樣,就可以使用了,注意:

1.要用到@value這個注解的話,不要new,否則取不到值,我這里用的是@Autowired     MessageProperty property;還有application.properties在resources目錄下,如果出現掃描不到xxx.properties等自定義配置文件的話可能是你的

@PropertySource(value={"classpath:application.properties","classpath:message.properties"},encoding="UTF-8")配置錯誤,注意不要寫成

@PropertySource(value={"classpath:resources/application.properties","classpath:resources/message.properties"},encoding="UTF-8"),否認你會啟動報錯的,因為SpringBoot會依次默認從 src/main/resources/等路徑下查找文件,你就不要多此一舉在前面加一個"resources/"了

2.如果出現配置文件中文亂碼就在注解@PropertySource里面加上encoding="UTF-8",可以解決配置文件中文亂碼問題!

3.多個配置文件的話@PropertySource要用value={"classpath:application.properties","classpath:message.properties"}來指定

4.@value得注解在@Component,@Server,@Controller下都可以拿到值

責任編輯:

標簽: 配置文件

精彩放送:

新聞聚焦
Top 岛国精品在线