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)); } }
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)); } }
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)); } }
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)); } }