Spring Boot 自定義屬性 以及 亂碼問題

自定義屬性

application.properties提供自定義屬性的支持,這樣我們就可以把一些常量配置在這裏:

#自定義屬性
com.waiting.custom.one=自定義屬性ONE
com.waiting.custom.two=自定義屬性TWO
com.waiting.custom.three=3

然後直接在要使用的地方通過註解@Value(value="${config.name}")取出:

@RestController
public class HelloController {

    public final static Logger logger = LoggerFactory.getLogger(HelloController.class);

    @Value("${com.waiting.custom.one}")
    private String stringOne;
    @Value("${com.waiting.custom.two}")
    private String stringTwo;
    @Value("${com.waiting.custom.three}")
    private Integer integerThree;

    @RequestMapping(value = "/",name = "Hello")
    public String testHello(){
        logger.info("logback-ok");
        return "Hello Spring-boot!"+stringOne+stringTwo+integerThree;
    }
 }

這裏寫圖片描述

使用隨機數及自定義配置類

有時候我們需要我們的參數不是一個固定值,而是一個隨機數(比如密鑰)。Spring Boot的屬性配置文件中可以通過${random}來產生int值、long值或者String字符串,來支持屬性的隨機值。

#隨機字符串
com.waiting.random.str=${random.value}
#隨機int
com.waiting.random.number=${random.int}
#隨機long
com.waiting.random.bigNumber=${random.long}
#10以內的隨機數
com.waiting.random.test1=${random.int(10)}
#10-20的隨機數
com.waiting.random.test2=${random.int[10,20]}

在這邊配置中我們發現有一個特點,所有配置都是以"com.waiting.random"開頭的,而且有時候屬性太多了,一個個綁定到屬性字段上太累,官方提倡綁定一個對象的bean,這裏我們建一個RandomProperties .java類,頂部需要使用註解@ConfigurationProperties(prefix = "com.waiting.random")來進行聲明:

//@Configuration
//@Component
@ConfigurationProperties(prefix = "com.waiting.random")
public class RandomProperties {

    private String str;
    private Integer number;
    private Long bigNumber;
    private Integer test1;
    private Integer test2;

    // getter/setter方法省略

 }

此時配置完還需要在spring Boot入口類加上@EnableConfigurationProperties並指明要加載哪個bean,如果不寫RandomProperties.class,在bean類那邊添加@Configuration或者@Component

@SpringBootApplication
@EnableConfigurationProperties({RandomProperties.class})
public class SpringbootdemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootdemoApplication.class, args);
    }
}

把所有的自定義配置都放在application.properties裏面會顯得臃腫,這時候我們可以另外定義一個配置文件,這裏我明取名爲random.properties,路徑放在src/main/resources/waiting下面。
這裏只需要在之前的bean上添加@PropertySource("classpath:waiting/random.properties")@Configuration這個註解就可以了。
注意:不能再採取之前的@EnableConfigurationProperties({RandomProperties.class})這個方法。
最後controller層或service獲取的時候用@Autowired註解就可以獲取。

    @Autowired
    private  RandomProperties randomProperties;

    @Value("${com.waiting.custom.one}")
    private String stringOne;
    @Value("${com.waiting.custom.two}")
    private String stringTwo;
    @Value("${com.waiting.custom.three}")
    private Integer integerThree;

    @RequestMapping(value = "/",name = "Hello")
    public String testHello(){
        logger.info("logback-ok");

        return "Hello Spring-boot!"+stringOne+stringTwo+integerThree+"\r"+randomProperties;
    }

這裏寫圖片描述

中文亂碼問題

當在.properties的配置文件中有中文時,讀取出來的是亂碼。需要進行如下操作:

1、添加配置

#設置spring-boot 編碼格式
banner.charset=UTF-8
server.tomcat.uri-encoding=UTF-8
spring.http.encoding.charset=UTF-8
spring.http.encoding.enabled=true
spring.http.encoding.force=true
spring.messages.encoding=UTF-8

2、設置文件類型

application.properites的文件類型修改爲UTF-8的編碼類型。
通過以上方法測試獲取出來的值還是亂碼。

解決辦法

2.1、IDEA

設置 File Encodings的Transparent native-to-ascii conversion爲true,具體步驟如下:
依次點擊
File -> Settings -> Editor -> File Encodings
將Properties Files (*.properties)下的Default encoding for properties files設置爲UTF-8,將Transparent native-to-ascii conversion前的勾選上。

2.2、eclipse

在eclipse中安裝properties插件PropertiesEditor及設置(附圖),ASCII碼轉換成中文
推薦使用裏面的離線安裝。安裝完成PropertiesEditor 插件後,使用該編輯器重新編輯屬性文件中的中文,然後重新運行程序,發現讀取的中文配置文件不是亂碼了。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章