SpringBoot如何獲取application.properties中自定義的值
目錄結(jié)構(gòu):
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> <!-- 導(dǎo)入配置文件處理器,配置文件進(jìn)行綁定就會(huì)提示 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency> <!--校驗(yàn)依賴(lài)--> <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 #也可以寫(xiě)成 test.user.user-name=zhangsan test.user.userName=zhansan #也可以寫(xiě)成 test.user.user-password=XXX test.user.userPassword=PWD123 #也可以寫(xiě)成 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類(lèi):
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類(lèi):
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; /* * 將配置文件的每一個(gè)屬性值,映射到這個(gè)組件中: * ①@ConfigurationProperties:告訴SpringBoot將本類(lèi)中的所有屬性和配置文件中相關(guān)的配置進(jìn)行綁定; * prefix = "test.user":將配置文件中前綴為test.user下面的所有屬性進(jìn)行一一映射 * 只有這個(gè)組件是容器中的組件,才能提供@ConfigurationProperties的功能,所以要加@Component * * ②@Value("${key}")從環(huán)境變量、配置文件中獲取值 * @Value("#{SpEl}")表達(dá)式 * * @ConfigurationProperties與@Value的區(qū)別: * @ConfigurationProperties支持松散語(yǔ)法,JSR303數(shù)據(jù)校驗(yàn),復(fù)雜類(lèi)型封裝,不支持SpEL * @Value支持SpEL,不支持松散語(yǔ)法,JSR303數(shù)據(jù)校驗(yàn),復(fù)雜類(lèi)型封裝 * 如果說(shuō),我們?cè)谀硞€(gè)業(yè)務(wù)邏輯中需要獲取一下配置文件中的某項(xiàng)值,可以用@Value * 如果說(shuō),我們專(zhuān)門(mén)編寫(xiě)了一個(gè)javaBean去和配置文件進(jìn)行映射,我們直接使用@ConfigurationProperties */ @Component @ConfigurationProperties(prefix = "test.user") @Validated public class User { //@Value("#{10*2}") private Integer id; //@Email userName必須輸入郵箱格式的值,要不然報(bào)錯(cuò) //@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 不行會(huì)報(bào)錯(cuò) private Map<String, Object> maps; //@Value("${test.user.lists}") private List<Object> lists; //@Value("${test.user.department}") X 不行會(huì)報(bào)錯(cuò) 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類(lèi):
package com.example.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * 應(yīng)用啟動(dòng)類(lèi) * */ @SpringBootApplication public class I18nSpringbootDemo1Application { public static void main(String[] args) { SpringApplication.run(I18nSpringbootDemo1Application.class, args); } }
單元測(cè)試類(lèi)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()); } }
啟動(dòng):
結(jié)果:
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]]
到此這篇關(guān)于SpringBoot獲取application.properties中的自定義的值的文章就介紹到這了,更多相關(guān)SpringBoot獲取自定義值內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
JAVA中string數(shù)據(jù)類(lèi)型轉(zhuǎn)換詳解
在JAVA中string是final類(lèi),提供字符串不可以修改,string類(lèi)型在項(xiàng)目中經(jīng)常使用,下面給大家介紹了string七種數(shù)據(jù)類(lèi)型轉(zhuǎn)換,需要的朋友可以參考下2015-07-07使用springboot activiti關(guān)閉驗(yàn)證自動(dòng)部署方式
這篇文章主要介紹了使用springboot activiti關(guān)閉驗(yàn)證自動(dòng)部署方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09詳解SpringBoot中的index首頁(yè)的訪問(wèn)、自定義Favicon圖標(biāo)
這篇文章主要介紹了SpringBoot中的index首頁(yè)的訪問(wèn)、自定義Favicon圖標(biāo),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-08-08Java中String判斷值為null或空及地址是否相等的問(wèn)題
這篇文章主要介紹了Java中String判斷值為null或空及地址是否相等的問(wèn)題,文中舉了簡(jiǎn)單的例子對(duì)字符串類(lèi)型的值和地址問(wèn)題進(jìn)行講解,需要的朋友可以參考下2016-01-01SpringBoot項(xiàng)目中遇到的BUG問(wèn)題及解決方法
這篇文章主要介紹了SpringBoot項(xiàng)目中遇到的BUG問(wèn)題及解決方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-11-11Spring?Boot中自動(dòng)執(zhí)行sql腳本的方法實(shí)例
在SpringBoot的架構(gòu)中,DataSourceInitializer類(lèi)可以在項(xiàng)目啟動(dòng)后初始化數(shù)據(jù),我們可以通過(guò)自動(dòng)執(zhí)行自定義sql腳本初始化數(shù)據(jù),下面這篇文章主要給大家介紹了關(guān)于Spring?Boot中自動(dòng)執(zhí)行sql腳本的相關(guān)資料,需要的朋友可以參考下2022-01-01Java Stopwatch類(lèi),性能與時(shí)間計(jì)時(shí)器案例詳解
這篇文章主要介紹了Java Stopwatch類(lèi),性能與時(shí)間計(jì)時(shí)器案例詳解,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-09-09java實(shí)現(xiàn)的2048游戲完整實(shí)例
這篇文章主要介紹了java實(shí)現(xiàn)的2048游戲,結(jié)合完整實(shí)例形式分析了java實(shí)現(xiàn)2048游戲功能的相關(guān)數(shù)值運(yùn)算、swing組件布局、事件響應(yīng)等相關(guān)操作技巧,需要的朋友可以參考下2018-01-01java實(shí)現(xiàn)字符串like和not?like的使用示例
在Java中,我們經(jīng)常需要對(duì)字符串進(jìn)行模式匹配操作,字符串的模式匹配通常使用like和not?like這兩個(gè)運(yùn)算符進(jìn)行,本文就來(lái)介紹一下如何實(shí)現(xiàn),感興趣的可以了解一下2023-09-09