SpringBoot加载读取配置文件过程详细分析
作者:FlyLikeButterfly 发布时间:2021-12-29 03:07:36
springboot默认读取的配置文件名字是:“application.properties”和“application.yml”,默认读取四个位置的文件:根目录下、根目录的config目录下、classpath目录下、classpath目录里的config目录下;
配置文件的读取顺序
根目录/config/application.properties
根目录/config/application.yml
根目录/application.properties
根目录/application.yml
classpath目录/config/application.properties
classpath目录/config/application.yml
classpath目录/application.properties
classpath目录/application.yml
默认可读取的配置文件全部都会被读取合并,按照顺序读取配置,相同的配置项按第一次读取的值为准,同一个目录下properties文件比yml优先读取,通常会把配置文件放到classpath下,一般是resources里;
多坏境的配置文件
通常可以使用4个配置文件:(yml也同理)
application.properties:默认配置文件
application-dev.properties:开发环境配置文件
application-prod.properties:生产环境配置文件
application-test.properties:测试环境配置文件
在application.properties里配置spring.profiles.active以指定使用哪个配置文件,可以配置dev、prod、test分别对应以-dev、-prod、-test结尾的配置文件;(yml配置文件也是同理)
也可以在命令行使用spring.profiles.active指定,例如:java -jarxxxxxx.jar--spring.profiles.active=dev;
个性化配置
对于更特殊的个性化配置可以使用@Profile注解指定;
@Profile标签可以用在@Component或者@Configuration修饰的类上,可以标记类和方法,用来指定配置名字,然后使用spring.profiles.active指定该配置名字就可生效;
就像这样:
package testspringboot.test2;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
@Configuration
@Profile("myconfig")
public class MyConfig {
@Bean("Tom")
@Profile("A")
public String a() {
return "tomtom";
}
@Bean("Tom")
@Profile("B")
public String b() {
return "TOMTOM";
}
@Bean("Tom")
public String c() {
return "ttoomm";
}
}
然后写一个controller类:
package testspringboot.test2;
import javax.annotation.Resource;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/test2controller")
public class Test2Controller {
@Resource(name = "Tom")
public String t;
@RequestMapping("/test2")
public String test2() {
System.out.println(t);
return "TEST2" + t;
}
}
启动类:
package testspringboot.test2;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Test2Main {
/**
* @param args
*/
public static void main(String[] args) {
SpringApplication.run(Test2Main.class, args);
}
}
配置文件里配置:
server.port=8888
server.servlet.context-path=/testspringboot
spring.profiles.active=myconfig
只指定myconfig配置,则MyConfig类里c()的bean生效,访问结果是:
修改spring.profiles.active=myconfig,A,则MyConfig类里标记@Profile("A")的bean生效:
修改spring.profiles.active=myconfig,B,则标记@Profile("B")的bean生效:
如果去掉spring.profiles.active配置,则就找不到MyConfig里的配置了,启动失败:
自定义配置文件名称和路径
可以使用@PropertySource标签指定自定义的配置文件名称和路径;(默认能加载到的配置文件也会先被加载)
通常只会用到设置配置文件的名字,并且配置文件的名字可以随便定义,可以叫xxxx.properties、a.txt、b.abc等等,但是内容格式需要跟.properties一致,即kv格式,所以不能直接加载yml格式的配置文件;
@PropertySource默认加载路径是classpath下,可以使用classpath:xxxx/xxxx/xxxx.properties指定目录和文件,如果使用根目录则需要使用file:xxxx/xxxx/xxxx.properties;
可以使用@PropertySource为启动类指定springboot的配置文件,能够做到使用一个main方法启动两个springboot实例,并各自使用不同的配置文件:
@SpringBootApplication
@PropertySource("classpath:a.properties")
@PropertySource(value = "file:a.properties", ignoreResourceNotFound = true)
public class Test2Main {
/**
* @param args
*/
public static void main(String[] args) {
SpringApplication.run(Test2Main.class, args);
}
}
也可以使用@PropertySource配置bean,在使用@Component和@ConfigurationProperties时也可给bean指定特定配置文件:
放在resources下的配置文件tom.abc:
mybean.name=Tom
mybean.age=12
bean类ABC,配置tom.abc文件注入:
package testspringboot.test2;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties("mybean")
@PropertySource(value = "classpath:tom.abc")
public class ABC {
public String name;
public int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "ABC [name=" + name + ", age=" + age + "]";
}
}
启动类可以直接获得bean:
package testspringboot.test2;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.PropertySource;
@SpringBootApplication
@PropertySource("classpath:a.properties")
@PropertySource(value = "file:a.properties", ignoreResourceNotFound = true)
public class Test2Main {
/**
* @param args
*/
public static void main(String[] args) {
ConfigurableApplicationContext ctx = SpringApplication.run(Test2Main.class, args);
System.out.println(ctx.getBean(ABC.class));
}
}
启动结果:
可以直接获得配置的bean,也可以在代码里使用@Resource或者@Autowired获得;
加载yml文件
如果使用@PropertySource配置yml,则需要自定义一个factory实现:
package testspringboot.test2;
import java.io.IOException;
import java.util.Properties;
import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
import org.springframework.core.env.PropertiesPropertySource;
import org.springframework.core.env.PropertySource;
import org.springframework.core.io.support.EncodedResource;
import org.springframework.core.io.support.PropertySourceFactory;
public class YmlPropertiesFactory implements PropertySourceFactory {
@Override
public PropertySource<?> createPropertySource(String name, EncodedResource resource) throws IOException {
YamlPropertiesFactoryBean factoryBean = new YamlPropertiesFactoryBean();
factoryBean.setResources(resource.getResource());
factoryBean.afterPropertiesSet();
Properties source = factoryBean.getObject();
return new PropertiesPropertySource("myyml", source);
}
}
然后在@PropertySource里配置factory和yml文件:@PropertySource(value = "myapplication.yml", factory = YmlPropertiesFactory.class),就可以加载yml配置文件了;
来源:https://blog.csdn.net/FlyLikeButterfly/article/details/127488971
猜你喜欢
- 随机数的定义为:产生的所有数字毫无关系.在实际应用中很多地方会用到随机数,比如需要生成唯一的订单号.在C#中获取随机数有三种方法:一.Ran
- 本例只做简单功能演示,代码并不严谨,只是说明客户端如何实现连接服务端简单代码。代码在集成Eclipse工具下测试编译运行环境如下图所示:客户
- 在开发过程中,碰到生成一个List对象,需要对其里面的每个对象都进行校验。但是,这个Lis
- 今天,给大家分享一个Java后端利用Phantomjs实现生成图片的功能,同学们使用的时候,可以参考下!PhantomJS简介首先,什么是P
- SessionSession对象用于获取与数据库的物理连接。 Session对象是重量轻,设计了一个互动是需要与数据库每次被实例化。持久化对
- 问题描述:某天打开项目的activity的java文件界面突然变成下面这样了,但是用Notepad++打开代码什么的都正常,不知道什么原因造
- 一、redis发布订阅简介Redis发布订阅(pub/sub)是一种消息通信模式:发送者(pub)发送消息,订阅者(sub)接收信息。可以参
- 本文实例讲述了Java实现的Base64加密算法。分享给大家供大家参考,具体如下:一 算法实现1、JDK2、Commonc Codec3、B
- 本文实例讲述了Java设计模式之工厂模式实现方法。分享给大家供大家参考,具体如下:工厂模式主要是为创建对象提供过渡接口,以便将创建对象的具体
- 懒加载---就是我们在spring容器启动的是先不把所有的bean都加载到spring的容器中去,而是在当需要用的时候,才把这个对象实例化到
- demo下载重要代码://1、此layout作为最外层的layout;//2、设置需要调整的view: setAdjustView(View
- 一、bean实例化——构造方法(常用)bean本质上就是对象,创建bean使用构造方法完成BookD
- 混合事务在ORM框架的事务管理器的事务内,使用JdbcTemplate执行SQL是不会纳入事务管理的。下面进行源码分析,看为什么必须要在Da
- ArrayList实现班级信息管理系统,供大家参考,具体内容如下代码如下:import java.util.*;public class D
- void UpdateContactSign() {&n
- 1.新建一个数组,把原来数组的内容搬到新数组中。这种方法实现的思路是:先新建一个数组(前提条件是长度得比原来的长),然后把原来数组的内容搬到
- 1、在Android studio中进行打开一个项目的文件之后,然后进行点击Android stuio中菜单中的“tools”的选项。在弹出
- Java File类 mkdir 不能创建多层目录File f = new File("/home/jp/Upload"
- 本文实例为大家分享了C#生成PDF文件流的具体代码,供大家参考,具体内容如下1、设置字体static BaseFont FontBase =
- 本文为大家分享了AForge实现C#摄像头视频录制功能的具体方法,供大家参考,具体内容如下1. 概述最近由于兴趣学习了下在C#上使用AFor