SpringBoot如何获取application.properties中自定义的值
程序员文章站
2022-03-04 14:58:51
目录结构:pom文件:
目录结构:
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获取自定义值内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
推荐阅读
-
js获取自定义属性的值(如何获取select框的值)
-
js获取自定义属性的值(如何获取select框的值)
-
JS中如何快速获取数组中的最大值最小值
-
如何更优雅地获取spring boot yml中的值
-
小程序之页面如何获取自定义组件中的值
-
Springboot如何获取配置文件application.yml中自定义的变量并使用
-
SpringBoot如何获取application.properties中自定义的值
-
struts2中action如何获取Session,jsp页面参数等等信息的值
-
Android中自定义xml文件给Spinner下拉框赋值并获取下拉选中的值
-
请问wordpress中如何获取自定义post_type的分类