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)

原文地址:https://www.cnblogs.com/gongxr/p/10234877.html

时间: 2024-08-29 01:18:31

SpringBoot之加载自定义配置文件的相关文章

ThinkPHP加载自定义的外部文件和配置文件

我们知道ThinkPHP有公共的函数文件和配置文件,位于Common目录下,默认Common/function.php为公共的函数文 件,Conf/config.php为公共配置文件.好了,那么如何自定义其他的公共函数和配置文件呢.这里不得不讲到两个配置参数 LOAD_EXT_FILE和LOAD_EXT_CONFIG了. 1. LOAD_EXT_FILE配置的是自定义的函数文件,比如我想在Common目录下再创建一个common.php文件,那么在config.php里 则可以配置LOAD_EX

[Yii2.0] 以Yii 2.0风格加载自定义类或命名空间 [配置使用Yii2 autoloader]

Yii 2.0最显著的特征之一就是引入了命名空间,因此对于自定义类的引入方式也同之前有所不同.这篇文章讨论一下如何利用Yii 2.0的自动加载机制,向系统中引入自定义类和命名空间.本文旨在抛砖引玉,如果有理解不当敬请指正,欢迎大家把自己的方法拿出来分享.我们希望被引入的类应该达成一下两点: 在应用中的任这里输入代码意位置可以使用该类名或命名空间,而不用显式调用require()/include(). 利用Yii的autoloader,仅在类被调用时加载,以遵循Yii按需加载的原则,节省资源. 我

selenium启动Chrome时,加载用户配置文件

selenium启动Chrome时,加载用户配置文件 Selenium操作浏览器是不加载任何配置的,网上找了半天,关于Firefox加载配置的多点,Chrome资料很少,下面是关于加载Chrome配置的方法: 一.加载所有Chrome配置 用Chrome地址栏输入chrome://version/,查看自己的“个人资料路径”,然后在浏览器启动时,调用这个配置文件,代码如下: #coding=utf-8 from selenium import webdriver option = webdriv

加载hibernate配置文件的2种方式

4.加载hibernate配置文件的2种方式 1.configuration.configure() 要求1.该配置文件必须放在classpath下2.名称必须为hibernate.cfg.xml 2.configuration.configure("/cn/itcast/config/hibernate.cfg.xml") 自定义配置文件的名称和路径

基于.net 的加载自定义配置-误操作

有时候 需要 将程序加载自定义的配置文件,除了自己写解析xml文件.内置的ConfigutionManager对象 是个不错的选项. 按照 app.config 的方式,做一个副本.然后从你的配置文件中,加载指定的配置 键值对! //方法1-可以读取任意位置的xml文件 ExeConfigurationFileMap filemap = new ExeConfigurationFileMap(); filemap.ExeConfigFilename = QuartzConfigPath; //f

在web.xml正确加载spring配置文件的方式

ssm框架整合时一直报出没有创建实例bean的错误,一直以为是代码原因,反复测试了很久,才找到原因是spring配置文件没有正确导入,下图是我的错误示例 web.xml加载spring配置文件的方式主要依据该配置文件的名称和存放的位置不同来区别,目前主要有两种方式. 1.如果spring配置文件的名称为applicationContext.xml,并且存放在WEB-INF/目录下,那么只需要在web.xml中加入以下代码即可 <listener> <listener-class>o

属性赋值[email&#160;protected]加载外部配置文件

配置类上添加注解@PropertySource加载外部配置文件 @Configuration @PropertySource("classpath:/person.properties") @ComponentScan(value = "com.yyc", includeFilters ={@ComponentScan.Filter(type= FilterType.CUSTOM, classes={MyTypeFilter.class})}, useDefault

Spring中加载xml配置文件的六种方式

因为目前正在从事一个项目,项目中一个需求就是所有的功能都是插件的形式装入系统,这就需要利用Spring去动态加载某一位置下的配置文件,所以就总结了下Spring中加载xml配置文件的方式,我总结的有6种, xml是最常见的spring 应用系统配置源.Spring中的几种容器都支持使用xml装配bean,包括: XmlBeanFactory,ClassPathXmlApplicationContext,FileSystemXmlApplicationContext,XmlWebApplicati

加载Properties配置文件

/** * 加载Properties配置文件 * * @author ZhangHaiNing * @param file 要读取的文件 * @return */ public static Properties getProp(String file){ Properties prop=new Properties(); try { String url = TelChargeDwr.class.getResource("").getPath().replaceAll("%