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

作者:赛亚人之神 时间:2023-11-24 07:14:09 

一、系统启动后注入配置


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

来源:http://www.jianshu.com/p/eadfecea1f2d

标签:spring,boot,yml
0
投稿

猜你喜欢

  • C# 设计模式系列教程-状态模式

    2022-11-07 13:31:55
  • C#实现Dictionary字典赋值的方法

    2021-12-09 17:55:40
  • Android实现清除单个域名的cookie

    2021-10-09 01:57:52
  • 详解java_ 集合综合案例:斗地主

    2022-02-08 04:14:49
  • Java基本数据类型族谱与易错点梳理解析

    2021-08-18 10:20:27
  • Android实现EditText控件禁止输入内容的方法(附测试demo)

    2021-06-26 08:43:38
  • 通过实例学习Either 树和模式匹配

    2023-05-21 02:02:41
  • C#设置与获取环境变量的方法详解

    2021-09-03 20:55:29
  • Android编程实现手机自带内部存储路径的获取方法

    2022-05-14 11:59:11
  • 灵活使用Android中ActionBar和ViewPager切换页面

    2022-07-08 17:20:09
  • 面试初级Java开发问到Arrays

    2023-11-27 05:40:04
  • Android加载Assets目录中Xml布局文件

    2021-06-19 14:59:53
  • Android scrollToTop实现点击回到顶部(兼容PullTorefreshScrollview)

    2021-07-29 09:06:37
  • 分享java中设置代理的两种方式

    2023-10-28 10:48:52
  • 一文详解Reactor模型与实现示例

    2023-11-13 12:22:09
  • 使用SpringMVC的@Validated注解验证的实现

    2023-09-20 19:49:55
  • Android开发:微信授权登录与微信分享完全解析

    2023-03-20 14:08:10
  • Java中启动线程start和run的两种方法

    2023-09-12 10:58:29
  • 深入理解Java Socket通信

    2022-10-03 04:56:10
  • 解决springcloud中Feign导入依赖为unknow的情况

    2022-02-03 14:05:45
  • asp之家 软件编程 m.aspxhome.com