SpringBoot怎么配置文件给实体注入值
这篇文章主要介绍了SpringBoot怎么配置文件给实体注入值的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇SpringBoot怎么配置文件给实体注入值文章都会有所收获,下面我们一起来看看吧。
配置文件给实体注入值SpringBoot 默认的全局配置文件有两个 application.properties 和application.yml
properties用法#例如修改端口号直接赋值server.port=8888yaml用法
#例如修改端口号server:port:8888
YML中赋值是以键值对的形式,且利用缩进表示层级关系,同一级缩进必须一致
大小写敏感,支持松散写法,且冒号后面必须有一个空格
通过配置文件给实体类注入属性//实体类@Component//把实体类放入到spring容器中@ConfigurationProperties(prefix="student")//识别配置文件publicclassStudent{privateStringname;privateintage;privatebooleansex;privateDatebirthday;privateMap<String,Object>location;privateString[]hobbies;privateList<String>skills;privatePetpet;//....set/get/toString}
#yml配置文件student:name:zsage:23sex:truebirthday:2020/10/1location:{province:湖北,city:武汉,zone:洪山}hobbies:[足球,篮球]skills:[编程,金融]pet:nickName:wcstrain:hsq
在这里注意不同数据类型的写法,简单的数据直接赋值
#map类型和对象类型的两种写法#1.行内写法location:{province:湖北,city:武汉,zone:洪山}pet:{nickName:wc,strain:hsq}#2.分行写location:province:湖北city:武汉zone:洪山pet:nickName:wcstrain:hsq
#数组、list、set#1、行内写法,其中中括号还可以省略hobbies:[足球,篮球]skills:[编程,金融]#2、分行写hobbies:-足球-篮球skills:-编程-金融
到 SpringBoot 测试文件中测试输出
@AutowiredStudentstudent;@TestvoidcontextLoads(){System.out.println(student);}
结果
上面yml中注意Student{name='zs', age=23, sex=true, birthday=Thu Oct 01 00:00:00 CST 2020, location={province=湖北, city=武汉, zone=洪山}, hobbies=[足球,篮球], skills=[编程, 金融], pet=Pet{nickName='wc', strain='hsq'}}
#如果对字符串加单引号,双引号会有什么区别location:{province:"湖北",city:'武汉',zone:洪山}#如果引号中只是字符串时没什么区别location:{province:"湖\n北",city:'武\n汉',zone:洪\n山}#此时双引号中的转义字符会被识别,会换行,单引号和不加引号会原样输出也可以使用properties注入
student.name=lsstudent.age=24...
其中properties文件和yml文件中的值可以互相引用。两个配置文件可以互补,properties文件优先级高于yml
另外如果要注入的值不在这两个默认配置文件中,这是需要使用@PropertySource注解,遗憾的是该注解只支持properties文件,不支持yml文件
@PropertySource(value={"classpath:conf.properties"})publicclassStudent{...配置文件值注入方式1.1 从全局配置文件中读取配置到实体类 (@ConfigurationProperties)
配置文件 (application.yml)(写在application.properties也可以,格式变一下即可)
person:lastName:Maryage:16birthDate:2004/01/01maps:{one:1,two:2,three:3}lists:-1-2-3pet:name:wangcaiage:3
实体类(javaBean):
/***将配置文件中配置的每一个属性的值,映射到这个组件中**@Component:*只有这个组件是容器中的组件,才能容器提供的@ConfigurationProperties功能;**@ConfigurationProperties:*告诉SpringBoot将本类中的所有属性和配置文件中相关的配置进行绑定(默认配置文件为application.properties与*application.yml);**prefix="person":*配置文件中哪个下面的所有属性进行一一映射*/@Component@ConfigurationProperties(prefix="person")publicclassPerson{privateStringlastName;privateIntegerage;privateDatebirthDate;privateMap<String,Object>maps;privateList<Object>lists;privatePetpet;//省略get,set方法@OverridepublicStringtoString(){StringmapValue="";if(this.maps!=null){for(Stringkey:this.maps.keySet()){Objectvalue=this.maps.get(key);mapValue+=key+"\t"+value+"\t";}}return"lastName:"+this.lastName+"\nage:"+this.age+"\nbirthDate:"+this.birthDate+"\nmaps:"+mapValue+"\nlists:"+this.lists+this.pet;}}
publicclassPet{privateStringname;privateIntegerage;//省略get,set方法@OverridepublicStringtoString(){return"\nPet:\n\tPetname:"+this.name+"\n\tPetage:"+this.age+"\n";}}
我们可以导入配置文件处理器,以后编写配置就有提示了(pom.xml)
<!--导入配置文件处理器,配置文件进行绑定就会有提示--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-configuration-processor</artifactId><optional>true</optional></dependency>
测试
@SpringBootTestclassDemoApplicationTests{@AutowiredPersonperson;@TestvoidcontextLoads(){System.out.println(person.toString());}}1.2 全局配置文件注入值(@Value)
@Value 其实是Spring中的注解,其功能使用xml文件描述是这样的:
<beanclass="Person"><propertyname="lastName"value="字面量/${key}从环境变量、配置文件中获取值/#{SpEL}"></property><bean/>
其中property表示的是Person类中的各项属性,name用于指定具体属性,value用于指定值。
value的值主要有3种:
字面量,也就是数字(1,2…)、字符串(abc)、布尔变量(true、false)等常量
${key},从环境变量、配置文件中获取值
#{SpEL}, Spring的表达式语言,可以执行一些计算,调用一些函数
@ComponentpublicclassPerson{@Value("${person.last-name}")//@Value("#{'Lily'.toUpperCase()}")privateStringlastName;@Value("#{11*2}")privateIntegerage;privateDatebirthDate;privateMap<String,Object>maps;privateList<Object>lists;privatePetpet;//省略get,set和toString}1.3 从指定文件读取并配置实体类(@PropertySource+@ConfigurationProperties)
在 resources 目录下创建 person.properties
person.lastName=Lilyperson.age=20person.birthDate=2000/01/01person.maps.one=1person.maps.two=2person.lists=a,b,chperson.pet.name=wangcaiperson.pet.age=3
更改 Person 类中的注解
@Component@PropertySource(value={"classpath:person.properties"})@ConfigurationProperties(prefix="person")publicclassPerson{//...省略属性,get,set,toString}
要注意的是,PropertySource 只支持properties文件,不支持yml文件读取。
1.4 从指定文件读取并注入值(@PropertySource+@Value)创建配置文件
更改注释
@Component@PropertySource(value={"classpath:person.properties"})publicclassPerson{@Value("${person.last-name}")privateStringlastName;//...省略属性,get,set,toString}
关于“SpringBoot怎么配置文件给实体注入值”这篇文章的内容就介绍到这里,感谢各位的阅读!相信大家对“SpringBoot怎么配置文件给实体注入值”知识都有一定的了解,大家如果还想学习更多知识,欢迎关注亿速云行业资讯频道。
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。