全网整合营销服务商

电脑端+手机端+微信端=数据同步管理

免费咨询热线:400-708-3566

详解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  # boot  # 加载yml  # yml配置  # springboot  # yml  # SpringBoot配置文件properties和yml的实现  # SpringBoot获取yml和properties配置文件的内容  # Spring Boot 配置文件类型properties 格式与yml 格式  # SpringBoot配置文件方式  # 在线yml文件转properties  # 命令行  # 大家多多  # 系统启动  # ApplicationContext  # Config  # public  # register  # run  # app  # AnnotationConfigEmbeddedWebApplicationContext  # classpath  # true  # ignoreResourceNotFound  # email  # properties  # getEnvironment  # static  # Main  # void 


相关文章: 网站设计制作企业有哪些,抖音官网主页怎么设置?  建站主机空间推荐 高性价比配置与快速部署方案解析  网站制作新手教程,新手建设一个网站需要注意些什么?  青浦网站制作公司有哪些,苹果官网发货地是哪里?  已有域名建站全流程解析:网站搭建步骤与建站工具选择  建站之星北京办公室:智能建站系统与小程序生成方案解析  Android使用GridView实现日历的简单功能  如何在Golang中引入测试模块_Golang测试包导入与使用实践  南阳网站制作公司推荐,小学电子版试卷去哪里找资源好?  建站之星如何快速解决建站难题?  昆明高端网站制作公司,昆明公租房申请网上登录入口?  如何获取开源自助建站系统免费下载链接?  岳西云建站教程与模板下载_一站式快速建站系统操作指南  如何确认建站备案号应放置的具体位置?  美食网站链接制作教程视频,哪个教做美食的网站比较专业点?  已有域名如何快速搭建专属网站?  如何选择适合PHP云建站的开源框架?  如何在橙子建站中快速调整背景颜色?  如何在阿里云虚拟机上搭建网站?步骤解析与避坑指南  GML (Geography Markup Language)是什么,它如何用XML来表示地理空间信息?  如何选择高效可靠的多用户建站源码资源?  如何通过西部建站助手安装IIS服务器?  网站制作多少钱一个,建一个论坛网站大约需要多少钱?  如何在云虚拟主机上快速搭建个人网站?  免费制作海报的网站,哪位做平面的朋友告诉我用什么软件做海报比较好?ps还是cd还是ai这几个软件我都会些我是做网页的?  制作网站的公司有哪些,做一个公司网站要多少钱?  桂林网站制作公司有哪些,桂林马拉松怎么报名?  建站之星客服服务时间及联系方式如何?  上海网站制作开发公司,上海买房比较好的网站有哪些?  ,sp开头的版面叫什么?  兔展官网 在线制作,怎样制作微信请帖?  制作旅游网站html,怎样注册旅游网站?  宝塔Windows建站如何避免显示默认IIS页面?  定制建站方案优化指南:企业官网开发与建站费用解析  如何在Windows 2008云服务器安全搭建网站?  简单实现Android文件上传  网页设计与网站制作内容,怎样注册网站?  清单制作人网站有哪些,近日“兴风作浪的姑奶奶”引起很多人的关注这是什么事情?  ,在苏州找工作,上哪个网站比较好?  建站OpenVZ教程与优化策略:配置指南与性能提升  非常酷的网站设计制作软件,酷培ai教育官方网站?  代刷网站制作软件,别人代刷火车票靠谱吗?  C#怎么使用委托和事件 C# delegate与event编程方法  东莞专业网站制作公司有哪些,东莞招聘网站哪个好?  如何快速打造个性化非模板自助建站?  javascript中的try catch异常捕获机制用法分析  红河网站制作公司,红河事业单位身份证如何上传?  西安大型网站制作公司,西安招聘网站最好的是哪个?  如何在IIS中新建站点并配置端口与物理路径?  如何通过服务器快速搭建网站?完整步骤解析 

您的项目需求

*请认真填写需求信息,我们会在24小时内与您取得联系。