前言我們知道SpringBoot通過配置類來解放一堆的xml文件配置,通屬性配置文件,來進行,系統全局屬性配置,這樣極大的簡化了我們開發過程,javaweb也可以甜甜的從此。
快速配置SpringBoot默認加載支持application*.properties、application*.yaml和application*.yml三種拓展名結尾的全局屬性配置文件處理
它們順序優先級為:application*.properties>application*.yaml>application*.yml
即在application.properties或application.yml等文件中添加屬性配置
可以使用@Value注解將屬性值注入到beans中,或使用@ConfigurationProperties注解將屬性值綁定到結構化的beans中
@Value是Spring框架提供的注解,用來讀取配置文件中的屬性并逐個注入到Bean對象對應的屬性中,SpringBoot框架對Spring框架的@Value注解進行了默認繼承
在resources文件下新增application.properties文件,配置對應的屬性
student.name=kenxstudent.age=23新增javabean把對應的屬性注入到javabean中對應字段使用@Value注解將屬性值注入到對應屬性上。
@Component@DatapublicclassUser{@Value(${student.name})privateStringname;@Value(${student.age})privateIntegerage;}@Component添加到springioc容器中,@Data添加getter,setter
寫用例測試
@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);}}我看可以看到控制臺正常打印,數據注入成功
: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注解將屬性值綁定到結構化的beans
上面通過@Value一個·一個注入很不方便
@Component@Data@ConfigurationProperties(prefix=student)publicclassUser{privateStringname;privateIntegerage;}這樣極大簡化代碼,對于屬性比較多,結構化bean,很有必要可以通過@ConfigurationProperties(prefix=student)這種方式指定前綴
當然有時候我們需要自定義加載屬性配置文件使用@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)//開啟對應配置類的屬性注入功能@ConfigurationProperties(prefix=test)//指定配置文件注入屬性前綴publicclassMyProperties{privateIntegerid;privateStringname;//省略getter/setter方法//省略toString()方法}1.@Configuration注解表示當前類是一個自定義配置類,并添加為Spring容器的組件,也可使用傳統的@Component注解
@PropertySource(classpath:test.properties)指定自定義配置文件位置和名稱
@ConfigurationProperties(prefix=test)指定將配置文件中前綴為test的屬性注入到配置類的屬性中
@EnableConfigurationProperties(MyProperties.class)表示開啟對應配置類的屬性注入功能,如果配置類上使用的是@Component注解而非@Configuration,@EnableConfigurationProperties(MyProperties.class)注解可以省略
application.properties配置文件#配置數字person.id=1#配置字符串person.name=tom#配置List集合person.hoby=吃飯,睡覺,打豆豆#配置String[]數組person.family=father,mother#配置map集合person.map.k1=v1person.map.k2=v2#配置對象type屬性person.pet.type=dog#配置對象name屬性person.pet.name=旺財application.y(a)ml配置文件value值為普通數據類型(例如:數字、字符串、布爾)
server:port:8081path:/hellovalue值為數組或單列集合
主要有兩種寫法:縮進式寫法和行內式寫法;其中縮進式寫法又有兩種寫法:
縮進式寫法1:
person:hobby:-play-read-sleep縮進式寫法2:
@Component@DatapublicclassUser{@Value(${student.name})privateStringname;@Value(${student.age})privateIntegerage;}0行內式寫法:
@Component@DatapublicclassUser{@Value(${student.name})privateStringname;@Value(${student.age})privateIntegerage;}1value值為Map或對象
縮進式寫法:
@Component@DatapublicclassUser{@Value(${student.name})privateStringname;@Value(${student.age})privateIntegerage;}2行內式寫法:
@Component@DatapublicclassUser{@Value(${student.name})privateStringname;@Value(${student.age})privateIntegerage;}3注意使用SpringBoot全局配置文件設置屬性時,
如果配置的屬性是已有屬性,例如服務端口server.port,那么SpringBoot會掃描并讀取這些配置屬性,覆蓋已有的默認配置;如果配置的是自定義屬性,則還需要在程序中注入這些配置屬性方可生效
默認屬性和參數引用SpringBoot屬性配置文件中默認給我們提供了一些特有的全局屬性參數值我們可以直接獲取
使用SpringBoot內嵌的RandomValuePropertySource類進行隨機值注入。
@Component@DatapublicclassUser{@Value(${student.name})privateStringname;@Value(${student.age})privateIntegerage;}4當然我們也可以自定義引用自己定義的值
@Component@DatapublicclassUser{@Value(${student.name})privateStringname;@Value(${student.age})privateIntegerage;}5作者:kenx
