美文网首页springboot
springboot启动并执行特定方法

springboot启动并执行特定方法

作者: 大韭哥 | 来源:发表于2019-01-04 18:29 被阅读0次

    版本:springboot2.1.1

    1.类上添加@Component注解,要执行的方法上添加@PostConstruct

    @Slf4j
    @Component
    public class AutoStart {
        @PostConstruct
        public void init() {
            log.info("启动.................");
        }
    }
    

    2.类上添加@Component注解,并实现CommandLineRunner接口重写run方法;run方法即在程序启动后执行

    @Slf4j
    @Component
    public class AutoStart implements CommandLineRunner {
        @Autowired
        private LoadInfo loadInfo;
        @Override
        public void run(String... args) throws Exception {
            log.info("启动.................");
            loadInfo.init();
        }
    }
    
    

    相关文章

      网友评论

        本文标题:springboot启动并执行特定方法

        本文链接:https://www.haomeiwen.com/subject/pwmkrqtx.html