springboot-加載配置文件-多種類型

       在咱們開發的不少項目當中,多多少少都會有一些特別的信息,這些信息不會寫到數據庫,而是配置在文件裏面。這類數據在被應用初始化完成以後,會加載到程序中,咱們能夠在程序的各個地方引用,來進行與業務相關聯的其餘操做。
今天要說的就是如何加載這些信息,咱們以springboot項目爲例,分別加載三種結構數據:普通屬性、對象、數組。java

首先,建立一個普通的springboot項目spring

在該項目中建立了兩個配置文件 (後綴名分別爲:properties、yaml;只要名稱爲application,都會被加載),分別用三個類來裝填;數據庫

1、普通屬性加載數組

    1.配置文件:springboot

    2.java代碼:app

package com.knowledge.knowledgeinboot.config.InitArgs;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * @Author chengxp
 * @Description 一個或多個分開的屬性
 */
@Component("singleProperty")
public class SingleProperty {

    private static String udpAddressIp;

    private static int udpAddressPort;

    /**
     * get 方法使用靜態,以便於獲取
     * @return
     */
    public static String getUdpAddressIp() {
        return udpAddressIp;
    }

    /**
     * 1.若將該註解加到靜態變量上,不會被加載;
     * 2.訪問修飾符修飾爲私有便可,spring在加載時,能找獲得;
     * @param udpAddressIp
     */
    @Value("${udpAddress.ip}")
    private void setUdpAddressIp(String udpAddressIp) {
        this.udpAddressIp = udpAddressIp;
    }

    /**
     * get 方法使用靜態,以便於獲取
     * @return
     */
    public static int getUdpAddressPort() {
        return udpAddressPort;
    }

    //若將該註解加到靜態變量上,不會被加載
    @Value("${udpAddress.port}")
    private void setUdpAddressPort(int udpAddressPort) {
        this.udpAddressPort = udpAddressPort;
    }
}

2、對象加載ide

    1.配置文件:this

    2.java代碼:code

package com.knowledge.knowledgeinboot.config.InitArgs;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

/**
 * @Author chengxp
 * @Description 加載配置文件中配置的對象數據
 */
@Configuration
@ConfigurationProperties("udp-address")
public class ObjProperty {

    private static String ip;
    private static int port;
    private static String desc;

    public void setIp(String ip) {
        this.ip = ip;
    }

    private void setPort(int port) {
        this.port = port;
    }

    private void setDesc(String desc) {
        this.desc = desc;
    }

    public static String getIp() {
        return ip;
    }

    public static int getPort() {
        return port;
    }

    public static String getDesc() {
        return desc;
    }
}

3、數組加載對象

    1.配置文件:

    2.java代碼:

package com.knowledge.knowledgeinboot.config.InitArgs;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

import java.util.List;

/**
 * @Author chengxp
 * @Description 數組屬性,加載在在yaml中的多個值.
 */
@Configuration
@ConfigurationProperties("device-arr")
public class ArrayProperty {

    private static List<Device> devices;

    public static List<Device> getDevices() {
        return devices;
    }

    /**
     * 這裏不能修飾爲 私有的,私有的沒法完成數據注入
     * @param devices
     */
    public void setDevices(List<Device> devices) {
        this.devices = devices;
    }

    @Data
    public static class Device {

        private String ip;
        private String port;
        private String type;
        private String position;
    }
}

4、最後再建立一個啓動類,來輸出以上配置

package com.knowledge.knowledgeinboot.listener;

import com.knowledge.knowledgeinboot.config.InitArgs.ArrayProperty;
import com.knowledge.knowledgeinboot.config.InitArgs.ObjProperty;
import com.knowledge.knowledgeinboot.config.InitArgs.SingleProperty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @Author chengxp
 * @Description
 */
@Slf4j
@Component
public class SpringInitListener implements ApplicationListener<ContextRefreshedEvent> {
    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {

        String udpIp = SingleProperty.getUdpAddressIp();
        int udpPort = SingleProperty.getUdpAddressPort();

        //單個屬性輸出
        log.info("SingleProperty:↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓");
        log.info("init args udpIp: " + udpIp);
        log.info("init args udpPort: " + udpPort);
        log.info("---------------------------------------------");

        //對象輸出
        log.info("ObjProperty:↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓");
        log.info("init args udpIp: " + ObjProperty.getIp());
        log.info("init args udpPort: " + ObjProperty.getPort());
        log.info("init args desc: " + ObjProperty.getDesc());
        log.info("---------------------------------------------");

        //數組輸出
        log.info("Array:↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓");
        List<ArrayProperty.Device> objPropertys = ArrayProperty.getDevices();
        for(int i = 0;i < objPropertys.size(); i++){

            log.info("objPropertys index: " + i);
            log.info("init args ip: " + objPropertys.get(i).getIp());
            log.info("init args port: " + objPropertys.get(i).getPort());
            log.info("init args type: " + objPropertys.get(i).getType());
            log.info("init args position: " + objPropertys.get(i).getPosition());
            log.info("---------------------------------------------");
        }
    }
}

輸出結果:

相關文章
相關標籤/搜索