问题1:Spring如何加载配置,配置文件位置?

1、默认位置:

Spring Boot默认的配置文件名称为application.properties,SpringApplication将从以下位置加载application.properties文件,并把它们添加到Spring Environment中:

  • 当前目录下的/config子目录,
  • 当前目录。
  • 一个classpath下的/config包
  • classpath根路径(root)

这个列表是按优先级排序的(列表中位置高的将覆盖位置低的)。并且,如果存在多个重名的application.properties。

注:Spring-boot配置文件的加载,先在与jar同级下查找,如果没有就去同级的config下查找;如果再没有,就在jar包中去查找相应的配置文件,如果再没有,就去jar包中的config下去查找。当查找到对应配置片段时,采用增量替换的方式来进行替换。

2、自定义位置:

如果不喜欢application.properties这个文件名或者需要自定义配置文件位置,在启动Spring应用的时候,可以传入一个spring.config.location参数指定配置文件位置,

例如:

java -jar xxxxx.jar   --spring.config.location=classpath:/default.properties,classpath:/override.properties

上述例子加载了两个配置文件,分别位于根目录下的:default.properties,override.properties。

问题2:配置文件如何加载到类文件中?

配置文件application.properties中配置属性:

test.name = wahaha
test.age = 27
test.tel = 18800118888

1、@Component和@Value("${"xxx"}")

类文件:

@Component
public class Configurations { @Value("${test.name}")
private String name; @Value("${test.age}")
private String age; @Value("${test.tel}")
private Long tel; // getter and setter
}

在类域属性上通过@Value("${xxx}")指定关联属性,Spring Application会自动加载。

启动类:

@RestController
@SpringBootApplication
public class ApplicationStarter { @Autowired
private Configuration configuration; @RequestMapping("/")
public Map<String, Object> sayHello() {
Map<String, Object> result = new HashMap<String, Object>();
result.put("name", configuration.getName());
result.put("age", configuration.getAge());
result.put("tel", configuration.getTel());
return result;
} public static void main(String[] args) throws Exception {
SpringApplication.run(ApplicationStarter.class, args);
}
}

运行,调用localhost:8080/会看到返回结果是:

{"name":"wahaha","tel":18800118888,"age":"27"}

2、@ConfigurationProperties(prefix = "test")

上述方法,手动书写@Value还是比较繁重的工作,好在Spring Boot提供了更简洁的方式。@ConfigurationProperties(prefix = "test")。prefix指定了配置文件的前缀为test,并且按照属性名进行自动匹配,例如:test.name属性值会自动加载到private String name域中。

@Component
@ConfigurationProperties(prefix = "test")
public class Configuration { private String name; private String age; private Long tel; // setter getter
}

PS:locations还能够指定自定义的配置文件位置,这里就不多说了。

@ConfigurationProperties(prefix = "test", locations = "classpath:xxxx.properties")

问题3:如何根据线上环境和线下环境加载不同的配置?如何加载多个配置文件?

1、Profiles:

Spring Profiles提供了一种隔离应用程序配置的方式,并让这些配置只能在特定的环境下生效。在配置文件中,用spring.profiles.active属性来指定特定环境的配置文件。在Spring Boot中多环境配置文件名需要满足application-{profile}.properties的格式,其中{profile}对应你的环境标识,比如:

  • application-dev.properties:开发环境
  • application-test.properties:测试环境
  • application-prod.properties:生产环境

至于哪个具体的配置文件会被加载,需要在application.properties文件中通过spring.profiles.active属性来设置,其值对应{profile}值。例如:

application.properties配置:spring.profiles.active = dev即指定application-dev.properties会被加载。

2、通过启动参数指定运行环境。

通过命令行启动:

  java -jar xxxxx.jar   --spring.profiles.active=prod。

上述指定为prod环境,则spring application会自动根据application-{profile}.properties的格式找到application-prod.properties文件加载。

3、加载多个自定义文件。

例如:

 spring.profiles.active = dev,database

等于告诉Spring Boot加载application-dev.properties和application-database.properties文件,从而实现多个配置文件的加载。

参见:https://my.oschina.net/wangyuefive/blog/704615#h3_4

