• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号
您的位置:首页 > 程序设计 >Java > 详解Spring Boot加载properties和yml配置文件

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

作者:赛亚人之神 字体:[增加 减小] 来源:互联网 时间:2017-05-28

赛亚人之神 通过本文主要向大家介绍了spring boot yml,spring yml,spring boot yml文件,yml和properties,yml等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

一、系统启动后注入配置

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 {}
</div>

需要在ApplicationContext中注册配置

AnnotationConfigEmbeddedWebApplicationContext context = (AnnotationConfigEmbeddedWebApplicationContext) app.run("参数1");
context.register(Config.class);
</div>

用以下方式取值

Environment env = context.getEnvironment();
System.out.println(env.getProperty("address"));
</div>

email.yml文件配置如下:

server: 
 address: 127.0.0.1
</div>

二、在命令行传入注入到程序中

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

</div>

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

</div>
分享到:QQ空间新浪微博腾讯微博微信百度贴吧QQ好友复制网址打印

您可能想查找下面的文章:

  • spring boot装载自定义yml文件
  • 详解Spring Boot加载properties和yml配置文件
  • Spring Boot实现文件上传示例代码
  • spring boot装载自定义yml文件
  • 详解Spring Boot加载properties和yml配置文件

相关文章

  • 2017-05-28Java基本数据类型与对应的包装类(动力节点java学院整理)
  • 2017-05-28Java正则验证电话,手机,邮箱,日期,金额的方法示例
  • 2017-05-28十大常见Java String问题_动力节点Java学院整理
  • 2017-05-28java String 可变性的分析
  • 2017-05-28Spring Boot 项目发布到 Tomcat 服务器的操作步骤
  • 2017-05-28详解Spring通过@Value注解注入属性的几种方式
  • 2017-05-28spring整合redis缓存并以注解(@Cacheable、@CachePut、@CacheEvict)形式使用
  • 2017-05-28List调用toString()方法后,去除两头的中括号实例
  • 2017-05-28java生成缩略图的方法示例
  • 2017-05-28详解JAVA类加载机制(推荐)

文章分类

  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号

最近更新的内容

    • java springmvc乱码解决归纳整理详解
    • Java自定义异常_动力节点Java学院整理
    • 浅析java中 Spring MVC 拦截器作用及其实现
    • Spring Hibernate实现分页功能
    • Java 选择排序、插入排序、希尔算法实例详解
    • Java IO流 文件的编码实例代码
    • 数据库基本操作语法归纳总结
    • Java中List Set和Map之间的区别_动力节点Java学院整理
    • java 中Collection存储器详解及简单实例
    • Java数据结构和算法之冒泡排序(动力节点Java学院整理)

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有