SpringBoot?屬性配置中獲取值的方式
SpringBoot 屬性配置中獲取值
在配置文件中定義屬性的值,然后再獲取,在這里做一個(gè)總結(jié),首先,在application.yml文件中定義端口和屬性的值,當(dāng)然,在.application.properties文件中也能定義,只是格式不同而已:
appliaction.yml:
server: port: 8081 cubSize: B age: 18
然后再定義一個(gè)controller,用@Value這個(gè)注解來獲取到值:
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,有簡便的寫法:
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é)果:

測試和生產(chǎn)區(qū)分
當(dāng)在項(xiàng)目開發(fā)的時(shí)候,如果區(qū)分測試和生產(chǎn)環(huán)境,那么就得區(qū)分開application.yml 文件:
新建application-dev.yml 文件和application-prod.yml文件:
然后在使用測試或者是生產(chǎn)的時(shí)候,application.yml 文件中這樣寫:
spring:
profiles:
active: prod決定是用測試環(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)測試類:
//可以在測試期間很方便地類似編碼一樣進(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來和配置文件進(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)書寫如下配置,可以省略配置文件的書寫,用注解來代替
@Configuration
public class MyAppConfig {
? ? @Bean
? ? public HelloService helloService(){
? ? ? ? return new HellService();
? ? }
}@Configuration說明這是一個(gè)配置類,就是在替代之前的配置文件
@Bean標(biāo)記在方法上,該方式將方法的返回值添加到容器中,容器中組件的ID默認(rèn)是方法名
總結(jié):
(1)@ConfigurationProperties與@Value

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
logback的DuplicateMessageFilter日志過濾操作源碼解讀
這篇文章主要為大家介紹了logback的DuplicateMessageFilter日志過濾操作源碼解讀,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-11-11
Java中synchronized與Lock的詳細(xì)對(duì)比
Lock和synchronized都是Java中用于實(shí)現(xiàn)線程同步的機(jī)制,但它們有一些重要的區(qū)別,這篇文章主要介紹了Java中synchronized與Lock的詳細(xì)對(duì)比,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2025-07-07
Java女裝商城系統(tǒng)的實(shí)現(xiàn)流程
讀萬卷書不如行萬里路,只學(xué)書上的理論是遠(yuǎn)遠(yuǎn)不夠的,只有在實(shí)戰(zhàn)中才能獲得能力的提升,本篇文章手把手帶你用java+SSM+jsp+mysql+maven實(shí)現(xiàn)一個(gè)女裝商城系統(tǒng),大家可以在過程中查缺補(bǔ)漏,提升水平2021-11-11
springBoot集成redis(jedis)的實(shí)現(xiàn)示例
Redis是我們Java開發(fā)中,使用頻次非常高的一個(gè)nosql數(shù)據(jù)庫,本文主要介紹了springBoot集成redis(jedis)的實(shí)現(xiàn)示例,具有一定的參考價(jià)值,感興趣的可以了解一下2023-09-09
java獲取當(dāng)前時(shí)間和前一天日期(實(shí)現(xiàn)代碼)
java獲取當(dāng)前時(shí)間和前一天日期的實(shí)現(xiàn)代碼。需要的朋友可以過來參考下,希望對(duì)大家有所幫助2013-10-10
springboot打印接口調(diào)用日志的實(shí)例
這篇文章主要介紹了springboot打印接口調(diào)用日志的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-09-09
詳解如何讓Spring MVC顯示自定義的404 Not Found頁面
這篇文章主要介紹了詳解如何讓Spring MVC顯示自定義的404 Not Found頁面,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-10-10
Spring 框架實(shí)現(xiàn)賬戶轉(zhuǎn)賬功能(推薦)
通過本文的介紹,我們了解了如何使用Spring框架實(shí)現(xiàn)一個(gè)簡單的賬戶轉(zhuǎn)賬功能,主要使用了 Spring 的依賴注入、和事務(wù)管理功能,保證了轉(zhuǎn)賬操作的原子性和數(shù)據(jù)的一致性,感興趣的朋友跟隨小編一起看看吧2025-07-07
java開發(fā)RocketMQ消息中間件原理基礎(chǔ)詳解
最近 RocketMQ 剛剛上生產(chǎn)環(huán)境,閑暇之時(shí)在這里做一些分享,主要目的是讓初學(xué)者能快速上手RocketMQ,有需要的朋友可以借鑒參考下,希望能夠有所幫助2021-11-11

