springboot之配置文件
springboot在加载配置文件的时候是有先后顺序的,了解加载配置文件的先后顺序,可以减少编写程序出现错误
1 springboot加载配置文件的先后顺序如下:
SpringApplication将从以下位置加载application.properties文件,并把它们添加到Spring Environment中:
- 当前目录下的
/config子目录。 - 当前目录。
- classpath下的
/config包。 - classpath根路径(root)
启动的时候,1中的配置文件优先级最高,会覆盖2,3,4中的配置信息
2 工程结构如图:

代码如下:
package com.rookie.bigdata.config; import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component; /**
* springboot注入随机值
* 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]}
* <p>
* <p>
* Created by on 2018/9/29.
*/
@Component
//此注解可以省略
//@ConfigurationProperties
public class RandomConfig { @Value("${random.value}")
private String secret;
@Value("${random.long}")
private Long number; @Value("${random.int(10)}")
private String numberLess; @Value("${random.int[1024,65536]}")
private Integer numberRange; @Value("${name}")
private String name; public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getSecret() {
return secret;
} public void setSecret(String secret) {
this.secret = secret;
} public Long getNumber() {
return number;
} public void setNumber(Long number) {
this.number = number;
} public String getNumberLess() {
return numberLess;
} public void setNumberLess(String numberLess) {
this.numberLess = numberLess;
} public Integer getNumberRange() {
return numberRange;
} public void setNumberRange(Integer numberRange) {
this.numberRange = numberRange;
}
}
package com.rookie.bigdata.config; import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component; /**
*
* springboot允许使用占位符进行配置
* Created by on 2018/9/29.
*/
@Component
public class AppConfig {
@Value("${app.name}")
private String appName;
@Value("${app.description}")
private String appDesc; public String getAppName() {
return appName;
} public void setAppName(String appName) {
this.appName = appName;
} public String getAppDesc() {
return appDesc;
} public void setAppDesc(String appDesc) {
this.appDesc = appDesc;
}
}
package com.rookie.bigdata; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; /**
* 应用程序启动类
* Created by on 2018/8/2.
*/
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication springApplication = new SpringApplication();
//通过设置该参数禁用命令行属性添加到Environment
// springApplication.setAddCommandLineProperties(false); springApplication.run(Application.class, args); }
}
package com.rookie.bigdata.config; import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner; import static org.junit.Assert.*; /**
* Created by liuxili on 2018/9/29.
*/
@RunWith(SpringRunner.class)
@SpringBootTest
public class AppConfigTest { @Autowired
AppConfig appConfig; @Test
public void test1() {
System.out.println(appConfig.getAppName());
System.out.println(appConfig.getAppDesc());
} }
package com.rookie.bigdata.config; import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner; import static org.junit.Assert.*; /**
* Created by liuxili on 2018/9/29.
*/
@RunWith(SpringRunner.class)
@SpringBootTest
public class RandomConfigTest { @Autowired
RandomConfig randomConfig; @Test
public void test1(){
System.out.println(randomConfig.getSecret());
System.out.println(randomConfig.getNumber());
System.out.println(randomConfig.getNumberLess());
System.out.println(randomConfig.getNumberRange());
System.out.println(randomConfig.getName());
}
}
配置文件如下
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]}
name=lisi
#属性占位符
#当使用application.properties定义的属性时,Spring会先通过已经存在的Environment查找该属性,所以你可以引用事先定义的值
app.name=appStore
app.description=${app.name} is a Spring Boot application
connection.username=root
connection.password=roots
3、使用@Value("${property}")注解注入配置属性有时会比较麻烦,特别是需要使用多个properties,或数据本身有层次结构。Spring Boot提供一种使用配置的替代方法,这种方法允许强类型的beans以管理和校验应用的配置,代码如下:
package com.rookie.bigdata.config; import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component; /**
* Created by liuxili on 2018/9/29.
*/
//@Component
@Configuration
@ConfigurationProperties(prefix = "connection")
public class ConnectionConfig { private String userName; private String passWord; public String getUserName() {
return userName;
} public void setUserName(String userName) {
this.userName = userName;
} public String getPassWord() {
return passWord;
} public void setPassWord(String passWord) {
this.passWord = passWord;
}
}
package com.rookie.bigdata.config; import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner; import static org.junit.Assert.*; /**
* Created by liuxili on 2018/9/29.
*/
@RunWith(SpringRunner.class)
@SpringBootTest
public class ConnectionConfigTest { @Autowired
private ConnectionConfig connectionConfig; @Test
public void test1(){
System.out.println(connectionConfig.getPassWord());
System.out.println(connectionConfig.getUserName());
}
}
属性名配置一般规则如下:
| 属性 | 说明 |
|---|---|
person.firstName |
标准驼峰规则 |
person.first-name |
虚线表示,推荐用于.properties和.yml文件中 |
person.first_name |
下划线表示,用于.properties和.yml文件的可选格式 |
PERSON_FIRST_NAME |
大写形式,使用系统环境变量时推荐 |
对于使用yml文件配置跟这里配置差不多,这里不再赘述,看个人喜好,有人喜好properties进行配置,有人喜好yml文件进行配置
springboot之配置文件的更多相关文章
- SpringBoot之配置文件加载位置
1.SpringBoot启动会扫描application.properties或者application.yml文件作为springboot的配置文件.默认创建项目生成application.prop ...
- [SpringBoot] - 了解什么是SpringBoot,使用SpringBoot的配置文件
首先明白Spring是什么,Spring是Java开发的一个框架,为了方便简化Java开发. 什么是注解(注解式开发)? Spring的常用注解有哪些? 假如用SpringBoot构建一个网站程序,应 ...
- springboot读取配置文件的顺序
前言 今天测试一些东西,发现配置文件连接的数据库一直不正常,数据也不对,今天请教了之后,原来springboot的配置文件加载不仅仅是项目内的配置文件. 正文 项目目录是这样的:文件夹下有:项目,ap ...
- SpringBoot读取配置文件源码探究
1. SpringBoot读取配置文件源码探究 1.1. 概览 springboot的源码是再原来的Spring源码上又包了一层,看过spring源码都知道,当我们从入口debug进去的时候,原来的S ...
- springboot yml配置文件注入值
1.编写javabean: package com.example.springboot.bean; import org.springframework.boot.context.propertie ...
- SpringBoot:配置文件及自动配置原理
西部开源-秦疆老师:基于SpringBoot 2.1.6 的博客教程 秦老师交流Q群号: 664386224 未授权禁止转载!编辑不易 , 转发请注明出处!防君子不防小人,共勉! SpringBoot ...
- SpringBoot系列:二、SpringBoot的配置文件
SpringBoot的配置文件在resources文件夹下 springboot的配置文件支持两种形式的写法,一种是经典的properties另一种是yml yml通过空格缩进的形式来表示对象的层级关 ...
- 「快学SpringBoot」配置文件的加载顺序和配置项默认值设置
前言 有的时候,配置信息是我们无法在开发过程中就能确定的.比如,给客户开发的项目,客户需要根据自身的情况自定义配置,如数据库配置,加密密钥配置等等.这时候,就需要把配置文件放在外面,让用户自定义配置部 ...
- springboot指定配置文件运行
1.springboot指定配置文件运行 创建三个配置文件如下: application.properties内容如下: spring.profiles.active=rabbit如上配置,在运行时就 ...
- SpringBoot(二) SpringBoot核心配置文件application.yml/properties
我们都知道在Spring中有着application.xml文件对Spring进行相关配置,通过web.xml中的contextConfigLocation指定application.xml文件所在位 ...
随机推荐
- rem布局完成响应式开发,通俗且详细的原理解析和代码实现
一.rem布局基本原理 原理:rem可以理解为一个长度单位,单位rem的值等于网页font-size的值.如果网页的字体大小为默认值16px,那么1rem就等于16px,0.5rem等于8px. 根据 ...
- 【安富莱二代示波器教程】第16章 附件A---电阻屏触摸校准
第16章 附件A---电阻屏触摸校准 二代示波器的触摸校准比较简单,随时随地都可以做触摸校准,按下K1按键即可校准.有时候我们做触摸校准界面,需要在特定的界面才可以进入触摸校准状态,非常繁琐 ...
- 【子集或者DFS】部分和问题
题目: 给定整数序列a1,a2,...,an,判断是否可以从中选出若干数,使它们的和恰好为k.1≤n≤20 -108≤ai≤108 -108≤k≤108 输入: n=4 a={1,2,4,7} ...
- [Swift]LeetCode856. 括号的分数 | Score of Parentheses
Given a balanced parentheses string S, compute the score of the string based on the following rule: ...
- PHP常用设计模式讲解
开发中适当的使用设计模式,可以让项目有更易扩展,易维护.低耦合,代码简洁等 单例模式 <?php /** * 单例模式:使类在全局范围内只允许创建一个对象,常用于数据库连接等 */ class ...
- 定时任务 winform开发
在项目中我们经常遇到与时间结合的无限或者有限轮回的任务.例如每月一号统计工作量,基本这种情况,都会是设置定时任务,定时执行.好了,下面就记录一下定时任务的开发吧. 首先描述一下开发思路: 建立一个wi ...
- Python 使用图灵机器人实现微信聊天功能
首先需要去图灵官网创建一个属于自己的机器人然后得到apikey. 一.自动与指定好友聊天 # -*- coding: utf-8 -*- """ Created at 2 ...
- SpringMVC+JWT+Swagger UI+RestFul
前言: 其实很早就想写这篇文章了,因为我觉得这会对很多新手有指引作用,当初自己也是瞎子过河的摸索着过来的.目前后台开发比较流行的MVC框架中使用Spring MVC还是比较多的,当然还有Spring ...
- SH2018笔试题之成长值问题
一.题目 详见代码 二.代码 import java.util.Scanner; /** * 3 * 1 1 5 10 * 2 3 4 * 1 4 6 -5 */ public class main4 ...
- JVM基础系列开篇:为什么要学虚拟机?
跟许多人一样,我一开始接触 Java 虚拟机只是因为面试需要用到,所以硬着头皮看看.所以很多人对于为什么要学虚拟机这个问题,他们的答案都是:因为面试.但我经过了几年的学习和实战,我发现其实学习虚拟机并 ...