zoukankan      html  css  js  c++  java
  • SpringBoot之加载自定义配置文件

    SpringBoot默认加载配置文件名为:application.properties和application.yml,如果需要使用自定义的配置文件,则通过@PropertySource注解指定。

    JavaBean:

    package org.springboot.model;
    
    import lombok.Data;
    import org.springframework.boot.context.properties.ConfigurationProperties;
    import org.springframework.context.annotation.PropertySource;
    import org.springframework.stereotype.Component;
    
    
    @Component
    @ConfigurationProperties(prefix = "pet")
    @Data
    // 自定义配置文件路径
    @PropertySource(value = {"classpath:config/pet.properties"})
    public class Pet {
        private String name;
        private String type;
    }

    pet.properties(./resources/config/pet.properties)

    pet.name=haha
    pet.type=dog

    测试代码:

    package org.springboot;
    
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springboot.model.Pet;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.test.context.junit4.SpringRunner;
    
    @RunWith(SpringRunner.class)
    @SpringBootTest
    public class DemoApplicationTests {
        @Autowired
        Pet pet;
    
        // 指定其他配置文件
        @Test
        public void testPet() {
            System.out.println(pet);
        }
    
    }

    执行结果:

    Pet(name=haha, type=dog)
  • 相关阅读:
    小白安装使用Redis
    Mysql的Sql语句优化
    maximo入门----用户使用提要
    时不时刷刷BOSS 看看技术需求
    2019.7.10整理
    docker使用入门
    docker之windows安装&centOS安装
    HashTable学习
    Hashmap学习
    红黑树学习
  • 原文地址:https://www.cnblogs.com/gongxr/p/10234877.html
Copyright © 2011-2022 走看看