springboot之配置文件

摘要:
当前目录/类路径下的config包。

springboot在加载配置文件的时候是有先后顺序的,了解加载配置文件的先后顺序,可以减少编写程序出现错误

1 springboot加载配置文件的先后顺序如下:

SpringApplication将从以下位置加载application.properties文件,并把它们添加到Spring Environment中:

  1. 当前目录下的/config子目录。
  2. 当前目录。
  3. classpath下的/config包。
  4. classpath根路径(root)

  启动的时候,1中的配置文件优先级最高,会覆盖2,3,4中的配置信息

2 工程结构如图:

springboot之配置文件第1张

代码如下:

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之配置文件》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇C# 通过GUID生成不重复的IDClion Debug模式使用实践下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

带中文的字符串截取

最近在页面展示的时候遇到这样的场景,文字有可能超长,却又不允许换行。 当然,可以用高超的css来搞定。但如果你想要让多余的文字用“...”来代替,并且要兼容很多浏览器,这种时候用css也会很头疼吧。 1.C#对中文字符串的截取 与英文字符相比,我们把中文字符按两个占位来计算,对于带中文的字符串截取,要面临两个问题: 1.无法截取半个中文字符; 2.对于除...

使用jquery刷新当前页面

如何使用jquery刷新当前页面 下面介绍全页面刷新方法:有时候可能会用到 window.location.reload()刷新当前页面. parent.location.reload()刷新父亲对象(用于框架) opener.location.reload()刷新父窗口对象(用于单开窗口) top.location.reload()刷新最顶端对象(用于...

nodejs中Buffer的创建和转换

  buffer是用来做什么?主要是用来处理二进制文件流和TCP流的文件缓存区。我们可以将二进制流和string,json,int进行转换,也可以进行复制,或者通过自带的函数进行判断buffer的一些状态。   创建Buffer对象(实例)     1.使用var buffer = new Buffer(size)创建对象,然后用buffer.fill(v...

解决Flink消费Kafka信息,结果存储在Mysql的重复消费问题

背景 最近项目中使用Flink消费kafka消息,并将消费的消息存储到mysql中,看似一个很简单的需求,在网上也有很多flink消费kafka的例子,但看了一圈也没看到能解决重复消费的问题的文章,于是在flink官网中搜索此类场景的处理方式,发现官网也没有实现flink到mysql的Exactly-Once例子,但是官网却有类似的例子来解决端到端的仅一...

WPF多语言化的实现

  Metro插件系统系列就暂时停一下,这次我们讨论一下WPF的资源本地化实现,主要用到的:CultureInfo,ResourceManger,MarkupExtension,RESX文件,这些都是.NET框架提供的。 项目结构: 运行结果: 可在程序运行时,实时切换语言 CultureInfo   CultureInfo类表示有关特定区域性的信息...

开发规约(一)接口统一返回值格式

一、前言 上篇在介绍 Spring Boot 集成 Dubbo 时,埋下了有关返回值格式的一个小小伏笔。本篇将主要介绍一种常用的返回值格式以及通过什么手段去达成这个目的。 二、Dubbo 接口统一返回值格式 我们在应用中经常会涉及到 server 和 client 的交互,目前比较流行的是基于 json 格式的数据交互。但是 json 只是消息的格式,其...