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

  • 相关阅读:
    JavaScript 继承机制设计思想
    JavaScript里的原型(prototype), 原型链,constructor属性,继承
    centos7用yum安装node.js v8.x
    sequelize 字段无法操作
    开发CLI命令行
    微信分组群发图文40152,微信分组群发图文invalid group id hint
    微信分组群发45028,微信分组群发has no masssend quota hint
    微信45028错误,微信has no masssend quota hint错误
    微信上传图文消息素材40007,invalid media_id hint
    Spring文件上传出错:java.lang.ClassCastException: org.apache.catalina.connector.Request
  • 原文地址:https://www.cnblogs.com/LDDXFS/p/13526835.html
Copyright © 2011-2022 走看看