zoukankan      html  css  js  c++  java
  • springboot启动时执行特定方法

    springboot启动时执行特定方法

    1 spring事件机制

    package lddxfs.springboot.study.runstarted.component;
    
    import lombok.extern.slf4j.Slf4j;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.context.event.ApplicationReadyEvent;
    import org.springframework.context.event.EventListener;
    import org.springframework.data.redis.core.StringRedisTemplate;
    import org.springframework.stereotype.Component;
    
    import java.time.LocalDateTime;
    import java.time.format.DateTimeFormatter;
    
    @Slf4j
    @Component
    public class ExampleApplicationListener {
        @Autowired
        private StringRedisTemplate stringRedisTemplate;
    
    
        /**
         * spring的事件
         * https://blog.csdn.net/chengqiuming/article/details/83349486
         */
        @EventListener(classes={ApplicationReadyEvent.class})
        public void event(ApplicationReadyEvent event) {
            log.info("ApplicationListener:{}", event);
            LocalDateTime localDateTime = LocalDateTime.now();
            stringRedisTemplate.opsForList().leftPush("key:ApplicationListener", localDateTime.format(DateTimeFormatter.ISO_DATE_TIME));
    
        }
    }
    View Code

    2 springboot 提供的ApplicationRunner

    package lddxfs.springboot.study.runstarted.component;
    
    import lombok.extern.slf4j.Slf4j;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.ApplicationArguments;
    import org.springframework.boot.ApplicationRunner;
    import org.springframework.data.redis.core.StringRedisTemplate;
    import org.springframework.stereotype.Component;
    
    import java.time.LocalDateTime;
    import java.time.format.DateTimeFormatter;
    @Slf4j
    @Component
    public class ExampleApplicationRunner implements ApplicationRunner {
        @Autowired
        private StringRedisTemplate stringRedisTemplate;
    
        @Override
        public void run(ApplicationArguments args) throws Exception {
            log.info("ApplicationRunner");
            LocalDateTime localDateTime = LocalDateTime.now();
            stringRedisTemplate.opsForList().leftPush("key:ApplicationRunner", localDateTime.format(DateTimeFormatter.ISO_DATE_TIME));
        }
    }
    View Code

    3 springboot 提供的CommandLineRunner

    package lddxfs.springboot.study.runstarted.component;
    
    import lombok.extern.slf4j.Slf4j;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.CommandLineRunner;
    import org.springframework.data.redis.core.StringRedisTemplate;
    import org.springframework.stereotype.Component;
    
    import java.time.LocalDateTime;
    import java.time.format.DateTimeFormatter;
    
    @Slf4j
    @Component
    public class ExampleCommandLineRunner implements CommandLineRunner {
    
        @Autowired
        private StringRedisTemplate stringRedisTemplate;
    
        @Override
        public void run(String... args) throws Exception {
            log.info("CommandLineRunner");
            LocalDateTime localDateTime = LocalDateTime.now();
            stringRedisTemplate.opsForList().leftPush("key:CommandLineRunner", localDateTime.format(DateTimeFormatter.ISO_DATE_TIME));
    
        }
    }
    View Code

    4 @PostConstruct方式

    package lddxfs.springboot.study.runstarted.component;
    
    import lombok.extern.slf4j.Slf4j;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.data.redis.core.StringRedisTemplate;
    import org.springframework.stereotype.Component;
    
    import javax.annotation.PostConstruct;
    import java.time.LocalDateTime;
    import java.time.format.DateTimeFormatter;
    
    @Slf4j
    @Component
    public class ExamplePostConstruct {
        @Autowired
        private StringRedisTemplate stringRedisTemplate;
    
        @PostConstruct
        public void postConstruct() {
            log.info("  @PostConstruct");
            LocalDateTime localDateTime = LocalDateTime.now();
            stringRedisTemplate.opsForList().leftPush("key:PostConstruct", localDateTime.format(DateTimeFormatter.ISO_DATE_TIME));
    
        }
    }
    View Code

  • 相关阅读:
    概率论02 概率公理-集合
    概率论1 计数-排列-组合
    matplotlib 练习
    python itertools模块练习
    主观世界的破碎与重建——湖畔大学的失败课外课
    Python操作MongoDB(PyMongo模块的使用)
    python操作json数据格式--基础
    linux shell awk实现实时监控网卡流量脚本
    Python 的十个自然语言处理工具
    实现优先级队列 --heapq模块
  • 原文地址:https://www.cnblogs.com/LDDXFS/p/13526835.html
Copyright © 2011-2022 走看看