上面讲到了yaml来配置属性,也提到了properties,所以这里便用properties来配置属性。
其实和上面没有什么太大的不同,代码再来添加一遍,和前面的代码大同小异。
首先pom.xml文件
<?xml version="1.0" encoding="UTF-8"?> <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> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.6.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>cc.acme_me</groupId> <artifactId>springboot-yaml</artifactId> <version>0.0.1-SNAPSHOT</version> <name>springboot-yaml</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
接着是程序入口:
package cc.acme_me.springbootyaml; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class SpringbootYamlApplication { public static void main(String[] args) { SpringApplication.run(SpringbootYamlApplication.class, args); } }
再接着分别是三个Entity类User、Role和Card
package cc.acme_me.springbootyaml.entity; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; import java.util.*; /** * User entity * * @author acme * @ConfigurationProperties 注解用于标识注释参数 prefix标识对应的前缀 * @Component 标识注入Spring容器中(只有注入Spring容器才能使用) */ @ConfigurationProperties(prefix = "user") @Component public class User { private Integer id; private String userName; private String password; private Date registerDate; private Integer privilegeLevel; private Role role; private List<String> hobbies = new ArrayList<>(); private Map<Integer, Card> card = new HashMap<>(); /** * 重新toString方法方便查看内容 */ @Override public String toString() { return "User{" + "id=" + id + ", userName='" + userName + '\'' + ", password='" + password + '\'' + ", registerDate=" + registerDate + ", privilegeLevel=" + privilegeLevel + ", role=" + role + ", hobbies=" + hobbies + ", card=" + card + '}'; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public Date getRegisterDate() { return registerDate; } public void setRegisterDate(Date registerDate) { this.registerDate = registerDate; } public Integer getPrivilegeLevel() { return privilegeLevel; } public void setPrivilegeLevel(Integer privilegeLevel) { this.privilegeLevel = privilegeLevel; } public Role getRole() { return role; } public void setRole(Role role) { this.role = role; } public List<String> getHobbies() { return hobbies; } public void setHobbies(List<String> hobbies) { this.hobbies = hobbies; } public Map<Integer, Card> getCard() { return card; } public void setCard(Map<Integer, Card> card) { this.card = card; } }
package cc.acme_me.springbootyaml.entity; /** * 权限 * * @author acme */ public class Role { private Integer id; private String level; /** * 重新toString方法方便查看内容 */ @Override public String toString() { return "Role{" + "id=" + id + ", level='" + level + '\'' + '}'; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getLevel() { return level; } public void setLevel(String level) { this.level = level; } }
package cc.acme_me.springbootyaml.entity; import java.util.Date; /** * 银行卡 * * @author acme */ public class Card { private Integer id; private String cardNumber; private String cvv; private Date startDate; private Date endDate; /** * 重新toString方法方便查看内容 */ @Override public String toString() { return "Card{" + "id=" + id + ", cardNumber='" + cardNumber + '\'' + ", cvv='" + cvv + '\'' + ", startDate=" + startDate + ", endDate=" + endDate + '}'; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getCardNumber() { return cardNumber; } public void setCardNumber(String cardNumber) { this.cardNumber = cardNumber; } public String getCvv() { return cvv; } public void setCvv(String cvv) { this.cvv = cvv; } public Date getStartDate() { return startDate; } public void setStartDate(Date startDate) { this.startDate = startDate; } public Date getEndDate() { return endDate; } public void setEndDate(Date endDate) { this.endDate = endDate; } }
还有就是properties文件的配置了
server.port=8080 ################User################ user.id=1 user.user-name=admin user.password=321 user.register_date=2023//03//03 user.privilegeLevel=15 user.role.id=2 user.role.level=Administrators user.hobbies=\u5531,\u8DF3,RAP,\u7BEE\u7403 ################分隔符 好看点################ user.card.1.id=1 user.card.1.cardNumber=123 user.card.1.cvv=321 user.card.1.startDate=2000//01//01 user.card.1.endDate=2010//01//01 ################分隔符 好看点################ user.card.2.id=2 user.card.2.cardNumber=456 user.card.2.cvv=654 user.card.2.startDate=2010//01//01 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就还是使用的驼峰标识,原来的属性名。
最后上测试类和测试结果:
package cc.acme_me.springbootyaml; import cc.acme_me.springbootyaml.entity.User; 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; /** * Spring Boot单元测试 * * @author acme */ @RunWith(SpringRunner.class) @SpringBootTest public class SpringbootYamlApplicationTests { @Autowired private User user; @Test public void contextLoads() { System.out.println(user); } }
测试结果表示,配置正常。