解決@ConfigurationProperties注解的使用及亂碼問題
@ConfigurationProperties
作用:用于獲取配置文件中的屬性定義并綁定到javaBean屬性中
舉個栗子:
配置文件
mycar.name=徐昂 mycar.price=18w
定義實體類
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) //配置文件屬性讀取,讀取前綴時mycar的,忽略不存在的字段 public class Car { private String name; private String price; @Override public String toString() { return "Car{" + "name='" + name + '\'' + ", price='" + price + '\'' + '}'; } }
啟動類輸出驗證
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)生問題,定義中文時,會產(chǎn)生亂碼
解決方法
1,將配置文件換成yml文件,則不會產(chǎn)生亂碼問題
mycar: name: '徐昂' price: '18w'
2, 覆蓋原文件:org.springframework.boot.env.OriginTrackedPropertiesLoader
將OriginTrackedPropertiesLoader所有代碼復制出來,按照包路徑建立自己的包和類(包名和類名都必須和原來的一致,不然不生效)
然后找出原來的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)); }
重啟項目,發(fā)現(xiàn)項目中文亂碼已經(jīng)解決
Car{name='徐昂', price='18w'}
3, 自定義配置類
配置類
mycar.name=小汽車 mycar.price=18w
定義實體,添加**@PropertySource**注解,指定字符集取utf-8,并指定讀取配置文件的路徑。
注意:這種方法只能對自定義的properties文件有效,對于spring boot默認生成的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作用:開啟組件配置綁定功能,將實體類組件注入到容器中
eg:
mycar.name=小汽車 mycar.price=18w
實體類
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) //配置文件屬性讀取,讀取前綴時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)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Mybatis-Plus或PageHelper多表分頁查詢總條數(shù)不對問題的解決方法
PageHelper 這個插件用了很多次了,今天使用的時候才遇到一個問題,這篇文章主要給大家介紹了關于Mybatis-Plus或PageHelper多表分頁查詢總條數(shù)不對問題的解決方法,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下2022-08-08Elasticsearch中store field與non-store field的區(qū)別說明
這篇文章主要介紹了Elasticsearch中store field與non-store field的區(qū)別說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-07-07遠程調(diào)用@FeignClient注解屬性使用詳解
這篇文章主要為大家介紹了遠程調(diào)用@FeignClient注解屬性使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-10-1030w+數(shù)據(jù)使用RedisTemplate?pipeline空指針NullPointerException異常分析
這篇文章主要為大家介紹了30w+數(shù)據(jù)使用RedisTemplate?pipeline空指針NullPointerException異常分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-08-08