最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

詳解Spring Boot加載properties和yml配置文件

 更新時間:2017年04月13日 09:52:16   作者:賽亞人之神  
本篇文章主要介紹了詳解Spring Boot加載properties和yml配置文件,具有一定的參考價值,感興趣的小伙伴們可以參考一下

一、系統(tǒng)啟動后注入配置

package com.example.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;

/**
 * @author: GrandKai
 * @create: 2016-09-01 11:24
 */
@Configuration
@PropertySource(ignoreResourceNotFound = true, value = {"classpath:/config/email.properties","classpath:/config/email.yml"}, name = "email")
public class Config {}

需要在ApplicationContext中注冊配置

AnnotationConfigEmbeddedWebApplicationContext context = (AnnotationConfigEmbeddedWebApplicationContext) app.run("參數(shù)1");
context.register(Config.class);

用以下方式取值

Environment env = context.getEnvironment();
System.out.println(env.getProperty("address"));

email.yml文件配置如下:

server: 
 address: 127.0.0.1

二、在命令行傳入注入到程序中

public class Main {
  public static void main(String... args) {
    //initialize the command line parsing stuff
    OptionParser parser = new OptionParser();
    parser.accepts("greeting").withRequiredArg();
    OptionSet options = parser.parse(args);

    //create the actual Spring PropertySource
    PropertySource<?> ps = new JOptCommandLinePropertySource(options);

    //setup the Spring context
    AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
    ctx.getEnvironment().getPropertySources().addLast(ps); 
    //register the property source with the environment

    ctx.register(Greeter.class);
    ctx.refresh();
    Greeter greeter = ctx.getBean(Greeter.class);
    greeter.sayGreeting();
  }
}

@Component
class Greeter {
  @Inject private Environment env;


  //the following would also work
  //@Value("${greeting}")
  //private String greeting;    

  /**
   * Print out the 'greeting' property if it exists, and otherwise, "Welcome!".
   */
  public void sayGreeting() {
    System.out.println(env.getProperty("greeting", "Welcome!"));
  }
}




public static void main(String [] args) {
  SimpleCommandLinePropertySource ps = new SimpleCommandLinePropertySource(args);
  @SuppressWarnings("resource")
  AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
  ctx.getEnvironment().getPropertySources().addFirst(ps);
  ctx.register(ApplicationConfig.class);
  ctx.refresh();
}


@Configuration
@EnableScheduling
@ComponentScan("com.mycompany.package")
@PropertySource(
    value = {"classpath:/application.properties", "file:${config.location}"},
    ignoreResourceNotFound = true
  )
class ApplicationConfig {

  @Bean
  public static PropertySourcesPlaceholderConfigurer propertyConfigurer() {
    return new PropertySourcesPlaceholderConfigurer();
  }
}

@Component
class MyComponent {

  @Value("${my.property.data}")
  private String myPropertyData;


  @Scheduled(fixedDelayString = "${schedule.delay.period}")
  public void run() {
     :
  }
}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • Spring源碼之請求路徑匹配路由方式

    Spring源碼之請求路徑匹配路由方式

    這篇文章主要介紹了Spring源碼之請求路徑匹配路由方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • 帶你粗略了解Java數(shù)組的使用

    帶你粗略了解Java數(shù)組的使用

    這篇文章主要給大家介紹了關于Java中數(shù)組的定義和使用的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2021-08-08
  • IDEA的Project無法正常顯示的問題解決

    IDEA的Project無法正常顯示的問題解決

    本文主要介紹了IDEA的Project無法正常顯示的問題解決,文中通過圖文介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2024-02-02
  • SpringBoot項目打包運行jar包的實現(xiàn)示例

    SpringBoot項目打包運行jar包的實現(xiàn)示例

    本文主要介紹了SpringBoot項目打包運行jar包的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2025-02-02
  • MyBatis一二級緩存

    MyBatis一二級緩存

    這篇文章主要介紹了MyBatis一二級緩存的相關知識,非常不錯,具有參考借鑒價值,需要的朋友可以參考下
    2017-06-06
  • 最新Java?泛型中的通配符講解

    最新Java?泛型中的通配符講解

    Java的泛型是偽泛型,那是因為泛型信息只存在于代碼編譯階段,在生成的字節(jié)碼中是不包含泛型中的類型信息的,使用泛型的時候加上類型參數(shù),在編譯器編譯的時候會去掉,這個過程為類型擦除,這篇文章主要介紹了Java?泛型中的通配符,需要的朋友可以參考下
    2022-06-06
  • Eclipse轉Itellij IDEA導入Git/svn本地項目的詳細步驟

    Eclipse轉Itellij IDEA導入Git/svn本地項目的詳細步驟

    這篇文章主要介紹了Eclipse轉Itellij IDEA導入Git/svn本地項目,本文通過圖文并茂的形式給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-10-10
  • Java之Class.forName()用法案例詳解

    Java之Class.forName()用法案例詳解

    這篇文章主要介紹了Java之Class.forName()用法案例詳解,本篇文章通過簡要的案例,講解了該項技術的了解與使用,以下就是詳細內容,需要的朋友可以參考下
    2021-09-09
  • java?Web實現(xiàn)用戶登錄功能圖文教程

    java?Web實現(xiàn)用戶登錄功能圖文教程

    這篇文章主要給大家介紹了關于java?Web實現(xiàn)用戶登錄功能的相關資料,在開發(fā)Web應用程序中,用戶登錄是一個常見的功能,文中通過代碼以及圖文介紹的非常詳細,需要的朋友可以參考下
    2023-10-10
  • java啟動參數(shù)之謎的排查過程

    java啟動參數(shù)之謎的排查過程

    在日常操作中,相信很多人對Java啟動參數(shù)存在疑惑,下面這篇文章主要給大家介紹了關于java啟動參數(shù)之謎的排查過程,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下
    2022-06-06

最新評論

桐柏县| 肥西县| 清镇市| 山东省| 高阳县| 东港市| 安岳县| 建水县| 安宁市| 卓尼县| 和田县| 稷山县| 克什克腾旗| 岐山县| 皋兰县| 西充县| 黄梅县| 烟台市| 萝北县| 新巴尔虎左旗| 九寨沟县| 西和县| 义马市| 灵璧县| 锡林郭勒盟| 宁武县| 昭通市| 上犹县| 密云县| 奉节县| 罗源县| 凉山| 本溪| 从化市| 广东省| 门源| 忻城县| 象州县| 同江市| 玉溪市| 和龙市|