springboot讀取配置檔案
一、配置檔案配置
直接配置
在src/main/resources下新增配置檔案application.properties
例如修改埠號
#埠號 server.port=8089
分環境配置
在src/main/resources下新增,application-pro.properties,application-dev.properties和application.properties三個檔案
application.properties
spring.profiles.active=dev
application-pro.properties
#埠號 server.port=80 #自定義埠號讀取 my.name=pzr.dev
application-dev.properties
#埠號 server.port=8089 #自定義埠號讀取 my.name=pzr.pro
當application.propertie設定spring.profiles.active=dev時,則說明是指定使用application-dev.properties檔案進行配置
二、配置檔案引數讀取
2.1、註解方式讀取
1、@PropertySource配置檔案路徑設定,在類上添加註解,如果在預設路徑下可以不新增該註解。
需要用@PropertySource的有:
- 例如非application.properties,classpath:config/my.properties指的是src/main/resources目錄下config目錄下的my.properties檔案,
- 例如有
多配置檔案引用,若取兩個配置檔案中有相同屬性名的值,則取值為最後一個配置檔案中的值
在application.properties中的檔案,直接使用@Value讀取即可,applicarion的讀取優先順序最高
@PropertySource({"classpath:config/my.properties","classpath:config/config.properties"}) public class TestController
2、@Value屬性名,在屬性名上新增該註解
@Value("${my.name}") private String myName;
示例1:使用@Value讀取application.properties裡的配置內容
配置檔案application.properties
spring.application.name=springbootdemo server.port=8080 mail.username=application-duan mail.password=application-duan123456
啟動類
package com.dxz.property5; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; @SpringBootApplication public class TestProperty5 { public static void main(String[] args) { //SpringApplication.run(TestProperty1.class, args); new SpringApplicationBuilder(TestProperty5.class).web(true).run(args); } }
測試類:
package com.dxz.property5; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.PropertySource; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/task") //@PropertySource("classpath:mail.properties") public class TaskController { @Value("${mail.username}") private String userName; @Value("${mail.password}") private String password; @RequestMapping(value = { "/", "" }) public String hellTask() { System.out.println("userName:" + userName); System.out.println("password:" + password); return "hello task !!"; } }
結果:
userName:application-duan password:application-duan123456
示例2:使用@[email protected]讀取其它配置檔案(多個)內容
讀取mail.properties配置
package com.dxz.property5; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.PropertySource; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/task") @PropertySource("classpath:mail.properties") public class TaskController { @Value("${mail.smtp.auth}") private String userName; @Value("${mail.from}") private String password; @RequestMapping(value = { "/", "" }) public String hellTask() { System.out.println("userName:" + userName); System.out.println("password:" + password); return "hello task !!"; } }
結果:
userName:false password:[email protected]
2.2、物件對映方式讀取
- 首先建立物件與配置檔案對映關係
- 方法中使用自動注入方式,將物件注入,呼叫get方法獲取屬性值
- 注意:新版本的@ConfigurationProperties沒有了location屬性,使用@PropertySource來指定配置檔案位置
- prefix=”obj”指的是配置檔案中的字首,如obj.name,在定義物件屬性名時為private String name;
- 讀取配置檔案中的集合時,使用List來接收資料,但List必須先例項化
測試類
package com.dxz.property6; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.PropertySource; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/task") @PropertySource({ "classpath:mail.properties", "classpath:db.properties" }) public class TaskController { // 在application.properties中的檔案,直接使用@Value讀取即可,applicarion的讀取優先順序最高 @Value("${mail.username}") private String myName; // 如果多個檔案有重複的名稱的屬性話,最後一個檔案中的屬性生效 @Value("${mail.port}") private String port; @Value("${db.username}") private String dbUserName; @Autowired ObjectProperties objectProperties; @RequestMapping("/test") @ResponseBody String test() { String result = "myName:" + myName + "\n port:" + port + "\n dbUserName:" + dbUserName + "\n objectProperties:" + objectProperties; System.out.println("result:=" + result); return result; } }
啟動類
package com.dxz.property6; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; @SpringBootApplication public class TestProperty6 { public static void main(String[] args) { //SpringApplication.run(TestProperty1.class, args); new SpringApplicationBuilder(TestProperty6.class).web(true).run(args); } }
ObjectProperties.java
package com.dxz.property6; import java.util.ArrayList; import java.util.List; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.PropertySource; import org.springframework.stereotype.Component; /** * 配置檔案對映物件 * @author DELL */ @Component @PropertySource("classpath:config/object.properties") @ConfigurationProperties(prefix = "obj") public class ObjectProperties { private String name; private String age; // 集合必須初始化,如果找不到就是空集合,會報錯 private List<String> className = new ArrayList<String>(); public List<String> getClassName() { return className; } public void setClassName(List<String> className) { this.className = className; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } @Override public String toString() { return "ObjectProperties [name=" + name + ", age=" + age + ", className=" + className + "]"; } }
object.properties
#自定義屬性讀取 obj.name=obj.name obj.age=obj.age obj.className[0]=obj.className[0] obj.className[1]=obj.className[1]
db.properties
db.username=admin db.password=xxxxx mail.port=xxxx
result:=myName:application-duan port:2555 dbUserName:admin objectProperties:ObjectProperties [name=obj.name, age=obj.age, className=[obj.className[0], obj.className[1]]]
相關推薦
Springboot讀取配置檔案、pom檔案及自定義配置檔案
前言 很多人都知道讀取配置檔案,這是初級做法,上升一點難度是使用java bean的方式讀取自定義配置檔案,但是大家很少有知道讀取pom檔案資訊,接下來我都會講到。 正文 筆者還是基於Spring Boot :: (v1.5.8.RE
springBoot 讀取配置檔案yml中的資訊
yml中自定義一些變數 var: analyze_url: test ocr_url: test microsoft_key: test 對映到類變數中 @Getter @Component public class varModel { @Value("${var.
springboot讀取配置檔案的配置項值
三種方式讀取: (一) 通過springboot帶有的Environment 類讀取 (二) 通過使用@Value讀取 (三) 通過新增一個配置類,使用@ConfigurationProperties讀取 使用步驟: ① 新增一個配置類,在類名上面使用@Conf
springboot讀取配置檔案中文亂碼
在配置檔案application.properties中新增如下: #設定spring-boot 編碼格式 spring.banner.charset=UTF-8 server.tomcat.uri-encoding=UTF-8 spring.http.encoding.charset=UTF
SpringBoot讀取配置檔案總結
前言 Spring-Boot的核心配置檔案是application.properties,會預設讀取該配置檔案,當然也可以通過註解自定義配置檔案的資訊。開發中,經常會有一些常量,變動較少,但是我們不能在java程式碼中寫死,這樣每次修改都得去java程式碼中修改
springboot 讀取配置檔案中的變數(通過註解方式)
springboot的application.properties檔案中可以定義一些可配置的常量。在程式中我們不需要再重新的讀取檔案,我們可以直接使用@Value註解讀取配置檔案中的值。首先看一下配置檔案application.properties中的內容是:spring.p
SpringBoot讀取配置檔案的兩種方式以及自定義配置檔案的讀取
1.讀取預設配置檔案中的資料 application.properties 直接使用@Value註解獲取資料 2.使用Environment獲取資料 防止亂碼統一編碼格式 注入Environment 使用getPro
springboot 讀取配置檔案內容的幾種方式
1 使用 Environment 進行讀取 env.getProperty("配置檔案中的值") 2 使用註解的方式 @PropertySource("classpath:applic
【SpringBoot】——SpringBoot 讀取配置檔案方式
// 方式一: 將配置檔案封裝為一個bean @Autowired private ConfigProps configProps; // 方式二: 通過 Spring 提供的類獲取配置檔案 @Autowired private Environment environmen
SpringBoot讀取配置檔案中文屬性值而在網頁顯示位亂碼的處理辦法(非原創)
我在 application.properties 中包含有中文的屬性值,在程式中讀取該屬性的值,顯示在網頁上是亂碼。根據網上的資料,我通過如下的設定解決了這個問題: 1. 在 application.properties 檔案中增加以下內容: 2
springboot讀取配置檔案
一、配置檔案配置 直接配置 在src/main/resources下新增配置檔案application.properties 例如修改埠號 #埠號 server.port=8089 分環境配置 在src/main/resources下新增,application-
SpringBoot讀取配置檔案注入到配置類
SpringBoot介紹 springboot可支援你快速實現resutful風格的微服務架構,對開發進行了很好的簡化 springboot快速入門 http://projects.spring.io/spring-boot/#quick-start 1
【springboot讀取配置檔案】@ConfigurationProperties、@PropertySource和@Value
### 概念: - @ConfigurationProperties : 是springboot的註解,用於把主配置檔案中配置屬性設定到對於的Bean屬性上 - @PropertySource :是spring的註解,用於載入指定的屬性配置檔案到Spring的Environment中,可以和 @V
SpringBoot | 第六章:springboot 專案啟動讀取按照順序讀取配置檔案
1.按照順序讀取配置檔案工具類 import java.io.BufferedWriter; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; im
springboot @Value 讀取配置檔案失敗
application.yml配置檔案: ##mq配置 spring: activemq: broker-url: tcp://localhost:61616 in-memory: true user: admin pas
Wagon部署springboot專案讀取配置檔案錯誤問題
wagon(瓦工)外掛是一個很不錯的輕量級,快速部署專案到伺服器的外掛,針對用中小專案,使用起來十分方便。今天跟大家分享一下自己在使用過程中遇到的一個坑,持續兩天時間都沒能夠解決,最終在多方求助下找到最終原因。 問題狀況 在伺服器上直接執行start.sh啟動指令碼
springboot 專案框架搭建(三):工具類中讀取配置檔案
一.原因 編寫一個服務類的工具類,想做成一個靈活的配置,各種唯一code想從配置檔案中讀取,便有了這個坑。 二.使用@value獲取值為null, 這是因為這個工具類沒有交給spring boot 來管理,導致每次都是new 一個新的,所以每次取出來的
SpringBoot學習——如何設定和讀取配置檔案中屬性
配置檔案配置 直接配置 在src/main/resources下新增配置檔案application.properties 例如修改埠號 #埠號 server.port=8089 分環境配置 在src/main/resources下新增,application-pro.
SpringBoot 之 自定義配置檔案及讀取配置檔案application.properties或yml
讀取核心配置檔案核心配置檔案是指在resources根目錄下的application.properties或application.yml配置檔案,讀取這兩個配置檔案的方法有兩種,都比較簡單。 核心配置檔案application.properties內容如下: server.port=9090 test.m
Springboot--從配置檔案properties讀取字串亂碼
當讀取properties的內容為:發現中文亂碼。原因是由於預設讀取的為ISO-8859-1格式,因此需要切換為UTF-8。 主要方式有如下兩種 方式一 在你的application.properties中增加如下配置,避免中文亂碼 spring.http.en