springboot配置文件拉取数据

场景:由于表结构一直变更,无法从数据库拉取数据,现数据从配置文件里取数据

配置:application-pppoeUser.properties

#是个list
pppoeuser.pppoeUserTemple[0].vni= 400
pppoeuser.pppoeUserTemple[1].vni= 300

总配置文件 aplication.properties

#dev是springboot启动的配置文件
spring.profiles.active=dev,pppoeUser

设置启动加载PppoeUserConfigure.java

package com.cmcc.goc.device.config;

import com.cmcc.goc.device.common.dto.PppoeUserDTO;
import lombok.Getter;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.List;

/**
 * @author Nicole
 */
@Getter
@Setter
@Component
@ConfigurationProperties(prefix = "pppoeuser")
@Configuration
public class PppoeUserConfigure {

    public static List PPPOE_USER_TEMPLATE;


    private List pppoeUserTemple;

    /**
     * Init template configure.
     */
    @PostConstruct
    private void initTemplateConfigure() {
        PppoeUserConfigure.PPPOE_USER_TEMPLATE = pppoeUserTemple;
    }
}

代码进行调用

public void test(){
....
 List pppoeUserTemple = PppoeUserConfigure.PPPOE_USER_TEMPLATE;

 pppoeUserTemple.forEach(p -> {
            PppoeUserDTO pppoeUserDTO = new PppoeUserDTO();
            if (p.getVni() != null) {
                pppoeUserDTO.setVni(p.getVni());
            }
}
....
}

你可能感兴趣的:(springboot配置文件拉取数据)