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

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

相关文章

  • Spring源码之请求路径匹配路由方式

    Spring源码之请求路径匹配路由方式

    这篇文章主要介绍了Spring源码之请求路径匹配路由方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-09-09
  • 带你粗略了解Java数组的使用

    带你粗略了解Java数组的使用

    这篇文章主要给大家介绍了关于Java中数组的定义和使用的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-08-08
  • IDEA的Project无法正常显示的问题解决

    IDEA的Project无法正常显示的问题解决

    本文主要介绍了IDEA的Project无法正常显示的问题解决,文中通过图文介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2024-02-02
  • SpringBoot项目打包运行jar包的实现示例

    SpringBoot项目打包运行jar包的实现示例

    本文主要介绍了SpringBoot项目打包运行jar包的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2025-02-02
  • MyBatis一二级缓存

    MyBatis一二级缓存

    这篇文章主要介绍了MyBatis一二级缓存的相关知识,非常不错,具有参考借鉴价值,需要的朋友可以参考下
    2017-06-06
  • 最新Java 泛型中的通配符讲解

    最新Java 泛型中的通配符讲解

    Java的泛型是伪泛型,那是因为泛型信息只存在于代码编译阶段,在生成的字节码中是不包含泛型中的类型信息的,使用泛型的时候加上类型参数,在编译器编译的时候会去掉,这个过程为类型擦除,这篇文章主要介绍了Java 泛型中的通配符,需要的朋友可以参考下
    2022-06-06
  • Eclipse转Itellij IDEA导入Git/svn本地项目的详细步骤

    Eclipse转Itellij IDEA导入Git/svn本地项目的详细步骤

    这篇文章主要介绍了Eclipse转Itellij IDEA导入Git/svn本地项目,本文通过图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-10-10
  • Java之Class.forName()用法案例详解

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

    这篇文章主要介绍了Java之Class.forName()用法案例详解,本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下
    2021-09-09
  • java Web实现用户登录功能图文教程

    java Web实现用户登录功能图文教程

    这篇文章主要给大家介绍了关于java Web实现用户登录功能的相关资料,在开发Web应用程序中,用户登录是一个常见的功能,文中通过代码以及图文介绍的非常详细,需要的朋友可以参考下
    2023-10-10
  • java启动参数之谜的排查过程

    java启动参数之谜的排查过程

    在日常操作中,相信很多人对Java启动参数存在疑惑,下面这篇文章主要给大家介绍了关于java启动参数之谜的排查过程,文中通过实例代码介绍的非常详细,需要的朋友可以参考下
    2022-06-06

最新评论