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

详解Spring Boot读取配置文件与配置文件优先级

程序员文章站 2024-02-25 09:11:04
spring boot读取配置文件 1)通过注入applicationcontext 或者 environment对象来读取配置文件里的配置信息。 packag...

spring boot读取配置文件

1)通过注入applicationcontext 或者 environment对象来读取配置文件里的配置信息。

package com.ivan.config.controller;

import org.springframework.beans.factory.annotation.autowired;

import org.springframework.context.applicationcontext;

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 configcontroller {
  @autowired

  applicationcontext context;

  @autowired

  environment environment;

  @requestmapping(value="/config", method={requestmethod.get})

  public string getconfigcontent(){       

    string name = context.getenvironment().getproperty("db.user.name");

    return name;

  }

  @requestmapping(value="/configenv", method={requestmethod.get})

  public string getconfigenvironment(){

    string name = environment.getproperty("db.user.name");

    return name;

  }
}

2)通过@configurationproperties配合@propertysource读取配置文件里的配置信息。

1:通过@propertysource指定当前类里属性的配置文件地址,configurationproperties可以指定配置的前缀,@configuration用于定义一个配置类:

package com.ivan.config.entity;
import org.springframework.boot.context.properties.configurationproperties;

import org.springframework.context.annotation.configuration;

import org.springframework.context.annotation.propertysource;

@configuration

@propertysource("classpath:config/druid.properties")

@configurationproperties(prefix = "druid")

public class druidconfig {

  private int  initialsize;

  private int  minidle;

  private int  maxactive;

  private int  maxwait;

  private string validationquery;

  private boolean testwhileidle;

  private boolean testonborrow;

  private boolean testonreturn;

  public int getinitialsize() {

    return initialsize;

  }

  public void setinitialsize(int initialsize) {

    this.initialsize = initialsize;

  }

  public int getminidle() {

    return minidle;

  }

  public void setminidle(int minidle) {

    this.minidle = minidle;

  }

  public int getmaxactive() {

    return maxactive;

  }

  public void setmaxactive(int maxactive) {

    this.maxactive = maxactive;

  }

  public int getmaxwait() {

    return maxwait;

  }

  public void setmaxwait(int maxwait) {

    this.maxwait = maxwait;

  }

  public string getvalidationquery() {

    return validationquery;

  }

  public void setvalidationquery(string validationquery) {

    this.validationquery = validationquery;

  }

  public boolean istestwhileidle() {

    return testwhileidle;

  }

  public void settestwhileidle(boolean testwhileidle) {

    this.testwhileidle = testwhileidle;

  }

  public boolean istestonborrow() {

    return testonborrow;

  }

  public void settestonborrow(boolean testonborrow) {

    this.testonborrow = testonborrow;

  }

  public boolean istestonreturn() {

    return testonreturn;

  }

  public void settestonreturn(boolean testonreturn) {

    this.testonreturn = testonreturn;

  }

  @override

  public string tostring() {

    return "druidconfig [initialsize=" + initialsize + ", minidle=" + minidle + ", maxactive=" + maxactive + ", maxwait=" + maxwait + ", validationquery=" + validationquery + ", testwhileidle=" + testwhileidle + ", testonborrow=" + testonborrow + ", testonreturn=" + testonreturn + "]";

  }
}

2:对应的配置文件:

druid.initialsize=5
druid.minidle=5
druid.maxactive=20
druid.maxwait=60000
druid.validationquery=select 'x'
druid.testwhileidle=true 
druid.testonborrow=true 
druid.testonreturn=true

3:在需要用到的类通过@autowired注入

package com.ivan.config.controller;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.web.bind.annotation.requestmapping;
import org.springframework.web.bind.annotation.requestmethod;
import org.springframework.web.bind.annotation.restcontroller;
import com.ivan.config.entity.druidconfig;
@restcontroller
public class druidconfigcontroller {
  @autowired
  public druidconfig druidconfig;

  @requestmapping(value="/druidconfig", method={requestmethod.get})
  public string getdruidconfig(){
    return druidconfig.tostring();
  }
}

3)通过@value注解

1:需要得到配置属性的类如下,可以在任何需要得到配置的地方用@value注解

package com.ivan.config.entity;
import org.springframework.beans.factory.annotation.value;
import org.springframework.context.annotation.configuration;
@configuration

public class valuetest {
  @value("${db.user.name}")
  private string username;
  public string getusername() {
    return username;
  }
  public void setusername(string username) {
    this.username = username;
  }
}

2:测试controller类通过@autowired注入实体类

package com.ivan.config.controller;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.web.bind.annotation.requestmapping;
import org.springframework.web.bind.annotation.requestmethod;
import org.springframework.web.bind.annotation.restcontroller;
import com.ivan.config.entity.valuetest;
@restcontroller
public class valuecontroller {
  @autowired
  private valuetest value;

  @requestmapping(value="/configvalue", method={requestmethod.get})
  public string getconfig(){
    return value.getusername();
  }
}

spring boot 配置文件优先级:

1:命令行参数。(以--开头的参数,比如可以设置:--server.port对同一套代码设置不同的参数)
2: 通过 system.getproperties() 获取的 java 系统参数。
3:操作系统环境变量(这解释了为什么你通过application.properties设置的user.name取的是系统的用户名了)
4:从 java:comp/env 得到的 jndi 属性。
5: 应用 jar 文件之外的属性文件(系统的application.properties文件)
6:应用 jar 文件内部的属性文件。
7: 在应用配置 java 类(包含“@configuration”注解的 java 类)中通过“@propertysource”注解声明的属性文件。
8: 通过“springapplication.setdefaultproperties”声明的默认属性。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。