亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

詳解Springboot配置文件的使用

 更新時(shí)間:2017年07月11日 16:47:16   作者:S_H-A_N  
在springboot項(xiàng)目中,也可以使用yml類型的配置文件代替properties文件。接下來(lái)通過(guò)本文給大家分享Springboot配置文件的使用,感興趣的朋友一起看看吧

如果使用IDEA創(chuàng)建Springboot項(xiàng)目,默認(rèn)會(huì)在resource目錄下創(chuàng)建application.properties文件,在springboot項(xiàng)目中,也可以使用yml類型的配置文件代替properties文件

一、單個(gè)的獲取配置文件中的內(nèi)容

在字段上使用@Value("${配置文件中的key}")的方式獲取單個(gè)的內(nèi)容

1.在resource目錄下創(chuàng)建application.yml文件,并添加一些配置,在yml文件中,key:后面需要添加一個(gè)空格,然后是value值,假設(shè)配置如下

#注意:在yml文件中添加value值時(shí),value前面需要加一個(gè)空格 
ip: 127.0.0.0 
port: 8080 

2.創(chuàng)建一個(gè)ConfigController類,獲取配置文件中的內(nèi)容并賦值給相應(yīng)的字段

package com.example; 
import org.springframework.beans.factory.annotation.Value; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RestController; 
@RestController 
public class ConfigController { 
 @Value("${ip}")//獲取application.yml文件中名為ip的value值 
 private String ip; 
 @Value("${port}")//獲取application.yml文件中名為port的value值,并且自動(dòng)完成數(shù)據(jù)類型轉(zhuǎn)換 
 private Integer port; 
 @RequestMapping("/config") 
 public String config() { 
  return "ip:"+ip+",port:"+port; 
 } 
} 

3.在SrpingbootdemoApplication中啟動(dòng)項(xiàng)目

package com.example; 
import org.springframework.boot.SpringApplication; 
import org.springframework.boot.autoconfigure.SpringBootApplication; 
//入口 
@SpringBootApplication 
public class SpringbootdemoApplication { 
 public static void main(String[] args) { 
  SpringApplication.run(SpringbootdemoApplication.class, args); 
 } 
} 

4.在瀏覽器中輸入http://localhost:8080/config,可以看到輸出了配置文件中配置的內(nèi)容

二、使用Bean自動(dòng)注入獲取配置文件中的內(nèi)容

假如配置文件中有很多內(nèi)容,一個(gè)一個(gè)獲取將會(huì)很麻煩,那么我們另外一種方式去獲取配置文件中的信息

1.在配置文件中添加以下信息(注意格式),此處我們使用了一個(gè)名為devconfig的前綴

devconfig: 
 ip: 127.0.0.0 
 port: 8080 

2.創(chuàng)建ConfigBean,在類中添加@Componet和@ConfigurationProperties注解,其中prefix設(shè)置為devconfig,將會(huì)獲取yml中前綴為devconfig下的配置信息

package com.example; 
 import org.springframework.boot.context.properties.ConfigurationProperties; 
import org.springframework.stereotype.Component; 
@Component 
@ConfigurationProperties(prefix = "devconfig")//獲取前綴為devconfig下的配置信息 
public class ConfigBean { 
 private String ip;//名字與配置文件中一致 
 private Integer port; 
 public String getIp() { 
  return ip; 
 } 
 public void setIp(String ip) { 
  this.ip = ip; 
 } 
 public Integer getPort() { 
  return port; 
 } 
 public void setPort(Integer port) { 
  this.port = port; 
 } 
} 

3.在ConfigController中使用@Autowrite對(duì)bean自動(dòng)注入,實(shí)例化bean

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.RestController; 
@RestController 
public class ConfigController { 
// @Value("${ip}")//獲取application.yml文件中名為ip的value值 
// private String ip; 
// 
// @Value("${port}")//獲取application.yml文件中名為port的value值,并且自動(dòng)完成數(shù)據(jù)類型轉(zhuǎn)換 
// private Integer port; 
 //自動(dòng)注入,實(shí)例化bean 
 @Autowired 
 private ConfigBean configBean; 
 @RequestMapping("/config") 
 public String config() { 
  return "另一種方式: ip:"+configBean.getIp()+",port:"+configBean.getPort(); 
 } 
} 

4.運(yùn)行程序,輸入http://localhost:8080/config進(jìn)行測(cè)試

三、多個(gè)配置文件切換使用

1.假設(shè)開(kāi)發(fā)環(huán)境使用ip為:127.0.0.0 使用端口為:8080

          生產(chǎn)環(huán)境使用ip為:127.0.0.1 使用端口為:8081

下面來(lái)修改配置文件,在resource目錄下創(chuàng)建一個(gè)名為application-dev.yml文件開(kāi)發(fā)環(huán)境使用配置文件和application-produce.yml生產(chǎn)環(huán)境配置文件

