关键词:
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之基础配置文件① 两种全局配置文件(文件名是固定的) 配置文件放在src/main/resources目录或者类路径/config下 application.properties(优先级高) application.yml/application.yaml 配置文 查看详情
springboot之配置文件application.properties
SpringBoot默认的配置文件为application.properties,放在src/main/resources目录下或者类路径的/config目录下,作为SpringBoot的全局配置文件,对一些默认的配置进行修改。接下来使用例子展示SpringBoot配置文件的用法:首先在src/main/resources下... 查看详情
springboot之加载自定义配置文件
SpringBoot默认加载配置文件名为:application.properties和application.yml,如果需要使用自定义的配置文件,则通过@PropertySource注解指定。 JavaBean:packageorg.springboot.model;importlombok.Data;importorg.springframework.boot.context.pr 查看详情
springboot配置之配置文件占位符
RandomValuePropertySource:配置文件中可以使用随机数${random.value}、${random.int}、${random.long}、${random.int(10)}、${random.int[123,456]}属性占位符app.name=MyAppapp.description=${app.name}isaspringbootapplication可以在配置文件 查看详情
详解springboot配置文件之多环境配置
一.多环境配置的好处:1.不同环境配置可以配置不同的参数~2.便于部署,提高效率,减少出错~二.properties多环境配置1.配置激活选项spring.profiles.active=dev2.添加其他配置文件application.properties:#激活哪一个环境的配置文件spring.profiles... 查看详情
微服务之springboot自定义配置application.properties文件
配置的文件的格式springboot可以识别两种格式的配置文件,分别是yml和properties文件。我们可以将application.properties文件换成application.yml,这两个文件都可以被SpringBoot自动识别并加载,但是如果是自定义的配置文件,就最好还是使... 查看详情
springboot配置之配置文件的加载顺序
springboot启动时会扫描一下位置的application.properties或者application.yml文件作为默认配置文件:file:./config/file:./classpath:/config/classpath:/以上是按照优先级从高到低的顺序,所有位置的文件都会被加载,高优先级配置会覆盖低优先级配... 查看详情
springboot之简单日志配置
...inkerDesktopmyfileootLog1使用path方式文件会默认10M分割比如:springboot1.logspringboot2.log…...2注意下上 查看详情
java框架之springboot三:springboot自定义配置一
java框架之Springboot三:SpringBoot自定义配置一私有化配置文件刚才我们介绍了在主配置文件汇中配置对应的文件,如果我们想要自定义配置文件该怎么处理呢?现在就要给大家介绍我们的@PropertySource注解。@PropertyS... 查看详情
4springboot配置文件之profile
Profile 1、多Profile文件我们在主配置文件编写的时候,文件名可以是application-{profile}.properties/yml默认使用application.properties的配置; 2、yml支持多文档块方式 使用 --- 区分文档一、文档二、文档三server:port:8081spring:pr... 查看详情
java框架之springboot二:springboot配置获取
java框架之Springboot二:SpringBoot配置获取resources文件夹中的目录结构:static:保存所有的静态资源;js,css,imagestemplates:保存所有的模板页面;application.properties:配置文件,可以修改一些默认配置配置文件... 查看详情
springboot框架学习学前掌握之重要注解-通过注解方式读取外部资源配置文件2
...据库连接池声明:本文是《凯哥陪你学系列-框架学习之springboot框架学习》中springboot框架学习学前掌握之重要注解(4)-通过注解方式读取外部资源配置文件2。欢迎大家提意见一:上节思考问题:1:如果是多个配置文件怎么写?2... 查看详情
补习系列(10)-springboot之配置读取
...曾经写过Spring程序通过Bean映射实现配置信息的读取。在SpringBoot框架中读取配置的方式变得非常多样 查看详情
java面试之springboot/springcloud
104.什么是springboot?springboot是为spring服务的,是用来简化新spring应用的初始搭建以及开发过程的。105.为什么要用springboot?配置简单独立运行自动装配无代码生成和 xml配置提供应用监控易上手提升开发效率106.springboot核心配... 查看详情
java面试之springboot/springcloud
104.什么是springboot?springboot是为spring服务的,是用来简化新spring应用的初始搭建以及开发过程的。105.为什么要用springboot?配置简单独立运行自动装配无代码生成和 xml配置提供应用监控易上手提升开发效率106.springboot核心配... 查看详情
java框架之springboot三:springboot自定义配置一
java框架之Springboot三:SpringBoot自定义配置一私有化配置文件刚才我们介绍了在主配置文件汇中配置对应的文件,如果我们想要自定义配置文件该怎么处理呢?现在就要给大家介绍我们的@PropertySource注解。@PropertyS... 查看详情
springboot参考教程springboot配置使用之配置类用法
4.2.SpringBoot配置使用之配置类使用SpringBoot的大部分自动配置都可以满足应用要求,但如果想精确的控制应用,或者想覆盖自动配置,使用配置类是另一种很好的选择,强调一下,SpringBoot主要使用配置文件和配置类来做配置。1.&nb... 查看详情
java框架之springboot二:springboot配置获取
java框架之Springboot二:SpringBoot配置获取resources文件夹中的目录结构:static:保存所有的静态资源;js,css,imagestemplates:保存所有的模板页面;application.properties:配置文件,可以修改一些默认配置配置文件... 查看详情