解決@ConfigurationProperties注解的使用及亂碼問題
@ConfigurationProperties
作用:用于獲取配置文件中的屬性定義并綁定到j(luò)avaBean屬性中
舉個栗子:
配置文件
mycar.name=徐昂 mycar.price=18w
定義實(shí)體類
package com.maggie.demo.entity;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Data //生成setget方法
@Component //將此類注冊為組件
@ConfigurationProperties(prefix = "mycar",ignoreUnknownFields = true) //配置文件屬性讀取,讀取前綴時(shí)mycar的,忽略不存在的字段
public class Car {
private String name;
private String price;
@Override
public String toString() {
return "Car{" +
"name='" + name + '\'' +
", price='" + price + '\'' +
'}';
}
}啟動類輸出驗(yàn)證
package com.maggie.demo;
import com.maggie.demo.entity.Car;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import java.io.IOException;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) throws IOException {
ConfigurableApplicationContext run = SpringApplication.run(DemoApplication.class, args);
Car car = run.getBean(Car.class);
System.out.println(car.toString());
}
}輸出結(jié)果
Car{name='�昂', price='18w'}
產(chǎn)生問題,定義中文時(shí),會產(chǎn)生亂碼
解決方法
1,將配置文件換成yml文件,則不會產(chǎn)生亂碼問題
mycar: name: '徐昂' price: '18w'
2, 覆蓋原文件:org.springframework.boot.env.OriginTrackedPropertiesLoader
將OriginTrackedPropertiesLoader所有代碼復(fù)制出來,按照包路徑建立自己的包和類(包名和類名都必須和原來的一致,不然不生效)

然后找出原來的OriginTrackedPropertiesLoader上的編碼片段:
CharacterReader(Resource resource) throws IOException {
this.reader = new LineNumberReader(new InputStreamReader(resource.getInputStream(), StandardCharsets.ISO_8859_1));
}將其改為 : StandardCharsets.UTF_8
CharacterReader(Resource resource) throws IOException {
this.reader = new LineNumberReader(new InputStreamReader(resource.getInputStream(), StandardCharsets.UTF_8));
}重啟項(xiàng)目,發(fā)現(xiàn)項(xiàng)目中文亂碼已經(jīng)解決
Car{name='徐昂', price='18w'}3, 自定義配置類

配置類
mycar.name=小汽車 mycar.price=18w
定義實(shí)體,添加**@PropertySource**注解,指定字符集取utf-8,并指定讀取配置文件的路徑。
注意:這種方法只能對自定義的properties文件有效,對于spring boot默認(rèn)生成的application.properties沒有效果
@Data //生成setget方法
@Component //將此類注冊為組件
//指定字符集,并且指定讀取的配置文件
@PropertySource(encoding = "UTF-8", value = "classpath:car.properties", ignoreResourceNotFound = true)
@ConfigurationProperties(prefix = "mycar",ignoreUnknownFields = true) //配置文件屬性讀取,讀取前綴是mycar的
public class Car {
private String name;
private String price;
@Override
public String toString() {
return "Car{" +
"name='" + name + '\'' +
", price='" + price + '\'' +
'}';
}
}結(jié)果:
Car{name='小汽車', price='18w'}
第二種不推薦寫法
@ConfigurationProperties+ @EnableConfigurationProperties
@EnableConfigurationProperties作用:開啟組件配置綁定功能,將實(shí)體類組件注入到容器中
eg:
mycar.name=小汽車 mycar.price=18w
實(shí)體類
package com.maggie.demo.entity;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Data //生成setget方法
@ConfigurationProperties(prefix = "mycar",ignoreUnknownFields = true) //配置文件屬性讀取,讀取前綴時(shí)mycar的,忽略不存在的字段
public class Car {
private String name;
private String price;
@Override
public String toString() {
return "Car{" +
"name='" + name + '\'' +
", price='" + price + '\'' +
'}';
}
}配置類
@Configuration //配置類注解 ==配置文件
@EnableConfigurationProperties(Car.class) //開啟加載配置類
public class BeansConfiguration {
}以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
java實(shí)現(xiàn)斗地主發(fā)牌系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)斗地主發(fā)牌系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-04-04
Mybatis-Plus或PageHelper多表分頁查詢總條數(shù)不對問題的解決方法
PageHelper 這個插件用了很多次了,今天使用的時(shí)候才遇到一個問題,這篇文章主要給大家介紹了關(guān)于Mybatis-Plus或PageHelper多表分頁查詢總條數(shù)不對問題的解決方法,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-08-08
Java笛卡爾積算法原理與實(shí)現(xiàn)方法詳解
這篇文章主要介紹了Java笛卡爾積算法原理與實(shí)現(xiàn)方法,結(jié)合實(shí)例形式較為詳細(xì)的分析了笛卡爾積算法的原理及java定義與使用笛卡爾積算法的相關(guān)操作技巧,需要的朋友可以參考下2017-12-12
Elasticsearch中store field與non-store field的區(qū)別說明
這篇文章主要介紹了Elasticsearch中store field與non-store field的區(qū)別說明,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-07-07
遠(yuǎn)程調(diào)用@FeignClient注解屬性使用詳解
這篇文章主要為大家介紹了遠(yuǎn)程調(diào)用@FeignClient注解屬性使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10
30w+數(shù)據(jù)使用RedisTemplate?pipeline空指針NullPointerException異常分析
這篇文章主要為大家介紹了30w+數(shù)據(jù)使用RedisTemplate?pipeline空指針NullPointerException異常分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08