application-dev.yml

config: 
 ip: 127.0.0.0 
 port: 8080 

application-produce.yml

config: 
 ip: 127.0.0.1 
 port: 8081 

application.yml中配置生效的配置文件,此處設(shè)為produce,也就是使用application-produce.yml文件

spring: 
 profiles: 
 active: produce 

2.修改ConfigBean的prefix為config

package com.example; 
import org.springframework.boot.context.properties.ConfigurationProperties; 
import org.springframework.stereotype.Component; 
@Component 
@ConfigurationProperties(prefix = "config") 
public class ConfigBean { 
 private String ip;//名字與配置文件中一致 
 private Integer port; 
 public String getIp() { 
  return ip; 
 } 
 public void setIp(String ip) { 
  this.ip = ip; 
 } 
 public Integer getPort() { 
  return port; 
 } 
 public void setPort(Integer port) { 
  this.port = port; 
 } 
} 

3.運(yùn)行程序,在瀏覽器輸入http://localhost:8080/config進(jìn)行測(cè)試

4.也可通過(guò)啟動(dòng)jar包時(shí)添加參數(shù)來(lái)更改生效的配置文件,命令為

Java -jar XXX.jar --spring.profiles.active=poduce

以上所述是小編給大家介紹的詳解Springboot配置文件的使用,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!

相關(guān)文章

  • Java生成遞增流水號(hào)(編號(hào)+時(shí)間+流水號(hào))簡(jiǎn)單示例

    Java生成遞增流水號(hào)(編號(hào)+時(shí)間+流水號(hào))簡(jiǎn)單示例

    這篇文章主要給大家介紹了關(guān)于Java生成遞增流水號(hào)(編號(hào)+時(shí)間+流水號(hào))的相關(guān)資料,在開(kāi)發(fā)項(xiàng)目漫長(zhǎng)的過(guò)程中常常會(huì)遇到流水號(hào)需要自動(dòng)生成的問(wèn)題存在,文中給出了詳細(xì)的代碼示例,需要的朋友可以參考下
    2023-07-07
  • 實(shí)例講解spring boot 多線程

    實(shí)例講解spring boot 多線程

    這篇文章主要介紹了spring boot 多線程的相關(guān)資料,文中示例代碼非常詳細(xì),幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下
    2020-07-07
  • idea搭建ssh框架的超詳細(xì)教程

    idea搭建ssh框架的超詳細(xì)教程

    這篇文章主要介紹了idea搭建ssh框架的超詳細(xì)教程,本文通過(guò)圖文實(shí)例代碼相結(jié)合給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-03-03
  • JVM的垃圾回收算法工作原理詳解

    JVM的垃圾回收算法工作原理詳解

    這篇文章主要介紹了JVM的垃圾回收算如何判斷對(duì)象是否可以被回收,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,,需要的朋友可以參考下
    2019-06-06
  • Java中Steam流的用法詳解

    Java中Steam流的用法詳解

    Stream是Java?8?API添加的一個(gè)新的抽象,稱為流Stream,本文主要介紹了Java中Steam流的用法詳解,具有一定的參考價(jià)值,感興趣的可以了解一下
    2023-04-04
  • Spring?BeanDefinition父子關(guān)系示例解析

    Spring?BeanDefinition父子關(guān)系示例解析

    這篇文章主要為大家介紹了Spring?BeanDefinition父子關(guān)系示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-08-08
  • Mybatis-Plus中g(shù)etOne方法獲取最新一條數(shù)據(jù)的示例代碼

    Mybatis-Plus中g(shù)etOne方法獲取最新一條數(shù)據(jù)的示例代碼

    這篇文章主要介紹了Mybatis-Plus中g(shù)etOne方法獲取最新一條數(shù)據(jù),本文通過(guò)示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-05-05
  • Java8中的default方法詳解

    Java8中的default方法詳解

    這篇文章主要介紹了Java8中的default方法詳解,Java 8新增了default方法,它可以在接口添加新功能特性,而且還不影響接口的實(shí)現(xiàn)類,需要的朋友可以參考下
    2015-03-03
  • 如何解決Project SDK is not defined問(wèn)題

    如何解決Project SDK is not defined問(wèn)題

    這篇文章主要介紹了如何解決Project SDK is not defined問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-09-09
  • SpringBoot實(shí)現(xiàn)圖片識(shí)別文字的四種方式小結(jié)

    SpringBoot實(shí)現(xiàn)圖片識(shí)別文字的四種方式小結(jié)

    本文主要介紹了SpringBoot實(shí)現(xiàn)圖片識(shí)別文字的四種方式,包括Tess4J,百度智能云,阿里云,騰訊云這四種,具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-02-02

最新評(píng)論