当前位置: 移动技术网 > IT编程>开发语言>Java > 详解Spring Boot加载properties和yml配置文件

详解Spring Boot加载properties和yml配置文件

2019年07月22日  | 移动技术网IT编程  | 我要评论

一、系统启动后注入配置

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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网