zoukankan      html  css  js  c++  java
  • SpringBoot引入其他配置文件

    1. @PropertySource
      • 属性

    1.value为要加载的文件,可以是多个,当以classpath开头时,程序会自动到classpath中读取,当以file开头时,会加载外部的文件
    2.name是表示要加载文件的名称,这里要加载的配置文件必须是 唯一的不能是多个
    3.encoding,设置编码,我们一般用utf-8
    4.ignoreResourceNotFound,这个属性的意思是当加载的配置文件不存在时,是否报错默认false,当为true时表示文件不存在不报错,为false时表示文件不存在报错

      • 获取单个参数

          使用@Value()注解获取:@Value("${参数名}“”)

      • 批量获取参数 (@ConfigurationProperties)

    语法:@ConfiggurationProperties(prefix="参数前缀')

    1. @PropertySources
      • 属性

          value:PropertySource[] value(),是一个PropertySource的集合。

          语法:@PropertySources({ @PropertySource("classpath:配置文件名"),@PropertySource("classpath:配置文件名") })

      • 获取单个参数

          使用@Value()注解获取:@Value("${参数名}“”)

      • 批量获取参数(@ConfigurationProperties)

          语法:@ConfiggurationProperties(prefix="参数前缀')

      • 代码示范
    @Configuration
    @PropertySources({
            @PropertySource("classpath:MongoDB.properties"),
            @PropertySource("classpath:Redis.properties"),
            @PropertySource("classpath:Mysql.properties")
    })
    @ConfigurationProperties(prefix = "data")
    public class DemoConfig {
        @Value("mysql")
        private String mysql;
        @Value("redis")
        private String redis;
        @Value("mongodb")
        private String mongodb;
        @Bean
        public User user(){
            return new User(mysql,mysql,mongodb);
        }
    }
  • 相关阅读:
    vim操作
    git命令
    Python笔记(二)
    python笔记
    gdb笔记 ---《Linux.C编程一站式学习》
    python笔记——dict和set
    echo $?
    FastDFS与Nginx环境配置
    Nginx依赖库安装
    mixin多继承包装过程
  • 原文地址:https://www.cnblogs.com/dai-tao/p/13124063.html
Copyright © 2011-2022 走看看