您好,登錄后才能下訂單哦!
這篇文章主要講解了“SpringBoot如何獲取application.properties中自定義的值”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“SpringBoot如何獲取application.properties中自定義的值”吧!
目錄結構:
pom文件:
<?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 https://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.5.4</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>i18nSpringbootDemo-1</artifactId> <version>0.0.1-SNAPSHOT</version> <name>i18nSpringbootDemo-1</name> <description>Demo project for Spring Boot</description> <properties> <java.version>11</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-starter-test</artifactId> <scope>test</scope> </dependency> <!-- 導入配置文件處理器,配置文件進行綁定就會提示 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency> <!--校驗依賴--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-validation</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> <packaging>war</packaging> </project>
application.properties:
test.user.id=12 #也可以寫成 test.user.user-name=zhangsan test.user.userName=zhansan #也可以寫成 test.user.user-password=XXX test.user.userPassword=PWD123 #也可以寫成 test.user.la-big-decimal=XXX test.user.laBigDecimal=138.3 test.user.maps.key1=V1 test.user.maps.key2=123 test.user.lists=a12,a13,sdf test.user.department.dep-code=dep001 test.user.department.dep-name=depName001
Department類:
package com.example.demo.obj; public class Department { private String depCode; private String depName; /** * @return depCode */ public String getDepCode() { return depCode; } /** * @param depCode セットする depCode */ public void setDepCode(String depCode) { this.depCode = depCode; } /** * @return depName */ public String getDepName() { return depName; } /** * @param depName セットする depName */ public void setDepName(String depName) { this.depName = depName; } @Override public String toString() { return "Department [depCode=" + depCode + ", depName=" + depName + "]"; } }
User類:
package com.example.demo.obj; import java.math.BigDecimal; import java.util.List; import java.util.Map; import javax.validation.constraints.Email; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; import org.springframework.validation.annotation.Validated; /* * 將配置文件的每一個屬性值,映射到這個組件中: * ①@ConfigurationProperties:告訴SpringBoot將本類中的所有屬性和配置文件中相關的配置進行綁定; * prefix = "test.user":將配置文件中前綴為test.user下面的所有屬性進行一一映射 * 只有這個組件是容器中的組件,才能提供@ConfigurationProperties的功能,所以要加@Component * * ②@Value("${key}")從環境變量、配置文件中獲取值 * @Value("#{SpEl}")表達式 * * @ConfigurationProperties與@Value的區別: * @ConfigurationProperties支持松散語法,JSR303數據校驗,復雜類型封裝,不支持SpEL * @Value支持SpEL,不支持松散語法,JSR303數據校驗,復雜類型封裝 * 如果說,我們在某個業務邏輯中需要獲取一下配置文件中的某項值,可以用@Value * 如果說,我們專門編寫了一個javaBean去和配置文件進行映射,我們直接使用@ConfigurationProperties */ @Component @ConfigurationProperties(prefix = "test.user") @Validated public class User { //@Value("#{10*2}") private Integer id; //@Email userName必須輸入郵箱格式的值,要不然報錯 //@Value("${test.user.userName}") private String userName; //@Value("${test.user.userPassword}") private String userPassword; //@Value("${test.user.laBigDecimal}") private BigDecimal laBigDecimal; //@Value("${test.user.maps}") X 不行會報錯 private Map<String, Object> maps; //@Value("${test.user.lists}") private List<Object> lists; //@Value("${test.user.department}") X 不行會報錯 private Department department; /** * @return id */ public Integer getId() { return id; } /** * @param id セットする id */ public void setId(Integer id) { this.id = id; } /** * @return userName */ public String getUserName() { return userName; } /** * @param userName セットする userName */ public void setUserName(String userName) { this.userName = userName; } /** * @return userPassword */ public String getUserPassword() { return userPassword; } /** * @param userPassword セットする userPassword */ public void setUserPassword(String userPassword) { this.userPassword = userPassword; } /** * @return laBigDecimal */ public BigDecimal getLaBigDecimal() { return laBigDecimal; } /** * @param laBigDecimal セットする laBigDecimal */ public void setLaBigDecimal(BigDecimal laBigDecimal) { this.laBigDecimal = laBigDecimal; } /** * @return maps */ public Map<String, Object> getMaps() { return maps; } /** * @param maps セットする maps */ public void setMaps(Map<String, Object> maps) { this.maps = maps; } /** * @return lists */ public List<Object> getLists() { return lists; } /** * @param lists セットする lists */ public void setLists(List<Object> lists) { this.lists = lists; } /** * @return department */ public Department getDepartment() { return department; } /** * @param department セットする department */ public void setDepartment(Department department) { this.department = department; } @Override public String toString() { return "User [id=" + id + ", userName=" + userName + ", userPassword=" + userPassword + ", laBigDecimal=" + laBigDecimal + ", maps=" + maps + ", lists=" + lists + ", department=" + department + "]"; } }
I18nSpringbootDemo1Application類:
package com.example.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * 應用啟動類 * */ @SpringBootApplication public class I18nSpringbootDemo1Application { public static void main(String[] args) { SpringApplication.run(I18nSpringbootDemo1Application.class, args); } }
單元測試類I18nSpringbootDemo1ApplicationTests:
package com.example.demo; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import com.example.demo.obj.User; @SpringBootTest class I18nSpringbootDemo1ApplicationTests { @Autowired User user; @Test void contextLoads() { System.out.println(user.toString()); } }
啟動:
結果:
User [id=12, userName=zhansan, userPassword=PWD123, laBigDecimal=138.3, maps={key1=V1, key2=123}, lists=[a12, a13, sdf], department=Department [depCode=dep001, depName=depName001]]
感謝各位的閱讀,以上就是“SpringBoot如何獲取application.properties中自定義的值”的內容了,經過本文的學習后,相信大家對SpringBoot如何獲取application.properties中自定義的值這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關知識點的文章,歡迎關注!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。