樱花视频在线观看-西西人体大胆4444ww张筱雨-久久网免费视频-国产99页-91高清视频在线-日日干夜夜干-91社区视频-中文高清av-久久成人国产-亚洲日韩欧洲乱码av夜夜摸-97人人射-亚洲视频观看-理论片亚洲-亚洲精品99999-免费能看的黄色片-精人妻无码一区二区三区-奇米影视播放器

游戲產(chǎn)業(yè)研究網(wǎng)

SpringBoot屬性配置文件數(shù)據(jù)注入配置和yml與properties區(qū)別

前言我們知道SpringBoot通過(guò)配置類來(lái)解放一堆的xml文件配置,通屬性配置文件,來(lái)進(jìn)行,系統(tǒng)全局屬性配置,這樣極大的簡(jiǎn)化了我們開發(fā)過(guò)程,javaweb也可以甜甜的從此。

快速配置SpringBoot默認(rèn)加載支持application*.properties、application*.yaml和application*.yml三種拓展名結(jié)尾的全局屬性配置文件處理

它們順序優(yōu)先級(jí)為:application*.properties>application*.yaml>application*.yml

即在application.properties或application.yml等文件中添加屬性配置

可以使用@Value注解將屬性值注入到beans中,或使用@ConfigurationProperties注解將屬性值綁定到結(jié)構(gòu)化的beans中

@Value是Spring框架提供的注解,用來(lái)讀取配置文件中的屬性并逐個(gè)注入到Bean對(duì)象對(duì)應(yīng)的屬性中,SpringBoot框架對(duì)Spring框架的@Value注解進(jìn)行了默認(rèn)繼承

在resources文件下新增application.properties文件,配置對(duì)應(yīng)的屬性

student.name=kenxstudent.age=23新增javabean把對(duì)應(yīng)的屬性注入到j(luò)avabean中對(duì)應(yīng)字段使用@Value注解將屬性值注入到對(duì)應(yīng)屬性上。

@Component@DatapublicclassUser{@Value(${student.name})privateStringname;@Value(${student.age})privateIntegerage;}@Component添加到springioc容器中,@Data添加getter,setter

寫用例測(cè)試

@SpringBootTest(webEnvironment=SpringBootTest.WebEnvironment.NONE,classes=cn.soboys.kmall.api.ApiApplication.class)publicclassPropertiesTest{@AutowiredprivateUserproperties;@Testpublicvoida(){Stringa=String.format(studentnameis%sstudentageis%s,properties.getName(),properties.getAge());System.out.println(a);}}我看可以看到控制臺(tái)正常打印,數(shù)據(jù)注入成功

:53:02INFObackground-preinitorg.hibernate.validator.internal.util.VersionHV000001:HibernateValidator6.1.7.Final:53:02INFOmainPropertiesTestStartingPropertiesTestusingJava1.8.0_202onxiangyongdeMacBook-Pro.localwithPID45463(startedbyxiangyongin/Users/xiangyong/selfProject/project/kmall/kmall-api):53:02INFOmainPropertiesTestThefollowingprofilesareactive:test,mptest__|___|_.____|_|||\/|_)(_|||_\|_)||_|_\/|3.4.1:53:08INFOmainPropertiesTestStartedPropertiesTestin6.132seconds(JVMrunningfor7.783)studentnameiskenxstudentageis23@ConfigurationProperties注解將屬性值綁定到結(jié)構(gòu)化的beans

上面通過(guò)@Value一個(gè)·一個(gè)注入很不方便

@Component@Data@ConfigurationProperties(prefix=student)publicclassUser{privateStringname;privateIntegerage;}這樣極大簡(jiǎn)化代碼,對(duì)于屬性比較多,結(jié)構(gòu)化bean,很有必要可以通過(guò)@ConfigurationProperties(prefix=student)這種方式指定前綴

當(dāng)然有時(shí)候我們需要自定義加載屬性配置文件使用@PropertySource加載配置文件

