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);
        }
    }
  • 相关阅读:
    python实现的列表操作
    python的静态方法
    python标准库学习2
    javascript继承原型继承的例子
    jQuery高亮显示文本中重要的关键字
    表格展开伸缩
    jQuery设计思想
    python标准库学习3
    python中的继承和抽象类的实现
    表格的变色问题
  • 原文地址:https://www.cnblogs.com/dai-tao/p/13124063.html
Copyright © 2011-2022 走看看