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

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

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

一、系統(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() {
     :
  }
}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

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

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

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

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

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

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

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

    SpringBoot項(xiàng)目打包運(yùn)行jar包的實(shí)現(xiàn)示例

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

    MyBatis一二級緩存

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

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

    Java的泛型是偽泛型,那是因?yàn)榉盒托畔⒅淮嬖谟诖a編譯階段,在生成的字節(jié)碼中是不包含泛型中的類型信息的,使用泛型的時(shí)候加上類型參數(shù),在編譯器編譯的時(shí)候會去掉,這個過程為類型擦除,這篇文章主要介紹了Java?泛型中的通配符,需要的朋友可以參考下
    2022-06-06
  • Eclipse轉(zhuǎn)Itellij IDEA導(dǎo)入Git/svn本地項(xiàng)目的詳細(xì)步驟

    Eclipse轉(zhuǎn)Itellij IDEA導(dǎo)入Git/svn本地項(xiàng)目的詳細(xì)步驟

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

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

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

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

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

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

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

最新評論

肇源县| 陈巴尔虎旗| 宜州市| 崇左市| 汽车| 安福县| 土默特右旗| 桑植县| 十堰市| 梅州市| 安远县| 哈巴河县| 溧水县| 元江| 隆尧县| 贵州省| 南平市| 红原县| 临漳县| 九江县| 普陀区| 百色市| SHOW| 确山县| 彰武县| 互助| 榕江县| 尉犁县| 上蔡县| 屯昌县| 昭通市| 新昌县| 新密市| 台湾省| 镇赉县| 泗阳县| 缙云县| 明水县| 九江市| 利辛县| 明星|