欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

springboot之配置文件

程序员文章站 2023-11-10 21:23:22
springboot在加载配置文件的时候是有先后顺序的,了解加载配置文件的先后顺序,可以减少编写程序出现错误 1 springboot加载配置文件的先后顺序如下: SpringApplication将从以下位置加载application.properties文件,并把它们添加到Spring Envi ......

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

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

springapplication将从以下位置加载application.properties文件,并把它们添加到spring environment中:

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

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

2 工程结构如图:

springboot之配置文件

代码如下:

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文件进行配置