在SpringBoot下讀取自定義properties配置文件的方法
SpringBoot工程默認(rèn)讀取application.properties配置文件。如果需要自定義properties文件,如何讀取呢?
一、在resource中新建.properties文件
在resource目錄下新建一個(gè)config文件夾,然后新建一個(gè).properties文件放在該文件夾下。如圖remote.properties所示

二、編寫配置文件
remote.uploadFilesUrl=/resource/files/ remote.uploadPicUrl=/resource/pic/
三、新建一個(gè)配置類RemoteProperties.java
@Configuration
@ConfigurationProperties(prefix = "remote", ignoreUnknownFields = false)
@PropertySource("classpath:config/remote.properties")
@Data
@Component
public class RemoteProperties {
private String uploadFilesUrl;
private String uploadPicUrl;
}
其中
@Configuration 表明這是一個(gè)配置類
@ConfigurationProperties(prefix = "remote", ignoreUnknownFields = false) 該注解用于綁定屬性。prefix用來選擇屬性的前綴,也就是在remote.properties文件中的“remote”,ignoreUnknownFields是用來告訴SpringBoot在有屬性不能匹配到聲明的域時(shí)拋出異常。
@PropertySource("classpath:config/remote.properties") 配置文件路徑
@Data 這個(gè)是一個(gè)lombok注解,用于生成getter&setter方法,詳情請查閱lombok相關(guān)資料
@Component 標(biāo)識為Bean
四、如何使用?
在想要使用配置文件的方法所在類上表上注解EnableConfigurationProperties(RemoteProperties.class)
并自動注入
@Autowired RemoteProperties remoteProperties;
在方法中使用 remoteProperties.getUploadFilesUrl()就可以拿到配置內(nèi)容了。
@EnableConfigurationProperties(RemoteProperties.class)
@RestController
public class TestService{
@Autowired
RemoteProperties remoteProperties;
public void test(){
String str = remoteProperties.getUploadFilesUrl();
System.out.println(str);
}
}
這里str就是配置文件中的”/resource/files/”了。
PS:下面看下 Spring-boot中讀取config配置文件的兩種方式
了解過spring-Boot這個(gè)技術(shù)的,應(yīng)該知道Spring-Boot的核心配置文件application.properties,當(dāng)然也可以通過注解自定義配置文件的信息。
Spring-Boot讀取配置文件的方式:
一.讀取核心配置文件信息application.properties的內(nèi)容
核心配置文件是指在resources根目錄下的application.properties或application.yml配置文件,讀取這兩個(gè)配置文件的方法有兩種,都比較簡單。
核心配置文件application.properties內(nèi)容如下:
test.msg=Hello World SpringBoot
方式一:使用@Value方式(常用)
package Solin.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class WebController {
@Value("${test.msg}")
private String msg;
@RequestMapping("/index1")
public String index1(){
return "方式一:"+msg;
}
}
注意:在@Value的${}中包含的是核心配置文件中的鍵名。在Controller類上加@RestController表示將此類中的所有視圖都以JSON方式顯示,類似于在視圖方法上加@ResponseBody。
訪問:http://localhost:8088/index1時(shí)得到:"方式一:Hello World SpringBoot"
方式二:使用Environment方式
package Solin.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class WebController {
@Autowired
private Environment env;
@RequestMapping("/index2")
public String index2(){
return "方式二:"+env.getProperty("test.msg");
}
}
注意:這種方式是依賴注入Evnironment來完成,在創(chuàng)建的成員變量private Environment env上加上@Autowired注解即可完成依賴注入,然后使用env.getProperty("鍵名")即可讀取出對應(yīng)的值。
訪問:http://localhost:8088/index2時(shí)得到:"方式二:Hello World SpringBoot"
二.讀取自定義配置文件信息,例如:author.properties
為了不破壞核心文件的原生態(tài),但又需要有自定義的配置信息存在,一般情況下會選擇自定義配置文件來放這些自定義信息,這里在resources目錄下創(chuàng)建配置文件author.properties
resources/author.properties內(nèi)容如下:
author.name=Solin author.age=22
創(chuàng)建管理配置的實(shí)體類:
package Solin.controller;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
//加上注釋@Component,可以直接在其他地方使用@Autowired來創(chuàng)建其實(shí)例對象
@Component
@ConfigurationProperties(prefix = "author",locations = "classpath:author.properties")
public class MyWebConfig{
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
注意:
在@ConfigurationProperties注釋中有兩個(gè)屬性:
locations:指定配置文件的所在位置
prefix:指定配置文件中鍵名稱的前綴(我這里配置文件中所有鍵名都是以author.開頭)
使用@Component是讓該類能夠在其他地方被依賴使用,即使用@Autowired注釋來創(chuàng)建實(shí)例。
創(chuàng)建測試Controller
package Solin.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class ConfigController {
@Autowired
private MyWebConfig conf;
@RequestMapping("/test")
public @ResponseBody String test() {
return "Name:"+conf.getName()+"---"+"Age:"+conf.getAge();
}
}
注意:由于在Conf類上加了注釋@Component,所以可以直接在這里使用@Autowired來創(chuàng)建其實(shí)例對象。
訪問:http://localhost:8088/test時(shí)得到:"Name:Solin---Age:22"
總結(jié)
以上所述是小編給大家介紹的在SpringBoot下讀取自定義properties配置文件的方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時(shí)回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
- Spring Boot 日志配置方法(超詳細(xì))
- SpringBoot + Spring Security 基本使用及個(gè)性化登錄配置詳解
- springboot如何讀取配置文件(application.yml)中的屬性值
- 詳解SpringBoot配置連接池
- SpringBoot獲取yml和properties配置文件的內(nèi)容
- spring boot Logging的配置以及使用詳解
- spring boot的maven配置依賴詳解
- spring boot開發(fā)遇到坑之spring-boot-starter-web配置文件使用教程
- Springboot配置doris連接的實(shí)現(xiàn)示例
相關(guān)文章
Java把多個(gè)對象的list的數(shù)據(jù)合并的方法示例
在Java中合并多個(gè)List可以使用遍歷、Stream API或Apache Commons Collections,通過遍歷各個(gè)List并將元素添加到新List實(shí)現(xiàn)合并,Java 8提供了Stream API,使用Stream.of()和flatMap()可以簡潔地合并List,文中給出了多種方法示例,需要的朋友可以參考下2024-09-09
MVC頁面之間參數(shù)傳遞實(shí)現(xiàn)過程圖解
這篇文章主要介紹了MVC頁面之間參數(shù)傳遞實(shí)現(xiàn)過程圖解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-11-11
解決Servlet4.0版本使用注解設(shè)置url但無法訪問的問題
在學(xué)習(xí)servlet過程中,使用web.xml文件配置servlet可以正常訪問,但使用WebServlet注解時(shí)出現(xiàn)404錯(cuò)誤,解決方法是在web.xml文件中將metadata-complete屬性改為false,啟動標(biāo)注支持,然而該方法對我無效,最后通過重建項(xiàng)目和手動將新建的項(xiàng)目添加到tomcat服務(wù)器解決問題2024-10-10
Spring Data Jpa如何實(shí)現(xiàn)批量插入或更新
文章總結(jié):本文分享了四種Spring Data JPA批量插入或更新的方法,包括BatchConsumer、QueryParameterBuilder、KeyValue和SqlUtil,旨在為開發(fā)者提供實(shí)用的參考2024-12-12
Java實(shí)現(xiàn)Kafka生產(chǎn)者消費(fèi)者代碼實(shí)例
這篇文章主要介紹了Java實(shí)現(xiàn)Kafka生產(chǎn)者消費(fèi)者代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-04-04

