在我们开发的很多项目当中,多多少少都会有一些特别的信息,这些信息不会写到数据库,而是配置在文件里面。这类数据在被应用初始化完成之后,会加载到程序中,我们可以在程序的各个地方引用,来进行与业务相关联的其他操作。
今天要说的就是如何加载这些信息,我们以springboot项目为例,分别加载三种结构数据:普通属性、对象、数组。
首先,创建一个普通的springboot项目
在该项目中创建了两个配置文件 (后缀名分别为:properties、yaml;只要名称为application,都会被加载),分别用三个类来装填;
一、普通属性加载
1.配置文件:
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.配置文件:
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.配置文件:
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("---------------------------------------------");
}
}
}
输出结果: