读取核心配置文件

核心配置文件是指在resources根目录下的application.properties或application.yml配置文件,读取这两个配置文件的方法有两种,都比较简单。

先创建一个简单的springBoot程序,可以参考:

http://www.cnblogs.com/lspz/p/6344327.html

一、通过@value注解来读取

核心配置文件application.properties内容如下:

server.port=9090

test.msg=Hello World Springboot!

编制Example.java

  package com.example.web;

    import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; @RestController
public class Example { @RequestMapping("/")
public String home(@Value("${test.msg}") String msg) {
return msg;
}
}

注意:在@Value的${}中包含的是核心配置文件中的键名。在Controller类上加@RestController表示将此类中的所有视图都以JSON方式显示,类似于在视图方法上加@ResponseBody,spring boo默认已经配置了很多环境变量,例如,tomcat的默认端口是8080,项目的contextpath是“/”等等,我们在application.properties中设置了server.port=9090,重写了spring boot 内嵌tomcat端口。

访问:http://localhost:9090 时将得到 Hello World Springboot!

二、使用Environment方式

 package com.example.web;

	import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController; @RestController
public class WebController {
@Autowired
private Environment env; @RequestMapping(value = "index", method = RequestMethod.GET)
public String index() {
return env.getProperty("test.msg");
}
}

注意:这种方式是依赖注入Evnironment来完成,在创建的成员变量private Environment env上加上@Autowired注解即可完成依赖注入,然后使用env.getProperty("键名")即可读取出对应的值。

访问:http://localhost:9090/index 时将得到Hello World Springboot!

三、读取自定义配置文件

为了不破坏核心文件的原生态,但又需要有自定义的配置信息存在,一般情况下会选择自定义配置文件来放这些自定义信息,这里在resources目录下创建配置文件my-web.properties

resources/my-web.properties内容如下:

com.name=testName
com.password=123

创建管理配置的实体类:

package com.example.model;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component; @Component
@PropertySource("classpath:/my-web.properties")
@ConfigurationProperties(prefix = "com")
public class ConfigBean {
private String name;
private String password; public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
}
}

注意:

spring boot1.5以上版本@ConfigurationProperties取消了location需要用@PropertySource来指定自定义的资源目录。

prefix:指定配置文件中键名称的前缀(我这里配置文件中所有键名都是以web.开头)

使用@Component是让该类能够在其他地方被依赖使用,即使用@Autowired注释来创建实例。

创建测试Controller

package com.example.web;

import com.example.model.ConfigBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; @RestController
public class UserController {
@Autowired
ConfigBean configBean; @RequestMapping("/user")
public String user() {
return configBean.getName() + ":" + configBean.getPassword();
}
}

由于在ConfigBean类上加了注释@Component,所以可以直接在这里使用@Autowired来创建其实例对象。

访问:http://localhost:9090/user 时将得到testName:123

四、参数间引用

可以利用${…}在application.properties引用变量

myapp.name=spring

myapp.desc=${myapp.name} nice

五、在application.properties配置随机变量

在application.properties配置随机变量,利用的是RandomValuePropertySource类

my.secret=${random.value}
my.number=${random.int}
my.bignumber=${random.long}
my.number.less.than.ten=${random.int(10)}
my.number.in.range=${random.int[1024,65536]}

六、使用profiles实现快速切换配置

新建一个properties文件application-prod.properties对应为

生产环节的配置。

application-prod.properties:

server.port=8080
test.msg=This is prod!

接下来,使用CMD进入src目录打包jar:

mvn package -Dmaven.test.skip=true

success后使用

java -jar -Dspring.profiles.active=prod target/loadProperties-0.0.1-SNAPSHOT.jar运行。

访问:http://localhost:8080/ 时得到 This is prod!

注意:

发现端口已经变成8080了。这是因为在“application-prod.properties”中规定了server.port=8080。

“java -jar”的命令中使用-D来传递参数:

java -jar -D配置=值 jar名.jar

“-Dspring.profiles.active=”用来指定切换到哪个配置,表达式为:“application-${profile}.properties”

七、配置文件优先级

application.properties和application.yml文件可以放在一下四个位置:

外置,在相对于应用程序运行目录的/congfig子目录里。

外置,在应用程序运行的目录里

内置,在config包内

内置,在Classpath根目录

同样,这个列表按照优先级排序,也就是说,src/main/resources/config下application.properties覆盖src/main/resources下application.properties中相同的属性,此外,如果你在相同优先级位置同时有application.properties和application.yml,那么application.yml里面的属性就会覆盖application.properties里的属性。

