SpringBoot?屬性配置中獲取值的方式
SpringBoot 屬性配置中獲取值
在配置文件中定義屬性的值,然后再獲取,在這里做一個(gè)總結(jié),首先,在application.yml文件中定義端口和屬性的值,當(dāng)然,在.application.properties文件中也能定義,只是格式不同而已:
appliaction.yml:
server: port: 8081 cubSize: B age: 18
然后再定義一個(gè)controller,用@Value這個(gè)注解來(lái)獲取到值:
package com.dist.tr.controller; import com.dist.tr.entity.GrilProperties; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping public class BeautifulGirlContrller { @Value("${cubSize}") private String cubSize; @Value("${age}") private Integer age; @RequestMapping(value = "/gril",method = RequestMethod.GET) public String HelloGril(){ return cubSize + age; } }
運(yùn)行結(jié)果:
如果當(dāng)屬性很多之后,要寫很多的@Value 的注解嘛???答案當(dāng)然是No,有簡(jiǎn)便的寫法:
application.yml 文件
server: port: 8081 gril: name: lisa height: 165
首先,定義一個(gè)實(shí)體類去寫屬性
GrilProperties實(shí)體:
注意我們用到了這個(gè)注解:@ConfigurationProperties(prefix = “gril”)
package com.dist.tr.entity; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; @Component @ConfigurationProperties(prefix = "gril") public class GrilProperties { private String name; private String height; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getHeight() { return height; } public void setHeight(String height) { this.height = height; } }
在controller 中的寫法:
首先用注解@Autowired 注入這個(gè)實(shí)體,如果不在實(shí)體中加@Component這個(gè)注解,在idea中發(fā)現(xiàn)會(huì)有紅線出現(xiàn)。
package com.dist.tr.controller; import com.dist.tr.entity.GrilProperties; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping public class BeautifulGirlContrller { @Autowired private GrilProperties grilProperties; @RequestMapping("/grilPerproties") public String grilPerproties(){ return grilProperties.getName()+grilProperties.getHeight(); } }
運(yùn)行結(jié)果:
這樣就不會(huì)需要去寫太多的@Value注解了。
還有中形式,就是在配置文件中也可以有這種情況出現(xiàn):
server: port: 8081 cubSize: B age: 18 context: "cubSize:${cubSize},age:${age}"
這種情況證明獲取的屬性值呢?
在controller中編碼:
package com.dist.tr.controller; import com.dist.tr.entity.GrilProperties; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping public class BeautifulGirlContrller { @Value("${context}") private String context; @RequestMapping("/grilSize") public String girlcubSize(){ return context ; } }
運(yùn)行結(jié)果:
測(cè)試和生產(chǎn)區(qū)分
當(dāng)在項(xiàng)目開(kāi)發(fā)的時(shí)候,如果區(qū)分測(cè)試和生產(chǎn)環(huán)境,那么就得區(qū)分開(kāi)application.yml 文件:
新建application-dev.yml 文件和application-prod.yml文件:
然后在使用測(cè)試或者是生產(chǎn)的時(shí)候,application.yml 文件中這樣寫:
spring: profiles: active: prod
決定是用測(cè)試環(huán)境還是生產(chǎn)環(huán)境。
SpringBoot 獲取值和配置文件
@ConfigurationProperties、@Value、@PropertySource、@ImportResource和@Bean
1、@ConfigurationProperties(prefix = "student")方式
(1)定義兩個(gè)實(shí)體類,其中student實(shí)體類的屬性包括Course類:
@Data @Component @ConfigurationProperties(prefix = "student")//告訴springboot將本類中的所有屬性和配置文件的相關(guān)配置進(jìn)行綁定 public class Student { ? ? ? //prefix:配置文件中哪一個(gè)名稱下面的屬性進(jìn)行一一映射 ? ? private String sname; ? ? private int age; ? ? private Map<String,Object> maps; ? ? private List<Object> list; ? ? private Course course; } @Data public class Course { ? ? private String courseno; ? ? private String coursename; }
(2)創(chuàng)建yaml配置文件:
student: ? sname: zhai ? age: 12 ? maps: {k1: 12,k2: 13} ? list: ? ? - zhai ? ? - zhang ? course: ? ? courseno: 202007 ? ? coursename: javaweb
(3)創(chuàng)建properties文件:
#配置student student.age=12 student.sname=zhai student.maps.k1=1 student.maps.k2=2 student.list=a,b,c student.course.courseno=202007 student.course.coursename=java
(4)測(cè)試類:
//可以在測(cè)試期間很方便地類似編碼一樣進(jìn)行自動(dòng)注入等容器的功能 @SpringBootTestclass Springboot03ApplicationTests { ? ? @Autowired ? ? Student student; ? ? @Test ? ? void contextLoads() { ? ? ? ? System.out.println(student); ? ? } }
(5)需要導(dǎo)入的依賴:配置文件處理器,配置文件進(jìn)行綁定會(huì)有提示
? ?<dependency> ? ? ? ? ? ? <groupId>org.springframework.boot</groupId> ? ? ? ? ? ? <artifactId>spring-boot-configuration-processor</artifactId> ? ? ? ? ? ? <version>2.2.1.RELEASE</version> ? ?</dependency>
2、@Value方式
(1)書寫配置文件
#配置student student.sname=zhai student.age=12 student.maps.k1=1 student.maps.k2=2 student.list=a,b,c student.course.courseno=202007 student.course.coursename=java
(2)獲取值:
@Data @Component public class Student { ? ? @Value("${student.sname}") ? ? private String sname; ? ? @Value("#{2*9}") ? ? private int age; ? ? private Map<String,Object> maps; ? ? private List<Object> list; ? ? private Course course; }
(3)@ConfigurationProperties(prefix = "")方式與@Value方式的比較
- @ConfigurationProperties(prefix = "")方式支持批量注入配置文件的屬性,@Value方式需要一個(gè)個(gè)指定
- @ConfigurationProperties(prefix = "")方式支持松散綁定,@Value方式不支持,
yml中寫的last-name,這個(gè)和lastName是一樣的,后面跟著的字母默認(rèn)是大寫的。這就是松散綁定
@Value方式支持JSR303校驗(yàn)
@Data @Component @Validated public class Student { ? ? @NonNull ? ? private String sname; ? ? private int age; ? ? private Map<String,Object> maps; ? ? private List<Object> list; ? ? private Course course; }
@Value方式支持SpEl
如果我們只是在某一個(gè)業(yè)務(wù)邏輯中需要獲取配置文件的某一項(xiàng)值,可以使用@Value,如果是一個(gè)javaBean來(lái)和配置文件進(jìn)行映射,則要使用@ConfigurationProperties(prefix = "")方式
@RestController public class HelloController { ? ? @Value("${student.sname}") ? ? private String sname; ? ? @RequestMapping("/hello") ? ? public String hello(){ ? ? ? ? return "hello"+sname; ? ? } }
配置文件:
#配置student student.sname=zhai student.age=12 student.maps.k1=1 student.maps.k2=2 student.list=a,b,c student.course.courseno=202007 student.course.coursename=java
3、@PropertySource
(1)配置文件(student.properties)
#配置student student.sname=zhai student.age=12 student.maps.k1=1 student.maps.k2=2 student.list=a,b,c student.course.courseno=202007 student.course.coursename=java
(2)實(shí)體類獲取值
@Data @Component @PropertySource(value = {"classpath:student.properties"}) public class Student { ? ? private String sname; ? ? private int age; ? ? private Map<String,Object> maps; ? ? private List<Object> list; ? ? private Course course; }
@PropertySource是從指定路徑下獲取數(shù)據(jù),默認(rèn)是從application.properties下獲取數(shù)據(jù)
4、@ImportResource和@Bean
(1)指定spring的配置文件
@SpringBootApplication(scanBasePackages = "com") @ImportResource(locations = {"classpath:beans.xml"}) public class Springboot02Application { ? ? public static void main(String[] args) { ? ? ? ? SpringApplication.run(Springboot02Application.class, args); ? ? } }
(2)書寫spring的配置文件:beans.xml
(3)書寫如下配置,可以省略配置文件的書寫,用注解來(lái)代替
@Configuration public class MyAppConfig { ? ? @Bean ? ? public HelloService helloService(){ ? ? ? ? return new HellService(); ? ? } }
@Configuration說(shuō)明這是一個(gè)配置類,就是在替代之前的配置文件
@Bean標(biāo)記在方法上,該方式將方法的返回值添加到容器中,容器中組件的ID默認(rèn)是方法名
總結(jié):
(1)@ConfigurationProperties與@Value
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
利用github搭建個(gè)人maven倉(cāng)庫(kù)的方法步驟
這篇文章主要介紹了利用github搭建個(gè)人maven倉(cāng)庫(kù)的方法步驟,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-12-12手動(dòng)部署java項(xiàng)目到k8s中的實(shí)現(xiàn)
本文主要介紹了手動(dòng)部署java項(xiàng)目到k8s中的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08Java微信二次開(kāi)發(fā)(一) Java微信請(qǐng)求驗(yàn)證功能
這篇文章主要為大家詳細(xì)介紹了Java微信二次開(kāi)發(fā)第一篇,Java微信請(qǐng)求驗(yàn)證功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-04-04Java實(shí)現(xiàn)京東聯(lián)盟API數(shù)據(jù)獲取功能
這篇文章介紹了Java獲取京東聯(lián)盟API數(shù)據(jù)的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-07-07擴(kuò)展Hibernate使用自定義數(shù)據(jù)庫(kù)連接池的方法
這篇文章主要介紹了擴(kuò)展Hibernate使用自定義數(shù)據(jù)庫(kù)連接池的方法,涉及Hibernate數(shù)據(jù)庫(kù)操作擴(kuò)展的相關(guān)技巧,需要的朋友可以參考下2016-03-03Java讀取properties配置文件時(shí),出現(xiàn)中文亂碼的解決方法
下面小編就為大家?guī)?lái)一篇Java讀取properties配置文件時(shí),出現(xiàn)中文亂碼的解決方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-11-11Java Socket實(shí)現(xiàn)聊天室附1500行源代碼
Socket是應(yīng)用層與TCP/IP協(xié)議族通信的中間軟件抽象層,它是一組接口。本篇文章手把手帶你通過(guò)Java Socket來(lái)實(shí)現(xiàn)自己的聊天室,大家可以在過(guò)程中查缺補(bǔ)漏,溫故而知新2021-10-10springboot跨域過(guò)濾器fetch react Response to p
這篇文章主要介紹了springboot跨域過(guò)濾器fetch react Response to preflight request doesn‘t pass access control check問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03