• 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-28springboot + swagger 实例代码
  • 2017-05-28详解springMVC之与json数据交互方法
  • 2017-05-28Spring Boot 集成Mybatis实现主从(多数据源)分离方案示例
  • 2017-05-28Java微信公众平台开发(6) 微信开发中的token获取
  • 2017-05-28spring boot日志管理配置
  • 2017-05-28SpringMVC结合天气api实现天气查询
  • 2017-05-28详解Spring Boot中使用AOP统一处理Web请求日志
  • 2017-05-28Java Calendar类的详解及使用实例
  • 2017-05-28Java装饰器设计模式_动力节点Java学院整理
  • 2017-05-28java客户端Jedis操作Redis Sentinel 连接池的实现方法

文章分类

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

最近更新的内容

    • Java 序列化和反序列化实例详解
    • Java中四种引用类型详细介绍
    • 30分钟入门Java8之方法引用学习
    • Spring2.5.6开发环境搭建图文教程
    • 全面掌握Java内部类
    • java判断ftp目录是否存在的方法
    • java多线程编程技术详解和实例代码
    • jstl标签基础开发步骤(详解)
    • Java中内存异常StackOverflowError与OutOfMemoryError详解
    • Java语言实现简单FTP软件 FTP上传下载管理模块实现(11)

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

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