SpringBoot?屬性配置中獲取值的方式
SpringBoot 屬性配置中獲取值
在配置文件中定義屬性的值,然后再獲取,在這里做一個總結(jié),首先,在application.yml文件中定義端口和屬性的值,當然,在.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é)果:
如果當屬性很多之后,要寫很多的@Value 的注解嘛???答案當然是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ū)分
當在項目開發(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標記在方法上,該方式將方法的返回值添加到容器中,容器中組件的ID默認是方法名
總結(jié):
(1)@ConfigurationProperties與@Value
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
logback的DuplicateMessageFilter日志過濾操作源碼解讀
這篇文章主要為大家介紹了logback的DuplicateMessageFilter日志過濾操作源碼解讀,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-11-11springBoot集成redis(jedis)的實現(xiàn)示例
Redis是我們Java開發(fā)中,使用頻次非常高的一個nosql數(shù)據(jù)庫,本文主要介紹了springBoot集成redis(jedis)的實現(xiàn)示例,具有一定的參考價值,感興趣的可以了解一下2023-09-09詳解如何讓Spring MVC顯示自定義的404 Not Found頁面
這篇文章主要介紹了詳解如何讓Spring MVC顯示自定義的404 Not Found頁面,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-10-10Spring 框架實現(xiàn)賬戶轉(zhuǎn)賬功能(推薦)
通過本文的介紹,我們了解了如何使用Spring框架實現(xiàn)一個簡單的賬戶轉(zhuǎn)賬功能,主要使用了 Spring 的依賴注入、和事務(wù)管理功能,保證了轉(zhuǎn)賬操作的原子性和數(shù)據(jù)的一致性,感興趣的朋友跟隨小編一起看看吧2025-07-07java開發(fā)RocketMQ消息中間件原理基礎(chǔ)詳解
最近 RocketMQ 剛剛上生產(chǎn)環(huán)境,閑暇之時在這里做一些分享,主要目的是讓初學者能快速上手RocketMQ,有需要的朋友可以借鑒參考下,希望能夠有所幫助2021-11-11