SpringBoot中讀取application.properties配置文件的方法
application.properties有以下這幾條數(shù)據(jù)

方法一:@Value注解+@Component
建議properties少的時候用,多的時候就不要使用這種方法了
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UserController {
@Value("${wx.open.app_id}")
private String appid;
@Value("${wx.open.app_secret}")
private String secret;
@Value("${wx.open.redirect_url}")
private String url;
@RequestMapping("hello")
public String test(){
return appid+"---"+secret+"---"+url;
}
}
另一種方法
創(chuàng)建一個WeProperties
@Component
@Data
public class WeProperties {
@Value("${wx.open.app_id}")
private String appid;
@Value("${wx.open.app_secret}")
private String secret;
@Value("${wx.open.redirect_url}")
private String url;
}
Controller層
@RestController
public class UserController {
@Autowired
private WeProperties properties;
@RequestMapping("hello")
public String test(){
return properties.getAppid()+"---"+properties.getSecret()+"---"+properties.getUrl();
}
}

方法二:@Component+@ConfigurationProperties
創(chuàng)建一個WeProperties
后面的屬性名一定要保持一致
@Component
@ConfigurationProperties(prefix = "wx.open")
@Data
public class WeProperties {
private String appid;
private String app_secret;
private String redirect_url;
}
Controller層
@RestController
public class UserController {
@Autowired
private WeProperties properties;
@RequestMapping("hello")
public String test(){
return properties.getAppid()+"---"+properties.getApp_secret()+"---"+properties.getRedirect_url();
}
}

方法三:@ConfigurationProperties+@EnableConfigurationProperties
創(chuàng)建一個WeProperties
后面的屬性名一定要保持一致
@ConfigurationProperties(prefix = "wx.open")
@Data
public class WeProperties {
private String appid;
private String app_secret;
private String redirect_url;
}
啟動類添加@EnableConfigurationProperties
@SpringBootApplication
@EnableConfigurationProperties(value = WeProperties.class)
public class PropertiesApplication {
public static void main(String[] args) {
SpringApplication.run(PropertiesApplication.class,args);
}
}
Controller層
@RestController
public class UserController {
@Autowired
private WeProperties properties;
@RequestMapping("hello")
public String test(){
return properties.getAppid()+"---"+properties.getApp_secret()+"---"+properties.getRedirect_url();
}
}
到此這篇關(guān)于SpringBoot中讀取application.properties配置文件的方法的文章就介紹到這了,更多相關(guān)SpringBoot讀取application.properties內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java 將 list 字符串用逗號隔開拼接字符串的多種方法
這篇文章主要介紹了java 將 list 字符串用逗號隔開拼接字符串,本文給大家分享四種方法,每種方法通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-12-12
Java?OpenCV學(xué)習(xí)之Mat的基本操作詳解
OpenCV用來存儲圖像,很多時候都會用到這個Mat方法。數(shù)字圖像可看做一個數(shù)值矩陣,?其中的每一個元素表明一個像素點。Mat在?OpenCV?中表示的是?N?維稠密矩陣,與稠密矩陣相對的是稀疏矩陣。本文將重點介紹OpenCV中Mat的一些基本操作,需要的可以參考一下2022-03-03
springboot?serviceImpl初始化注入對象實現(xiàn)方式
這篇文章主要介紹了springboot?serviceImpl初始化注入對象實現(xiàn)方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-05-05
JDK9的新特性之String壓縮和字符編碼的實現(xiàn)方法
這篇文章主要介紹了JDK9的新特性之String壓縮和字符編碼的實現(xiàn)方法,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-05-05
解決問題:Failed to execute goal org.apache.m
這篇文章主要給大家介紹了關(guān)于解決問題:Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources的相關(guān)資料,文中將解決的辦法介紹的非常詳細,需要的朋友可以參考下2023-03-03

