SpringBoot 读取配置文件及profiles切换配置文件
读取核心配置文件
核心配置文件是指在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切换配置文件的更多相关文章
- SpringBoot利用spring.profiles.active=@spring.active@不同环境下灵活切换配置文件
一.创建配置文件 配置文件结构:这里建三个配置文件,application.yml作为主配置文件配置所有共同的配置:-dev和-local分别配置两种环境下的不同配置内容,如数据库地址等. appli ...
- SpringBoot读取配置文件源码探究
1. SpringBoot读取配置文件源码探究 1.1. 概览 springboot的源码是再原来的Spring源码上又包了一层,看过spring源码都知道,当我们从入口debug进去的时候,原来的S ...
- SpringBoot配置文件-多环境切换
profile是Spring对不同环境提供不同配置功能的支持,可以通过激活不同的环境版本,实现快速切换环境: 多个文件-配置多环境: 需要多个配置文件,文件名可以是 application-{prof ...
- springboot读取properties和yml配置文件
一.新建maven工程:springboot-configfile-demo,完整工程如下: pom.xml <?xml version="1.0" encoding=&qu ...
- jar包读取jar包内部和外部的配置文件,springboot读取外部配置文件的方法
jar包读取jar包内部和外部的配置文件,springboot读取外部配置文件的方法 用系统属性System.getProperty("user.dir")获得执行命令的目录(网上 ...
- Springboot读取自定义配置文件的几种方法
一.读取核心配置文件 核心配置文件是指在resources根目录下的application.properties或application.yml配置文件,读取这两个配置文件的方法有两种,都比较简单. ...
- springboot读取自定义properties配置文件方法
1. 添加pom.xml依赖 <!-- springboot configuration依赖 --> <dependency> <groupId>org.sprin ...
- springboot读取配置文件中的信息
在一个项目中,我们有时候会把一些配置信息写入到一个配置文件中,在java代码中读取配置文件的信息.在此记录下读取属性文件中的内容. 在springboot项目中,springboot的配置文件可以使用 ...
- SpringBoot读取外部配置文件的方法
SpringBoot读取外部配置文件的方法 Spring高级之注解@PropertySource详解(超详细) 1.@PropertySource(value = {"classpath:c ...
随机推荐
- springboot @PropertySource
@ConfigurationProperties(prefix="person") 默认加载全局配置文件 application.properties或application.ym ...
- mybatis源码-解析配置文件(二)之解析的流程
目录 1. 简介 2. 配置文件解析流程分析 2.1 调用 2.2 解析的目的 2.3 XML 解析流程 2.3.1 build(parser) 2.3.2 new XMLConfigBuilder( ...
- Centos 7 安装mysql5.7.24二进制 版本
Mysql 二进制安装方法 下载mysql https://dev.mysql.com/downloads/mysql/ 1.解压包 tar xf mysql-5.7.24-linux-glibc2. ...
- jenkins pipeline 部署
一.git 版本控制结合jenkins 发布 sh-4.2$ git branch sh-4.2$ git chekout master sh-4.2$ git tag v1.1 sh-4.2$ gi ...
- 父类与子类this相关问题
1.SinglyLinkedList: package No3_PolySinglyList; /*实现 带头结点的单链表SinglyLinkedList类*/ public class Singly ...
- C# 导入(读取) WPS ET文件
本文章介绍基于VS2010 Winform 的WPS2016二次开发 ET数据读取程序 本程序支持多个Sheet页面 前提:引用WPS安装目录下的etapi.dll private void butt ...
- Daily Scrum NO.5
工作概况 符美潇 昨日完成的工作 1.Daily Scrum.日常会议及日常工作的分配和查收. 2.变更集461代码签入,主要与视频链接爬取有关. 今日工作 1.Daily Scrum.日常会议及日常 ...
- [2017BUAA软工助教]结对组队
请同学们把第一次结对编程双方的学号评论在本博客下,只要一位同学评论即可.例如: 14061195 + 14061183
- Java实现模拟登录新浪微博
毕设题目要使用到新浪微博数据,所以要爬取新浪微博的数据.一般而言,新浪微博的爬虫有两种模式:新浪官方API和模拟登录新浪微博.两种方法的异同点和适用情况就无须赘述了.前辈的文章已经非常多了.写这篇文章 ...
- PAT乙级(Basic Level)练习题-NowCoder数列总结
题目描述 NowCoder最近在研究一个数列: F(0) = 7 F(1) = 11 F(n) = F(n-1) + F(n-2) (n≥2) 他称之为NowCoder数列.请你帮忙确认一下数列中第n ...