當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
SpringBoot_配置-properties配置文件编码问题
生活随笔
收集整理的這篇文章主要介紹了
SpringBoot_配置-properties配置文件编码问题
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
我們在properties里怎么配呢,我們看一下,來配置person的值,我們寫的時候就有提示person.last-name=張三
person.age=18
person.age=2017/12/15
person.boss=false
person.maps.k1=v1
person.maps.k2=14
person.lists=a,b,c
person.dog.name=dog
person.dog.age=15
#server.port=8081person.last-name=\u5F20\u4E09
person.age=18
person.birth=2017/12/15
person.boss=false
person.maps.k1=v1
person.maps.k2=14
person.lists=a,b,c
person.dog.name=dog
person.dog.age=15
package com.learn.bean;public class Dog {private String name;private Integer age;public String getName() {return name;}public void setName(String name) {this.name = name;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}@Overridepublic String toString() {return "Dog [name=" + name + ", age=" + age + "]";}}
package com.learn.bean;import java.util.Date;
import java.util.List;
import java.util.Map;import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;/*** 將配置文件中配置的每一個屬性的值,映射到這個組件* @ConfigurationProperties:告訴SpringBoot將本類中的所有屬性和配置文件中的相關的配置進行綁定* prefix="person":配置文件中哪個下面的所有屬性進行映射* * 只有這個組件是容器中的組件,才能容器提供的@ConfigurationProperties功能;* 默認從全局配置文件中獲取值;*/
//@PropertySource(value= {"classpath:person.properties"})
@Component
@ConfigurationProperties(prefix="person")
//@Validated
public class Person {/*** <bean class="person">* <property name="lastName" value="字面量/${key}從環境變量、配置文件中獲取值/#{SpEL}"></property>* </bean>*/// lastName必須是郵箱格式//@Email//@Value("${person.last-name}")private String lastName;private Integer age;private Boolean boss;private Date birth;//@Value("${person.maps}")private Map<String,Object> maps;private List<Object> lists;private Dog dog;public String getLastName() {return lastName;}public void setLastName(String lastName) {this.lastName = lastName;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}public Boolean getBoss() {return boss;}public void setBoss(Boolean boss) {this.boss = boss;}public Date getBirth() {return birth;}public void setBirth(Date birth) {this.birth = birth;}public Map<String, Object> getMaps() {return maps;}public void setMaps(Map<String, Object> maps) {this.maps = maps;}public List<Object> getLists() {return lists;}public void setLists(List<Object> lists) {this.lists = lists;}public Dog getDog() {return dog;}public void setDog(Dog dog) {this.dog = dog;}@Overridepublic String toString() {return "Person [lastName=" + lastName + ", age=" + age + ", boss=" + boss + ", birth=" + birth + ", maps="+ maps + ", lists=" + lists + ", dog=" + dog + "]";}}
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.learn</groupId><artifactId>spring-boot-01-helloworld</artifactId><version>0.0.1-SNAPSHOT</version><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.5.12.RELEASE</version><relativePath/> </parent><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><java.version>1.8</java.version><thymeleaf.version>3.0.9.RELEASE</thymeleaf.version><thymeleaf-layout-dialect.version>2.3.0</thymeleaf-layout-dialect.version><!-- 布局功能的支持程序 thymeleaf3主程序 layout2以上版本 --><!-- thymeleaf2 layout1 --><thymeleaf-layout-dialect.version>2.2.2</thymeleaf-layout-dialect.version></properties><dependencies><dependency><!-- 引入web模塊 --><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- Spring Boot進行單元測試的模塊 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId></dependency></dependencies><!-- 這個插件,可以將應用打包成一個可執行的jar包 --><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>
package com.learn.springboot;import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;import com.learn.bean.Person;/*** SpringBoot單元測試** 可以在測試期間很方便的類似編碼一樣進行自動注入等容器的功能*/
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringBoot02ConfigurationTests {@AutowiredPerson person;@Testpublic void contextLoads() {System.out.println(person);}}
Person [lastName=張三, age=18, boss=false, birth=Fri Dec 15 00:00:00 CST 2017, maps={k1=v1,
k2=14}, lists=[a, b, c], dog=Dog [name=dog, age=15]]
?
總結
以上是生活随笔為你收集整理的SpringBoot_配置-properties配置文件编码问题的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SpringBoot_入门-HelloW
- 下一篇: SpringBoot_配置-@Confi