SpringBoot?屬性配置中獲取值的方式
SpringBoot 屬性配置中獲取值
在配置文件中定義屬性的值,然后再獲取,在這里做一個總結(jié),首先,在application.yml文件中定義端口和屬性的值,當(dāng)然,在.application.properties文件中也能定義,只是格式不同而已:
appliaction.yml:
server: port: 8081 cubSize: B age: 18
然后再定義一個controller,用@Value這個注解來獲取到值:
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; } }
運行結(jié)果:
如果當(dāng)屬性很多之后,要寫很多的@Value 的注解嘛???答案當(dāng)然是No,有簡便的寫法:
application.yml 文件
server: port: 8081 gril: name: lisa height: 165
首先,定義一個實體類去寫屬性
GrilProperties實體:
注意我們用到了這個注解:@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 注入這個實體,如果不在實體中加@Component這個注解,在idea中發(fā)現(xiàn)會有紅線出現(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(); } }
運行結(jié)果:
這樣就不會需要去寫太多的@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 ; } }
運行結(jié)果:
測試和生產(chǎn)區(qū)分
當(dāng)在項目開發(fā)的時候,如果區(qū)分測試和生產(chǎn)環(huán)境,那么就得區(qū)分開application.yml 文件:
新建application-dev.yml 文件和application-prod.yml文件:
然后在使用測試或者是生產(chǎn)的時候,application.yml 文件中這樣寫:
spring: profiles: active: prod
決定是用測試環(huán)境還是生產(chǎn)環(huán)境。
SpringBoot 獲取值和配置文件
@ConfigurationProperties、@Value、@PropertySource、@ImportResource和@Bean
1、@ConfigurationProperties(prefix = "student")方式
(1)定義兩個實體類,其中student實體類的屬性包括Course類:
@Data @Component @ConfigurationProperties(prefix = "student")//告訴springboot將本類中的所有屬性和配置文件的相關(guān)配置進行綁定 public class Student { ? ? ? //prefix:配置文件中哪一個名稱下面的屬性進行一一映射 ? ? 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)測試類:
//可以在測試期間很方便地類似編碼一樣進行自動注入等容器的功能 @SpringBootTestclass Springboot03ApplicationTests { ? ? @Autowired ? ? Student student; ? ? @Test ? ? void contextLoads() { ? ? ? ? System.out.println(student); ? ? } }
(5)需要導(dǎo)入的依賴:配置文件處理器,配置文件進行綁定會有提示
? ?<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方式需要一個個指定
- @ConfigurationProperties(prefix = "")方式支持松散綁定,@Value方式不支持,
yml中寫的last-name,這個和lastName是一樣的,后面跟著的字母默認是大寫的。這就是松散綁定
@Value方式支持JSR303校驗
@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
如果我們只是在某一個業(yè)務(wù)邏輯中需要獲取配置文件的某一項值,可以使用@Value,如果是一個javaBean來和配置文件進行映射,則要使用@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)實體類獲取值
@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ù),默認是從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)書寫如下配置,可以省略配置文件的書寫,用注解來代替
@Configuration public class MyAppConfig { ? ? @Bean ? ? public HelloService helloService(){ ? ? ? ? return new HellService(); ? ? } }
@Configuration說明這是一個配置類,就是在替代之前的配置文件
@Bean標(biāo)記在方法上,該方式將方法的返回值添加到容器中,容器中組件的ID默認是方法名
總結(jié):
(1)@ConfigurationProperties與@Value
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java微信二次開發(fā)(一) Java微信請求驗證功能
這篇文章主要為大家詳細介紹了Java微信二次開發(fā)第一篇,Java微信請求驗證功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-04-04Java實現(xiàn)京東聯(lián)盟API數(shù)據(jù)獲取功能
這篇文章介紹了Java獲取京東聯(lián)盟API數(shù)據(jù)的方法,文中通過示例代碼介紹的非常詳細。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-07-07擴展Hibernate使用自定義數(shù)據(jù)庫連接池的方法
這篇文章主要介紹了擴展Hibernate使用自定義數(shù)據(jù)庫連接池的方法,涉及Hibernate數(shù)據(jù)庫操作擴展的相關(guān)技巧,需要的朋友可以參考下2016-03-03Java讀取properties配置文件時,出現(xiàn)中文亂碼的解決方法
下面小編就為大家?guī)硪黄狫ava讀取properties配置文件時,出現(xiàn)中文亂碼的解決方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-11-11Java Socket實現(xiàn)聊天室附1500行源代碼
Socket是應(yīng)用層與TCP/IP協(xié)議族通信的中間軟件抽象層,它是一組接口。本篇文章手把手帶你通過Java Socket來實現(xiàn)自己的聊天室,大家可以在過程中查缺補漏,溫故而知新2021-10-10springboot跨域過濾器fetch react Response to p
這篇文章主要介紹了springboot跨域過濾器fetch react Response to preflight request doesn‘t pass access control check問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-03-03