03、Spring Boot配置文件-properties
Spring Boot    2019-07-24 00:00:48    470    0    0
acme   Spring Boot

    上面讲到了yaml来配置属性,也提到了properties,所以这里便用properties来配置属性。

其实和上面没有什么太大的不同,代码再来添加一遍,和前面的代码大同小异。

首先pom.xml文件

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3.          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4.     <modelVersion>4.0.0</modelVersion>
  5.     <parent>
  6.         <groupId>org.springframework.boot</groupId>
  7.         <artifactId>spring-boot-starter-parent</artifactId>
  8.         <version>2.1.6.RELEASE</version>
  9.         <relativePath/> <!-- lookup parent from repository -->
  10.     </parent>
  11.     <groupId>cc.acme_me</groupId>
  12.     <artifactId>springboot-yaml</artifactId>
  13.     <version>0.0.1-SNAPSHOT</version>
  14.     <name>springboot-yaml</name>
  15.     <description>Demo project for Spring Boot</description>
  16.  
  17.     <properties>
  18.         <java.version>1.8</java.version>
  19.     </properties>
  20.  
  21.     <dependencies>
  22.         <dependency>
  23.             <groupId>org.springframework.boot</groupId>
  24.             <artifactId>spring-boot-starter-web</artifactId>
  25.         </dependency>
  26.  
  27.         <dependency>
  28.             <groupId>org.springframework.boot</groupId>
  29.             <artifactId>spring-boot-devtools</artifactId>
  30.             <scope>runtime</scope>
  31.             <optional>true</optional>
  32.         </dependency>
  33.         <dependency>
  34.             <groupId>org.springframework.boot</groupId>
  35.             <artifactId>spring-boot-starter-test</artifactId>
  36.             <scope>test</scope>
  37.         </dependency>
  38.  
  39.         <dependency>
  40.             <groupId>org.springframework.boot</groupId>
  41.             <artifactId>spring-boot-configuration-processor</artifactId>
  42.             <optional>true</optional>
  43.         </dependency>
  44.  
  45.     </dependencies>
  46.  
  47.     <build>
  48.         <plugins>
  49.             <plugin>
  50.                 <groupId>org.springframework.boot</groupId>
  51.                 <artifactId>spring-boot-maven-plugin</artifactId>
  52.             </plugin>
  53.         </plugins>
  54.     </build>
  55.  
  56. </project>

 接着是程序入口:

  1. package cc.acme_me.springbootyaml;
  2.  
  3. import org.springframework.boot.SpringApplication;
  4. import org.springframework.boot.autoconfigure.SpringBootApplication;
  5.  
  6. @SpringBootApplication
  7. public class SpringbootYamlApplication {
  8.  
  9.     public static void main(String[] args) {
  10.         SpringApplication.run(SpringbootYamlApplication.class, args);
  11.     }
  12.  
  13. }

 再接着分别是三个Entity类User、Role和Card

  1. package cc.acme_me.springbootyaml.entity;
  2.  
  3. import org.springframework.boot.context.properties.ConfigurationProperties;
  4. import org.springframework.stereotype.Component;
  5.  
  6. import java.util.*;
  7.  
  8. /**
  9.  * User entity
  10.  *
  11.  * @author acme
  12.  * @ConfigurationProperties 注解用于标识注释参数 prefix标识对应的前缀
  13.  * @Component 标识注入Spring容器中(只有注入Spring容器才能使用)
  14.  */
  15. @ConfigurationProperties(prefix = "user")
  16. @Component
  17. public class User {
  18.  
  19.     private Integer id;
  20.     private String userName;
  21.     private String password;
  22.  
  23.     private Date registerDate;
  24.  
  25.     private Integer privilegeLevel;
  26.  
  27.     private Role role;
  28.  
  29.     private List<String> hobbies = new ArrayList<>();
  30.  
  31.     private Map<Integer, Card> card = new HashMap<>();
  32.  
  33.     /**
  34.      * 重新toString方法方便查看内容
  35.      */
  36.     @Override
  37.     public String toString() {
  38.         return "User{" +
  39.                 "id=" + id +
  40.                 ", userName='" + userName + '\'' +
  41.                 ", password='" + password + '\'' +
  42.                 ", registerDate=" + registerDate +
  43.                 ", privilegeLevel=" + privilegeLevel +
  44.                 ", role=" + role +
  45.                 ", hobbies=" + hobbies +
  46.                 ", card=" + card +
  47.                 '}';
  48.     }
  49.  
  50.     public Integer getId() {
  51.         return id;
  52.     }
  53.  
  54.     public void setId(Integer id) {
  55.         this.id = id;
  56.     }
  57.  
  58.     public String getUserName() {
  59.         return userName;
  60.     }
  61.  
  62.     public void setUserName(String userName) {
  63.         this.userName = userName;
  64.     }
  65.  
  66.     public String getPassword() {
  67.         return password;
  68.     }
  69.  
  70.     public void setPassword(String password) {
  71.         this.password = password;
  72.     }
  73.  
  74.     public Date getRegisterDate() {
  75.         return registerDate;
  76.     }
  77.  
  78.     public void setRegisterDate(Date registerDate) {
  79.         this.registerDate = registerDate;
  80.     }
  81.  
  82.     public Integer getPrivilegeLevel() {
  83.         return privilegeLevel;
  84.     }
  85.  
  86.     public void setPrivilegeLevel(Integer privilegeLevel) {
  87.         this.privilegeLevel = privilegeLevel;
  88.     }
  89.  
  90.     public Role getRole() {
  91.         return role;
  92.     }
  93.  
  94.     public void setRole(Role role) {
  95.         this.role = role;
  96.     }
  97.  
  98.     public List<String> getHobbies() {
  99.         return hobbies;
  100.     }
  101.  
  102.     public void setHobbies(List<String> hobbies) {
  103.         this.hobbies = hobbies;
  104.     }
  105.  
  106.     public Map<Integer, Card> getCard() {
  107.         return card;
  108.     }
  109.  
  110.     public void setCard(Map<Integer, Card> card) {
  111.         this.card = card;
  112.     }
  113. }
  1. package cc.acme_me.springbootyaml.entity;
  2.  
  3. /**
  4.  * 权限
  5.  *
  6.  * @author acme
  7.  */
  8. public class Role {
  9.  
  10.     private Integer id;
  11.     private String level;
  12.  
  13.     /**
  14.      * 重新toString方法方便查看内容
  15.      */
  16.     @Override
  17.     public String toString() {
  18.         return "Role{" +
  19.                 "id=" + id +
  20.                 ", level='" + level + '\'' +
  21.                 '}';
  22.     }
  23.  
  24.     public Integer getId() {
  25.         return id;
  26.     }
  27.  
  28.     public void setId(Integer id) {
  29.         this.id = id;
  30.     }
  31.  
  32.     public String getLevel() {
  33.         return level;
  34.     }
  35.  
  36.     public void setLevel(String level) {
  37.         this.level = level;
  38.     }
  39. }
  1. package cc.acme_me.springbootyaml.entity;
  2.  
  3. import java.util.Date;
  4.  
  5. /**
  6.  * 银行卡
  7.  *
  8.  * @author acme
  9.  */
  10. public class Card {
  11.  
  12.     private Integer id;
  13.     private String cardNumber;
  14.     private String cvv;
  15.     private Date startDate;
  16.     private Date endDate;
  17.  
  18.     /**
  19.      * 重新toString方法方便查看内容
  20.      */
  21.     @Override
  22.     public String toString() {
  23.         return "Card{" +
  24.                 "id=" + id +
  25.                 ", cardNumber='" + cardNumber + '\'' +
  26.                 ", cvv='" + cvv + '\'' +
  27.                 ", startDate=" + startDate +
  28.                 ", endDate=" + endDate +
  29.                 '}';
  30.     }
  31.  
  32.     public Integer getId() {
  33.         return id;
  34.     }
  35.  
  36.     public void setId(Integer id) {
  37.         this.id = id;
  38.     }
  39.  
  40.     public String getCardNumber() {
  41.         return cardNumber;
  42.     }
  43.  
  44.     public void setCardNumber(String cardNumber) {
  45.         this.cardNumber = cardNumber;
  46.     }
  47.  
  48.     public String getCvv() {
  49.         return cvv;
  50.     }
  51.  
  52.     public void setCvv(String cvv) {
  53.         this.cvv = cvv;
  54.     }
  55.  
  56.     public Date getStartDate() {
  57.         return startDate;
  58.     }
  59.  
  60.     public void setStartDate(Date startDate) {
  61.         this.startDate = startDate;
  62.     }
  63.  
  64.     public Date getEndDate() {
  65.         return endDate;
  66.     }
  67.  
  68.     public void setEndDate(Date endDate) {
  69.         this.endDate = endDate;
  70.     }
  71. }

 还有就是properties文件的配置了

  1. server.port=8080
  2. ################User################
  3. user.id=1
  4. user.user-name=admin
  5. user.password=321
  6. user.register_date=2023//03//03
  7. user.privilegeLevel=15
  8. user.role.id=2
  9. user.role.level=Administrators
  10. user.hobbies=\u5531,\u8DF3,RAP,\u7BEE\u7403
  11. ################分隔符 好看点################
  12. user.card.1.id=1
  13. user.card.1.cardNumber=123
  14. user.card.1.cvv=321
  15. user.card.1.startDate=2000//01//01
  16. user.card.1.endDate=2010//01//01
  17. ################分隔符 好看点################
  18. user.card.2.id=2
  19. user.card.2.cardNumber=456
  20. user.card.2.cvv=654
  21. user.card.2.startDate=2010//01//01
  22. user.card.2.endDate=2020//01//01

    这里有两点需要注意的:

    一、是关于properties文件的编码问题,可以看到,我在user.hobbies参数后面都是\u开始,这个是什么意思呢?\u开头表示这一个Unicode编码的字符,为了解决中文乱码问题,可以在IDE工具里面去设置工具来解决,但是相比之下我更推荐我上面这种方式。如果去改IDE的编码,换了一个环境,换了一个编码,就要再去改一遍。当然,使用Unicode编码字符串也是有坏处的,就比如上面这一段,直接看并不知道这里面的内容是啥,是不是中文都不一定。

    这里这个小问题一笔带过,properties中文要记得注意编码问题!

    二、例如我entity中的User类,用户名是userName为什么这里配置的是user-name?还有register_date?一个变成了短杠,一个用下划线隔开?这个其实是针对驼峰标识的单词,可以使用这两种方式。比如我下面的cardNum就还是使用的驼峰标识,原来的属性名。

最后上测试类和测试结果:

  1. package cc.acme_me.springbootyaml;
  2.  
  3. import cc.acme_me.springbootyaml.entity.User;
  4. import org.junit.Test;
  5. import org.junit.runner.RunWith;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.boot.test.context.SpringBootTest;
  8. import org.springframework.test.context.junit4.SpringRunner;
  9.  
  10. /**
  11.  * Spring Boot单元测试
  12.  *
  13.  * @author acme
  14.  */
  15. @RunWith(SpringRunner.class)
  16. @SpringBootTest
  17. public class SpringbootYamlApplicationTests {
  18.  
  19.     @Autowired
  20.     private User user;
  21.  
  22.     @Test
  23.     public void contextLoads() {
  24.         System.out.println(user);
  25.     }
  26.  
  27. }

测试结果表示,配置正常。

Pre: 04、Spring Boot @Value和@Configurationproperties注解的区别

Next: 02、Spring Boot入门YAML基础语法

470
Table of content