Java教程

springboot如何在项目启动时初始化资源-ApplicationRunner

本文主要是介绍springboot如何在项目启动时初始化资源-ApplicationRunner,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

 实现ApplicationRunner,并重写run方法即可

@Component
public class InitPortJob implements ApplicationRunner {
 
    private final static Logger logger = LoggerFactory.getLogger(InitPortJob.class);
    //数量待调整
    public static BlockingQueue<Integer>  portQueue = new LinkedBlockingQueue<Integer>(10000);
    
    //@Value("${sip.begin.port}")
    private int beginPort=30000;
 
    //@Value("${sip.end.port}")
    private int endPort=40000;
    
    @Override
    public void run(ApplicationArguments args) throws Exception {
        logger.info("InitPort Begin MinPort:{};MaxPort:{};",beginPort,endPort);
        for (int tempPort = beginPort; tempPort < endPort; tempPort++) {
            if(tempPort%2 == 0) {
                portQueue.offer(tempPort);
            }
        }
    }
}

 

这篇关于springboot如何在项目启动时初始化资源-ApplicationRunner的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!