SpringBoot 读取配置文件及profiles切换配置文件的更多相关文章

  1. SpringBoot利用spring.profiles.active=@spring.active@不同环境下灵活切换配置文件

    一.创建配置文件 配置文件结构:这里建三个配置文件,application.yml作为主配置文件配置所有共同的配置:-dev和-local分别配置两种环境下的不同配置内容,如数据库地址等. appli ...

  2. SpringBoot读取配置文件源码探究

    1. SpringBoot读取配置文件源码探究 1.1. 概览 springboot的源码是再原来的Spring源码上又包了一层,看过spring源码都知道,当我们从入口debug进去的时候,原来的S ...

  3. SpringBoot配置文件-多环境切换

    profile是Spring对不同环境提供不同配置功能的支持,可以通过激活不同的环境版本,实现快速切换环境: 多个文件-配置多环境: 需要多个配置文件,文件名可以是 application-{prof ...

  4. springboot读取properties和yml配置文件

    一.新建maven工程:springboot-configfile-demo,完整工程如下: pom.xml <?xml version="1.0" encoding=&qu ...

  5. jar包读取jar包内部和外部的配置文件,springboot读取外部配置文件的方法

    jar包读取jar包内部和外部的配置文件,springboot读取外部配置文件的方法 用系统属性System.getProperty("user.dir")获得执行命令的目录(网上 ...

  6. Springboot读取自定义配置文件的几种方法

    一.读取核心配置文件 核心配置文件是指在resources根目录下的application.properties或application.yml配置文件,读取这两个配置文件的方法有两种,都比较简单. ...

  7. springboot读取自定义properties配置文件方法

    1. 添加pom.xml依赖 <!-- springboot configuration依赖 --> <dependency> <groupId>org.sprin ...

  8. springboot读取配置文件中的信息

    在一个项目中,我们有时候会把一些配置信息写入到一个配置文件中,在java代码中读取配置文件的信息.在此记录下读取属性文件中的内容. 在springboot项目中,springboot的配置文件可以使用 ...

  9. SpringBoot读取外部配置文件的方法

    SpringBoot读取外部配置文件的方法 Spring高级之注解@PropertySource详解(超详细) 1.@PropertySource(value = {"classpath:c ...

随机推荐

  1. Hexo站点之域名配置【2】

    该系列博客列表请访问:http://www.cnblogs.com/penglei-it/category/934299.html 摘要 因为Hexo个人博客是托管在github之上,每次访问都要使用 ...

  2. Jmeter(二十二)_脚本上传Gitlab

    Docker部署接口自动化持续集成环境第四步,代码上传到远程仓库! 接上文:Ubuntu部署jmeter与ant Gitlab在容器中部署好了之后,本地直接打开.我们可以在里面创建项目,上传脚本. 新 ...

  3. 前端项目模块化的实践2:使用 Webpack 打包基础设施代码

    以下是关于前端项目模块化的实践,包含以下内容: 搭建 NPM 私有仓库管理源码及依赖: 使用 Webpack 打包基础设施代码: 使用 TypeScript 编写可靠类库 使用 TypeScript ...

  4. C#_Winfrom将浏览器生成Image

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  5. Git提交空目录

    1.git仅跟踪文件的变动,不跟踪目录.如果需要提交空目录,可以在里面添加 .gitignore 文件,方法如下: find . -type d -empty -exec touch {}/.giti ...

  6. win10安装tensorflow-gpu

    1.安装anaconda (最好使用清华源下载) 2.打开cmd conda create -n tensorflow pip python=3.6 activate tensorflow pip i ...

  7. Apache Ignite 学习笔记(三): Ignite Server和Client节点介绍

    在前两篇文章中,我们把Ignite集群当做一个黑盒子,用二进制包自带的脚本启动Ignite节点后,我们用不同的客户端连接上Ignite进行操作,展示了Ignite作为一个分布式内存缓存,内存数据库的基 ...

  8. kubeadm安装K8S单master双节点集群

    宿主机:master:172.16.40.97node1:172.16.40.98node2:172.16.40.99 # 一.k8s初始化环境:(三台宿主机) 关闭防火墙和selinux syste ...

  9. 华为笔试——C++平安果dp算法

    题目:平安果 题目介绍:给出一个m*n的格子,每个格子里有一定数量的平安果,现在要求从左上角顶点(1,1)出发,每次走一格并拿走那一格的所有平安果,且只能向下或向右前进,最终到达右下角顶点(m,n), ...

  10. VPS性能测试(3):磁盘IO读写速度、SSD硬盘速度测试

    1.磁盘IO,即输入/输出(Input/Output),这是测试磁盘性能一个重要指标,一些便宜的VPS主机为了降低成本,以大量的低性能的硬盘来充当服务器,导致VPS主机因为IO差而拖了整个主机性能的后 ...