欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

一、系統(tǒng)啟動(dò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中注冊(cè)配置

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() {
     :
  }
}

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

相關(guān)文章

  • Java+opencv3.2.0實(shí)現(xiàn)輪廓檢測(cè)

    Java+opencv3.2.0實(shí)現(xiàn)輪廓檢測(cè)

    這篇文章主要為大家詳細(xì)介紹了Java+opencv3.2.0實(shí)現(xiàn)輪廓檢測(cè),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-07-07
  • Java中Base64加密解密舉例詳解

    Java中Base64加密解密舉例詳解

    Base64編碼是我們程序開(kāi)發(fā)中經(jīng)常使用到的編碼方法,它是一種基于用64個(gè)可打印字符來(lái)表示二進(jìn)制數(shù)據(jù)的表示方法,這篇文章主要給大家介紹了關(guān)于Java中Base64加密解密的相關(guān)資料,需要的朋友可以參考下
    2024-05-05
  • java web過(guò)濾器處理亂碼

    java web過(guò)濾器處理亂碼

    本文主要介紹了java web過(guò)濾器處理亂碼的方法解析。具有很好的參考價(jià)值。下面跟著小編一起來(lái)看下吧
    2017-04-04
  • 淺談靜態(tài)變量、成員變量、局部變量三者的區(qū)別

    淺談靜態(tài)變量、成員變量、局部變量三者的區(qū)別

    下面小編就為大家?guī)?lái)一篇淺談靜態(tài)變量、成員變量、局部變量三者的區(qū)別。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2016-09-09
  • jackson在springboot中的使用方式-自定義參數(shù)轉(zhuǎn)換器

    jackson在springboot中的使用方式-自定義參數(shù)轉(zhuǎn)換器

    這篇文章主要介紹了jackson在springboot中的使用方式-自定義參數(shù)轉(zhuǎn)換器,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-10-10
  • 使用java + OpenCV破解頂象面積驗(yàn)證碼的示例

    使用java + OpenCV破解頂象面積驗(yàn)證碼的示例

    這篇文章主要介紹了使用java + OpenCV破解頂象面積驗(yàn)證碼的示例,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-02-02
  • Java并發(fā)編程中的synchronized關(guān)鍵字詳細(xì)解讀

    Java并發(fā)編程中的synchronized關(guān)鍵字詳細(xì)解讀

    這篇文章主要介紹了Java并發(fā)編程中的synchronized關(guān)鍵字詳細(xì)解讀,在Java早期版本中,synchronized 屬于 重量級(jí)鎖,效率低下,這是因?yàn)楸O(jiān)視器鎖(monitor)是依賴(lài)于底層的操作系統(tǒng)的Mutex Lock來(lái)實(shí)現(xiàn)的,Java 的線程是映射到操作系統(tǒng)的原生線程之上的,需要的朋友可以參考下
    2023-12-12
  • java核心編程之文件過(guò)濾類(lèi)FileFilter和FilenameFilter

    java核心編程之文件過(guò)濾類(lèi)FileFilter和FilenameFilter

    這篇文章主要為大家詳細(xì)介紹了java文件過(guò)濾類(lèi)FileFilter和FilenameFilter,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-08-08
  • springboot ehcache 配置使用方法代碼詳解

    springboot ehcache 配置使用方法代碼詳解

    EhCache是一個(gè)比較成熟的Java緩存框架,Springboot對(duì)ehcache的使用非常支持,所以在Springboot中只需做些配置就可使用,且使用方式也簡(jiǎn)易,今天給大家分享springboot ehcache 配置使用教程,一起看看吧
    2021-06-06
  • 淺談java日志格式化

    淺談java日志格式化

    不管我們使用何種語(yǔ)言開(kāi)發(fā),一旦程序發(fā)生異常,日志是一個(gè)很重要的數(shù)據(jù)。但是并不是意味著打印的日志越多越好,我們需要的是有用的日志。下面小編來(lái)和大家一起學(xué)習(xí)以下知識(shí)
    2019-05-05

最新評(píng)論