springboot-加载配置文件-多种类型

       在我们开发的很多项目当中,多多少少都会有一些特别的信息,这些信息不会写到数据库,而是配置在文件里面。这类数据在被应用初始化完成之后,会加载到程序中,我们可以在程序的各个地方引用,来进行与业务相关联的其他操作。
今天要说的就是如何加载这些信息,我们以springboot项目为例,分别加载三种结构数据:普通属性、对象、数组。

首先,创建一个普通的springboot项目

在该项目中创建了两个配置文件 (后缀名分别为:properties、yaml;只要名称为application,都会被加载),分别用三个类来装填;

f6155b83f70e131405e27e1bac4a2ee077f.jpg

一、普通属性加载

    1.配置文件:

a69696a508764b503f802986ec7c92d97f7.jpg

    2.java代码:

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;
    }
}

二、对象加载

    1.配置文件:

4f5676347166245c4e1b09a84b24a2a2902.jpg

    2.java代码:

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;
    }
}

三、数组加载

    1.配置文件:

cee6c22ec73326c4a1782ca8cf6c34bf444.jpg

    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;
    }
}

四、最后再创建一个启动类,来输出以上配置

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("---------------------------------------------");
        }
    }
}

输出结果:

faf2ab24448b9071fe1bacc9a7def974c5e.jpg

转载于:https://my.oschina.net/u/4122740/blog/3080605

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值