zoukankan      html  css  js  c++  java
  • 项目启动加载配置,以及IP黑名单,使用CommandLineRunner和ApplicationRunner来实现(一般用在网关进行拦截黑名单)

    //使用2个类的run方法都可以在项目启动时加载配置,唯一不同的是他们的参数不一样,CommandLineRunner的run方法参数是基本类型,ApplicationRunner的run方法参数是一个ApplicationArguments对象

    下面做个ip黑名单的demo:

    首先在application.yml配置ip黑名单,如下图

    之后在启动时,加载进入内存:

    package com.example.demo.config;
    
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.boot.CommandLineRunner;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.util.StringUtils;
    
    import java.util.HashSet;
    import java.util.Set;
    
    @Configuration
    public class Ipconfig implements CommandLineRunner{
        @Value("${ipBlack._set}")
        private String ipBlack;
    
        public static final Set<String> ipblackSet=new HashSet<>();
    
    
        public void run(String... strings) throws Exception {
            if(StringUtils.isEmpty(ipBlack)){
                System.out.println("没有黑名单");
            }
            String[] split = ipBlack.split("\
    ");
            for (String s : split) {
                ipblackSet.add(s.trim());
            }
            System.out.println(ipblackSet);
    
        }
    }
    //为了测试2个类,谁的run方法先执行,下面写了一个demo:
    package com.example.demo.config;
    
    import org.springframework.boot.ApplicationArguments;
    import org.springframework.boot.ApplicationRunner;
    import org.springframework.stereotype.Component;
    
    @Component
    public class IPConfig2 implements ApplicationRunner {
        @Override
        public void run(ApplicationArguments applicationArguments) throws Exception {
    
            System.out.println("是不是我先执行呢");
        }
    }
    
    注意:类上要加注解componet或者其他的,让spring管理,不然没效果
    结果如图:
    

     

  • 相关阅读:
    摄像头标定
    Call PDF to Vector Converter Command Line from C#, ASP, etc. web program languages
    Camera Calibration and 3D Reconstruction
    C++获取一个文件夹下的所有文件名(转)
    javascript学习之对象应用
    javascript中Array对象总结
    Joomla学习之模块
    关于ci中的表单提交问题
    phpcms之文件目录
    jQuery中的join方法
  • 原文地址:https://www.cnblogs.com/yangxiaohui227/p/11193313.html
Copyright © 2011-2022 走看看