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

  • 相关阅读:
    【题解】洛谷P5048 Yuno loves sqrt technology III
    【题解】Codeforces1545D AquaMoon and Wrong Coordinate
    Linux压缩解压命令汇总
    Linux设置DNS地址及清理DNS缓存方法
    Linux设置系统运行模式
    JDBC 1.0
    iBATIS 历史三个版本小细节对比
    Oracle import/Export 命令
    初创互联网团队如何利用开源技术和云服务快速建立网站
    oracle Merge 函数
  • 原文地址:https://www.cnblogs.com/LDDXFS/p/13526835.html
Copyright © 2011-2022 走看看