Spring Boot加载配置文件的更多相关文章

  1. spring boot加载配置文件的顺序

    四个默认加载配置文件地方的优先级,四个文件相同配置有优先级概念  不同位置相互补充 外部配置文件不建议使用,不符合maven项目结构,打包会打不进去

  2. Spring Boot加载application.properties配置文件顺序规则

    SpringApplication会从以下路径加载所有的application.properties文件: 1.file:./config/(当前目录下的config文件夹) 2.file:./(当前 ...

  3. spring boot 加载web容器tomcat流程源码分析

    spring boot 加载web容器tomcat流程源码分析 我本地的springboot版本是2.5.1,后面的分析都是基于这个版本 <parent> <groupId>o ...

  4. Spring中加载配置文件的方式

    原文:http://blog.csdn.net/snowjlz/article/details/8158560 Spring 中加载XML配置文件的方式,好像有3种, XML是最常见的Spring 应 ...

  5. spring boot 加载自定义log4j 文件路径

    spring boot 使用log4j 打印时,需首先去除自带 Logger ,然后加入log4j 依赖 <dependencies> <!-- https://mvnreposit ...

  6. Spring boot加载REACTIVE源码分析

    一,加载REACTIVE相关自动配置 spring boot通过判断含org.springframework.web.reactive.DispatcherHandler字节文件就确定程序类型是REA ...

  7. spring boot 加载配置 文件

    在springboot启动的过程中,默契情况下会在classpath路径下加载application.properties当做系统配置文件,但有时候我们想要替换成另一个文件,可以 通过以下方式:   ...

  8. spring boot加载自定义配置

    1.通过@Value 配置文件中 wechat: ssh: host: 192.0.1.1 port: 22 加载类 @Component @Data public class SftpConfig ...

  9. spring Boot加载bean

    1.SpringBoot中加载bean,可以使用注解@compenent直接加载到applicationContext容器中 2.在直接类@Configuration中,手动注册bean,如:

随机推荐

  1. AOP中Advice执行两遍的原因

    在我的spring项目中,Aop的Advice执行了两边,就好像拦截了两遍一样. 原因是:切面应该切到接口的实现类上,而不是接口上

  2. 一次jdbc乱码解决

    今天我做了一个小实验,从sqlserver 2010中将一张表转移到mysql中,使用的是基本的jdbc,前面复制的好好地,不知道怎么了,到了第三万行,突然出现了下面的异常 Incorrect str ...

  3. ASP入门(十四)-FileSystemObject 对象

    File Access 组件中常见的对象有 FileSystemObject.TextStream.File.Folder.Drive等,其中每一个对象都有不同的属性和方法,我们将会在用到的时候进行详 ...

  4. 【python】安装bencode

    C:\Users\horn1\Desktop\python\35-pytorrent>pip install bencodeCollecting bencode Downloading http ...

  5. 一家VC支持企业的发展轨迹——了解每次融资后股权和期权的变化,以及股份是如何被稀释的【转载】

    来源:ReachVc 该文是 ReachVC 上一篇文章,是某个投资公司副总裁的独立博客,文章写得不错.如果你是一个不太了解融资的创业者,那么本文将对你很有用.通过武林外传同福客栈的例子,了解每次融资 ...

  6. guava 学习笔记 瓜娃(guava)的API快速熟悉使用

    1,大纲 让我们来熟悉瓜娃,并体验下它的一些API,分成如下几个部分: Introduction Guava Collection API Guava Basic Utilities IO API C ...

  7. Windows 之 可以Ping通服务器但无法使用服务器连接的共享打印机

    故障现象:一个公司内部局域网中,一台电脑可以Ping通服务器,但无法使用服务器连接的共享打印机. 故障分析与排除:根据故障现象分析,由于客户端可以Ping通服务器,说明网络连接正常,故障可能是由客户端 ...

  8. 2016 博客导读总结 &amp; 个人感悟

    此文着笔之时.2017已经在眼前了.预计等我写完,2017已经到了. 二次编辑于2017年1月1日早11点. 关于2016的感悟.十二月初就想写,当时认为是有点太早了,只是却思绪如泉涌. 且那时候才刚 ...

  9. node中__dirname、__filename、process.cwd()表示的路径

    直接上结论:__dirname 表示当前文件所在的目录的绝对路径__filename 表示当前文件的绝对路径module.filename ==== __filename 等价process.cwd( ...

  10. Linux下找不到动态链接库(转)

    1.生成静态库 生成静态库使用ar工具,其实ar是archive的意思 $ar cqs libhello.a hello.o 2.生成动态库 用gcc来完成,由于可能存在多个版本,因此通常指定版本号: ...