• 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

  • 相关阅读:
    Oracle EBS 技术顾问常用的PL/SQL工具
    ORACLE EBS 计划功能 理论整理
    Oracle SQL 空值排序(Nulls)
    Oracle数据库中表的四种连接方式讲解
    JVM学习笔记之CodeCache
    Java序列化的作用和反序列化
    Spring MVC framework深入分析之一总体分析
    SpringMVC 拦截器实现分析
    Spring MVC framework深入分析之三执行过程
    Ajax应用开发:实践者指南
  • 原文地址:https://www.cnblogs.com/LDDXFS/p/13526835.html
Copyright © 2020-2023  润新知