test.id=100test.name=lucypackagecom.lzx.springboot01demo.pojo;importorg.springframework.boot.context.properties.ConfigurationProperties;importorg.springframework.boot.context.properties.EnableConfigurationProperties;importorg.springframework.context.annotation.Configuration;importorg.springframework.context.annotation.PropertySource;@Configuration//自定義配置類@PropertySource(classpath:test.properties)//指定自定義配置文件位置和名稱@EnableConfigurationProperties(MyProperties.class)//開啟對(duì)應(yīng)配置類的屬性注入功能@ConfigurationProperties(prefix=test)//指定配置文件注入屬性前綴publicclassMyProperties{privateIntegerid;privateStringname;//省略getter/setter方法//省略toString()方法}1.@Configuration注解表示當(dāng)前類是一個(gè)自定義配置類,并添加為Spring容器的組件,也可使用傳統(tǒng)的@Component注解

@PropertySource(classpath:test.properties)指定自定義配置文件位置和名稱

@ConfigurationProperties(prefix=test)指定將配置文件中前綴為test的屬性注入到配置類的屬性中

@EnableConfigurationProperties(MyProperties.class)表示開啟對(duì)應(yīng)配置類的屬性注入功能,如果配置類上使用的是@Component注解而非@Configuration,@EnableConfigurationProperties(MyProperties.class)注解可以省略

application.properties配置文件#配置數(shù)字person.id=1#配置字符串person.name=tom#配置List集合person.hoby=吃飯,睡覺,打豆豆#配置String[]數(shù)組person.family=father,mother#配置map集合person.map.k1=v1person.map.k2=v2#配置對(duì)象type屬性person.pet.type=dog#配置對(duì)象name屬性person.pet.name=旺財(cái)application.y(a)ml配置文件value值為普通數(shù)據(jù)類型(例如:數(shù)字、字符串、布爾)

server:port:8081path:/hellovalue值為數(shù)組或單列集合

主要有兩種寫法:縮進(jìn)式寫法和行內(nèi)式寫法;其中縮進(jìn)式寫法又有兩種寫法:

縮進(jìn)式寫法1:

person:hobby:-play-read-sleep縮進(jìn)式寫法2:

@Component@DatapublicclassUser{@Value(${student.name})privateStringname;@Value(${student.age})privateIntegerage;}0行內(nèi)式寫法:

@Component@DatapublicclassUser{@Value(${student.name})privateStringname;@Value(${student.age})privateIntegerage;}1value值為Map或?qū)ο?/p>

縮進(jìn)式寫法:

@Component@DatapublicclassUser{@Value(${student.name})privateStringname;@Value(${student.age})privateIntegerage;}2行內(nèi)式寫法:

@Component@DatapublicclassUser{@Value(${student.name})privateStringname;@Value(${student.age})privateIntegerage;}3注意使用SpringBoot全局配置文件設(shè)置屬性時(shí),

如果配置的屬性是已有屬性,例如服務(wù)端口server.port,那么SpringBoot會(huì)掃描并讀取這些配置屬性,覆蓋已有的默認(rèn)配置;如果配置的是自定義屬性,則還需要在程序中注入這些配置屬性方可生效

默認(rèn)屬性和參數(shù)引用SpringBoot屬性配置文件中默認(rèn)給我們提供了一些特有的全局屬性參數(shù)值我們可以直接獲取

使用SpringBoot內(nèi)嵌的RandomValuePropertySource類進(jìn)行隨機(jī)值注入。

@Component@DatapublicclassUser{@Value(${student.name})privateStringname;@Value(${student.age})privateIntegerage;}4當(dāng)然我們也可以自定義引用自己定義的值

@Component@DatapublicclassUser{@Value(${student.name})privateStringname;@Value(${student.age})privateIntegerage;}5作者:kenx

汉阴县| 万盛区| 永仁县| 芒康县| 磐安县| 客服| 龙南县| 鲁甸县| 义马市| 新干县| 罗平县| 上思县| 大姚县| 宜阳县| 平乐县| 涡阳县| 杭锦旗| 峨边| 龙里县| 安溪县| 天祝| 噶尔县| 西乌珠穆沁旗| 屏边| 边坝县| 汉源县| 沙坪坝区| 晋城| 昌都县| 科尔| 阳曲县| 乐至县| 河曲县| 瑞金市| 古丈县| 屏南县| 英山县| 开平市| 岚皋县| 晋州市| 莱州市|