springboot笔记

1. springboot 启动之后执行方法,需要对库操作(方法挺多)

踩坑: 使用 CommandLineRunner 实现的时候, 打jar包的时候会测试jar包,首先会运行 实现CommandLineRunner 的方法, 当时这里面使用线程做的,导致打jar包一直出不来结果. 可以把 实现该类的方法 和 tomcat 看作一个整体

实现 CommandLineRunner
实现 ServletContextListener() 监听器, 监听 tomcat 开启和关闭
public class TomcatListener implements ServletContextListener {

    @Override
    public void contextInitialized(ServletContextEvent sce) {
        System.out.println("服务启动");
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        System.out.println("服务结束");
    }
}
@Component
public class Text implements CommandLineRunner {

    @Override
    public void run(String... args) {
           
    }
}

 2. springboot 开启定时任务需要

@EnableScheduling
@SpringBootApplication
@EnableScheduling
@MapperScan("com.example.hostinfo.dao")
public class HostinfoApplication {

    public static void main(String[] args) {
        SpringApplication.run(HostinfoApplication.class, args);
    }

}

  

 
原文地址:https://www.cnblogs.com/han-guang-xue/p/13749496.html