欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-03-02 09:01:52
一、系统启动后注入配置 package com.example.config; import org.springframework.beans.facto...

一、系统启动后注入配置

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

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