springBoot 獲取配置文件裏的值

通過spring容器獲取配置文件中的參數

package com.boot.config.bootconfig;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.ConfigurableEnvironment;
@SpringBootApplication
public class BootConfigApplication {
    public static void main(String[] args) {
        //通過spring容器獲取環境對象
        ConfigurableApplicationContext context = SpringApplication.run(BootConfigApplication.class, args);
        ConfigurableEnvironment environment = context.getEnvironment();
        String property = environment.getProperty("local.ip.addr");
        System.out.println(environment.getProperty("user.dir"));
        System.out.println(environment.getProperty("local.ip.root"));
        System.out.println(property);
    }
}

在這裏插入圖片描述

通過@Value方法讀取配置文件中的參數

springBoot默認是讀取resources下文件名爲application.properties或者application.yml的文件,那如何讀取文件名不是application的配置文件呢?方法如下:

在運行的時候指定參數的方式

在jvm裏配置需要使用-D
在這裏插入圖片描述
在這裏插入圖片描述

通過@ConfigurationProperties註解讀取配置文件中的值,一般都是使用這種方式

在這裏插入圖片描述

application.properties 文件內容:

local.ip.addr = 192.168.2.110
local.ip.root= root
greey.datasource.driverClassName=com.mysql.jdbc.Driver
greey.datasource.url=jdbc:mysql://123
greey.datasource.username=root
greey.datasource.password=torey123

裝載的實體類:DataSourceProperties.java

package com.boot.config.bootconfig;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

/**
 * @ClassName:DataSourceProperties
 * @Description:
 * @author: Torey
 */
@Component
@ConfigurationProperties(prefix = "greey.datasource")
public class DataSourceProperties {
    //一般情況下都是用這種方法讀取的
    private String driverClassName;
    private String url;
    private String username;
    private String password;

    @Override
    public String toString() {
        return "DataSourceProperties{" +
                "driverClassName='" + driverClassName + '\'' +
                ", url='" + url + '\'' +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                '}';
    }

    public String getDriverClassName() {
        return driverClassName;
    }

    public void setDriverClassName(String driverClassName) {
        this.driverClassName = driverClassName;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    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.boot.config.bootconfig;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.ConfigurableEnvironment;

@SpringBootApplication
public class BootConfigApplication {
    public static void main(String[] args) {
        //通過spring容器獲取環境對象
        ConfigurableApplicationContext context = SpringApplication.run(BootConfigApplication.class, args);
        ConfigurableEnvironment environment = context.getEnvironment();
        //通過ConfigurationProperties方式讀取
        System.out.println("===通過ConfigurationProperties方式讀取===");
        System.out.println(context.getBean(DataSourceProperties.class));
        context.close();
    }
}

在運行時,讀取多個文件

–spring.config.location=classpath:conf/app.yml,classpath:conf/app1.properties 或者用
-Dspring.config.location=classpath:conf/app.yml,classpath:conf/app1.properties
在這裏插入圖片描述
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章