SpringBoot加载读取配置文件过程详细分析

目录
  • 配置文件的读取顺序
  • 多坏境的配置文件
  • 个性化配置
  • 自定义配置文件名称和路径
  • 加载yml文件

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配置文件了;

到此这篇关于SpringBoot加载读取配置文件过程详细分析的文章就介绍到这了,更多相关SpringBoot加载配置文件内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

(0)

相关推荐

  • SpringBoot配置文件的加载位置实例详解

    springboot采纳了建立生产就绪spring应用程序的观点. Spring Boot优先于配置的惯例,旨在让您尽快启动和运行.在一般情况下,我们不需要做太多的配置就能够让spring boot正常运行.在一些特殊的情况下,我们需要做修改一些配置,或者需要有自己的配置属性. SpringBoot启动会扫描以下位置的application.yml或者 application.properties文件作为SpringBoot的默认配置文件. -file:./config/    -file:./

  • Springboot常用注解及配置文件加载顺序详解

    Springboot常用注解及底层实现 1.@SpringBootApplication:这个注解标识了一个SpringBoot工程,她实际上是另外三个注解的组合,分别是: @SpringBootConfiguration:源码可以看到,这个注解除了元注解外,实际就只有一个@Configuration,把该类变成一个配置类,表示启动类也是一个配置类: @EnableAutoConfiguration:是开启自动配置的功能,向Spring容器中导入了一个Selector,用来加载ClassPath

  • SpringBoot配置加载,各配置文件优先级对比方式

    目录 1.SpringBoot配置文件 以设置应用端口为例 2.配置文件目录 3.自定义配置属性 自定义配置提示 4.指定配置文件 @PropertySource使用 装配yaml配置文件 @ImportResource使用 其他问题 idea使用*.properties文件出现中文乱码问题? 解决方法: 1.SpringBoot配置文件 SpringBoot使用一个以application命名的配置文件作为默认的全局配置文件.支持properties后缀结尾的配置文件或者以yml/yaml后缀

  • SpringBoot配置文件加载方法详细讲解

    目录 配置文件的读取顺序 多坏境的配置文件 个性化配置 自定义配置文件名称和路径 加载yml文件 配置文件的读取顺序 根目录/config/application.properties 根目录/config/application.yml 根目录/application.properties 根目录/application.yml classpath目录/config/application.properties classpath目录/config/application.yml classp

  • SpringBoot加载配置文件的实现方式总结

    目录 一.简介 二.代码实践 2.1.通过@value注解实现参数加载 2.2.通过@ConfigurationProperties注解实现参数加载 2.3.通过@PropertySource注解实现配置文件加载 2.4.通过自定义环境处理类,实现配置文件的加载 2.5.最后,我们来介绍一下yml文件读取 三.小结 一.简介 在实际的项目开发过程中,我们经常需要将某些变量从代码里面抽离出来,放在配置文件里面,以便更加统一.灵活的管理服务配置信息.比如,数据库.eureka.zookeeper.r

  • SpringBoot 配置文件加载位置与优先级问题详解

    [1]项目内部配置文件 spring boot 启动会扫描以下位置的application.properties或者application.yml文件作为Spring boot的默认配置文件 –file:./config/ –file:./ –classpath:/config/ –classpath:/ 即如下图所示: 以上是按照优先级从高到低(1-4)的顺序,所有位置的文件都会被加载,高优先级配置内容会覆盖低优先级配置内容. SpringBoot会从这四个位置全部加载主配置文件,如果高优先级

  • SpringBoot内部外部配置文件加载顺序解析

    内部配置加载顺序 SpringBoot 启动会扫描以下位置的application.properties或者application.yml文件作为Spring boot的默认配置文件 –file:./config/ –file:./ –classpath:/config/ –classpath:/ 优先级由高到底,高优先级的配置会覆盖低优先级的配置: SpringBoot会从这四个位置全部加载主配置文件:互补配置:还可以通过spring.config.location来改变默认的配置文件位置 项

  • SpringBoot项目实战之加载和读取资源文件

    目录 通过Resource接口 手动加载 通过@Value自动转换 通过ResourceLoader加载 使用ResourceUtils加载资源 读取资源中的内容 通过File对象读取 通过InputStream对象读取 文末总结 本文聊一聊在 SpringBoot 应用中,访问加载类路径(classpath)中的文件内容的多种方法. 通过Resource接口 Resource接口抽象出一种更底层的方式管理资源,可以实现通过统一的方式处理各类文件资源.下面是几种获取资源实例的方法. 手动加载 访

  • SpringBoot加载读取配置文件过程详细分析

    目录 配置文件的读取顺序 多坏境的配置文件 个性化配置 自定义配置文件名称和路径 加载yml文件 springboot默认读取的配置文件名字是:“application.properties”和“application.yml”,默认读取四个位置的文件:根目录下.根目录的config目录下.classpath目录下.classpath目录里的config目录下: 配置文件的读取顺序 根目录/config/application.properties 根目录/config/application.

  • SpringBoot加载外部依赖过程解析

    这篇文章主要介绍了SpringBoot加载外部依赖过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 背景 公司一个项目的大数据平台进行改造,之前使用Structured Streaming作为实时计算框架,需要替换为替换为Kafka Streams,并使用SpringBoot包装,使其可以纳入微服务体系. 然而由于之前并没有接触过SpringFramework相关技术,并且项目工期较为紧张,因此只好花了2天时间看了看Spring和Spri

  • 关于springboot加载yml配置文件的no字段自动转义问题

    目录 加载yml配置文件的no字段自动转义 springboot配置文件自动转译的坑 小结一下 加载yml配置文件的no字段自动转义 项目上线了才发现一个字段被转义了,如下图: 本来应该会拿到no字段和数据进行比对的,结果发现比对完的数据这个字段全是null,debug才发现这个字段这么写在加载yml文件之后自动变成了"false",第一次发现这个问题,修改很方便,把yml文件里面这个no,换成'no'就可以不被转义成"false"了,谨以此提醒,小错误导致临时调整

  • 解决SpringBoot加载application.properties配置文件的坑

    SpringBoot加载application.properties配置文件的坑 事情的起因是这样的 一次,本人在现场升级程序,升级完成后进行测试,结果接口调用都报了这么个错误: 大概意思是https接口需要证书校验,这就奇怪了,项目启动加载的是包外的application.properties配置文件,配置文件里没有配置使用https啊.本人马上检查了下包内的application.properties配置文件,发现包内确实配置了https相关的配置项: 明明包外的配置文件优先级高于包内的,为

  • SpringBoot单点登录实现过程详细分析

    目录 1.具体实现步骤 2.代码展示 后台代码 前台代码 效果展示 1.具体实现步骤 添加拦截器,设置UUID作为唯一标识,存入数据库中 通过当前登陆者的账户进行查询 如果当前登陆者session中存入的UUID与我们数据库中的UUID值相同则通过 否则返回false,表示已在其他设备或浏览器登录登录 2.代码展示 首先我们新建一个Spring项目 添加以下几个依赖 yml配置文件 server:  port: 8080spring:  datasource:    driver-class-n

  • SpringBoot预加载与懒加载实现方法超详细讲解

    目录 预加载 getMergedLocalBeanDefinition 循环创建bean 懒加载 @Lazy 全局懒加载 为什么需要全局懒加载 全局懒加载的好处与问题 预加载 bean在springBoot启动过程中就完成创建加载 在AbstractApplicationContext的refresh方法中 // Instantiate all remaining (non-lazy-init) singletons. beanFactory.preInstantiateSingletons()

  • SpringBoot自动配置特点与原理详细分析

    目录 一.SpringBoot是什么 二.SpringBoot的特点(核心功能) 三.SpringBoot的自动配置原理 1. @SpringBootApplication 2. @SpringBootConfiguration 3. @EnableAutoConfiguration 4. @ComponentScan 四.核心原理图 五.常用的Conditional注解 一.SpringBoot是什么 Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Sprin

  • springboot加载命令行参数ApplicationArguments的实现

    目录 一.介绍 二.通过应用程序参数获取配置 1. 通过bean获取应用程序参数 2. 通过@Value注解获取 三.源码解读 - 封装应用程序参数 1. DefaultApplicationArguments 2. Source类 3. SimpleCommandLinePropertySource 4. SimpleCommandLineArgsParser 5. CommandLinePropertySource 6. PropertySource 四.源码解读 - 为什么可以通过@Val

随机推荐