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

 更新时间:2017年04月13日 09:52:16   作者:赛亚人之神  
本篇文章主要介绍了详解Spring Boot加载properties和yml配置文件,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

一、系统启动后注入配置

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

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

相关文章

  • Java流程控制语句最全汇总(上篇)

    Java流程控制语句最全汇总(上篇)

    这篇文章主要介绍了Java流程控制语句最全汇总(上篇),本文章内容详细,通过案例可以更好的理解数组的相关知识,本模块分为了三部分,本次为上篇,需要的朋友可以参考下
    2023-01-01
  • Java中二叉树的建立和各种遍历实例代码

    Java中二叉树的建立和各种遍历实例代码

    这篇文章主要介绍了Java中二叉树的建立和各种遍历实例代码,涉及树节点的定义,后序遍历,层序遍历,深度优先和广度优先等相关内容,具有一定借鉴价值,需要的朋友可以参考下
    2018-01-01
  • Java中如何获取当前服务器的IP地址

    Java中如何获取当前服务器的IP地址

    这篇文章主要给大家介绍了关于Java中如何获取当前服务器的IP地址的相关资料,我们可以使用Java中的InetAddress类来获取Linux服务器的IP地址,需要的朋友可以参考下
    2023-07-07
  • Lombok中关于@Data的使用解析

    Lombok中关于@Data的使用解析

    这篇文章主要介绍了Lombok中关于@Data的使用解析,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-12-12
  • Java批量转换文件编码格式的实现方法及实例代码

    Java批量转换文件编码格式的实现方法及实例代码

    这篇文章主要介绍了Java实现 批量转换文件编码格式的方法及实例代码,本文通过实例代码给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
    2019-04-04
  • java Scanner类的使用示例代码

    java Scanner类的使用示例代码

    这篇文章主要介绍了java Scanner类的使用,Scanner类还可以任意地对字符串和基本类型(如int和double)的数据进行分析。借助于Scanner,可以针对任何要处理的文本内容编写自定义的语法分析器,感兴趣的朋友跟随小编一起看看吧
    2021-07-07
  • Java实现计算器设计

    Java实现计算器设计

    这篇文章主要为大家详细介绍了Java实现计算器设计,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-07-07
  • 详解JUnit5参数化测试的几种方式

    详解JUnit5参数化测试的几种方式

    参数化测试一直是津津乐道的话题,我们都知道JMeter有四种参数化方式:用户自定义变量、用户参数、CSV文件、函数助手,那么JUnit5有哪些参数化测试的方式呢
    2021-07-07
  • java 示例讲解循环语句的使用

    java 示例讲解循环语句的使用

    顺序结构的程序语句只能被执行一次。如果您想要同样的操作执行多次,就需要使用循环结构,循环结构就是在循环条件满足的情况下,反复执行特定代码
    2022-04-04
  • SpringBoot 整合 dubbo xml实现代码示例

    SpringBoot 整合 dubbo xml实现代码示例

    这篇文章主要介绍了SpringBoot 整合 dubbo xml实现代码示例,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-03-03

最新评论