日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

@Value@PropertySource@ConfigurationProperties注解使用

發布時間:2024/4/15 编程问答 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 @Value@PropertySource@ConfigurationProperties注解使用 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

使用配置文件注入屬性

Spring Boot 默認的配置文件src/main/java/resources/application.properties或者src/main/java/resources/application.yml,在這里我們可以配置一些常量。
首先我們使用配置文件給一個類注入相關的屬性:

com.xiaohang.controller.pet.no = ${random.uuid} com.xiaohang.controller.pet.name = Tom

通過注解@Value(value=”${config.name}”)就可以綁定到你想要的屬性上面。

package com.xiaohang.demo.controller;import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;@RestController @RequestMapping("/pet") public class PetController {@Value(value = "${com.xiaohang.controller.pet.no}")private String no;@Value(value = "${com.xiaohang.controller.pet.name}")private String name;@RequestMapping("/d")public String getDeatils() {return "no: " + no + ", name: " + name;} }

一個個綁定數據還是很不方便,可以新建一個Bean,專門用來綁定注入的屬性使用注解@ConfigurationProperties(prefix = “prefix”),不過需要注意的是先要引入相關依賴

<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency>

通過使用spring-boot-configuration-processor jar,你可以從被@ConfigurationProperties注解的節點輕松的產生自己的配置元數據文件。

這里我新建一個PetBean用來注入屬性。

package com.xiaohang.demo.bean;import lombok.Data; import org.springframework.boot.context.properties.ConfigurationProperties;@ConfigurationProperties(prefix = "com.xiaohang.controller.pet") @Data public class PetBean {private String no;private String name; }

使用配置文件給一個類注入相關的屬性:

com.xiaohang.controller.pet.no = ${random.uuid} com.xiaohang.controller.pet.name = Tom

注意在啟動類上加上注解

@EnableConfigurationProperties({PetBean.class}),

Application.java

package com.xiaohang.demo;import com.xiaohang.demo.bean.PetBean; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.context.properties.EnableConfigurationProperties;@SpringBootApplication @EnableConfigurationProperties(value = PetBean.class) public class DemoApplication {public static void main(String[] args) {SpringApplication.run(DemoApplication.class, args);} }

新建一個controller,注入我們創建的PetBean

package com.xiaohang.demo.controller;import com.xiaohang.demo.bean.PetBean; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;@RestController @RequestMapping("/v2/pet") public class PetController1 {@Autowiredprivate PetBean pet;@RequestMapping("/d")public String getDetail() {return "no: " + pet.getNo() + ", name: " + pet.getName();} }

使用自定義的配置文件
我們在resouce目錄下面創建一個bean/pet.properties,加入

com.xiaohang.no = 123456 com.xiaohang.name = Tom

新建一個PetBean1.java:
@PropertySource 這個注解可以指定具體的屬性配置文件,優先級比較低。

package com.xiaohang.demo.bean;import lombok.Data; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource;@Configuration @ConfigurationProperties(prefix = "com.xiaohang") @PropertySource(value = "classpath:bean/pet.properties") @Data public class PetBean1 {private String no;private String name; }

在controller中加入PetBean1的注入

package com.xiaohang.demo.controller;import com.xiaohang.demo.bean.PetBean1; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;@RestController @RequestMapping("v1/pet") public class PetController2 {@Autowiredprivate PetBean1 pet1;@RequestMapping("d1")public String getDetails() {return "no: " + pet1.getNo() + ", name: " + pet1.getName();} }

隨機數

my.secret=${random.value} my.number=${random.int} my.bignumber=${random.long} my.number.less.than.ten=${random.int(10)} my.number.in.range=${random.int[1024,65536]}

屬性占位符

app.name=MyApp app.description=${app.name} is a Spring Boot application

可以在配置文件中引用前面配置過的屬性(優先級前面配置過的這里都能用)。
通過如${app.name:默認名稱}方法還可以設置默認值,當找不到引用的屬性時,會使用默認的屬性。

屬性名匹配規則

例如有如下配置對象:

@Component @ConfigurationProperties(prefix="person") public class ConnectionSettings {private String firstName; }

firstName可以使用的屬性名如下:

  • person.firstName,標準的駝峰式命名
  • person.first-name,虛線(-)分割方式,推薦在.properties和.yml配置文件中使用
  • PERSON_FIRST_NAME,大寫下劃線形式,建議在系統環境變量中使用

    配置文件的優先級

    Spring Boot 支持多種外部配置方式,這些方式優先級如下:

  • 命令行參數
  • 來自java:comp/env的JNDI屬性
  • Java系統屬性(System.getProperties())
  • 操作系統環境變量
  • RandomValuePropertySource配置的random.*屬性值
  • jar包外部的application-{profile}.properties或application.yml(帶spring.profile)配置文件
  • jar包內部的application-{profile}.properties或application.yml(帶spring.profile)配置文件
  • jar包外部的application.properties或application.yml(不帶spring.profile)配置文件
  • jar包內部的application.properties或application.yml(不帶spring.profile)配置文件
  • @Configuration注解類上的@PropertySource
  • 通過SpringApplication.setDefaultProperties指定的默認屬性

同樣,這個列表按照優先級排序,也就是說,src/main/resources/config下application.properties覆蓋src/main/resources下application.properties中相同的屬性,此外,如果你在相同優先級位置同時有application.properties和application.yml,那么application.properties里的屬性里面的屬性就會覆蓋application.yml。

Profile-多環境配置

當應用程序需要部署到不同運行環境時,一些配置細節通常會有所不同,最簡單的比如日志,生產日志會將日志級別設置為WARN或更高級別,并將日志寫入日志文件,而開發的時候需要日志級別為DEBUG,日志輸出到控制臺即可。
如果按照以前的做法,就是每次發布的時候替換掉配置文件,這樣太麻煩了,Spring Boot的Profile就給我們提供了解決方案,命令帶上參數就搞定。

這里我們來模擬一下,只是簡單的修改端口來測試
在Spring Boot中多環境配置文件名需要滿足application-{profile}.properties的格式,其中{profile}對應你的環境標識,比如:

application-dev.properties:開發環境
application-prod.properties:生產環境
然后在application.properties中加入

spring.profiles.active=dev

或application.yml中加入

spring:# 環境 dev|test|proprofiles:active: dev

或啟動命令

java -jar xxx.jar --spring.profiles.active=dev

參數用–xxx=xxx的形式傳遞。意思就是表示在application.properties文件中配置了屬性。
可以通過SpringApplication.setAddCommandLineProperties(false)禁用命令行配置。

轉載于:https://blog.51cto.com/12012821/2358082

與50位技術專家面對面20年技術見證,附贈技術全景圖

總結

以上是生活随笔為你收集整理的@Value@PropertySource@ConfigurationProperties注解使用的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。