@PostConstruct注解

@PostConstruct该注解被用来修饰一个非静态的void()方法。被@PostConstruct修饰的方法会在服务器加载Servlet的时候运行,并且只会被服务器执行一次。

  1. @Component
  2. public class Test1 {
  3. @PostConstruct
  4. public void init(){
  5. //...
  6. }
  7. }

实现ApplicationRunner接口

在springboot容器启动完成时调用

  1. @Component
  2. public class ApplicationRunnerImpl implements ApplicationRunner {
  3. @Override
  4. public void run(ApplicationArguments args) throws Exception {
  5. System.out.println("实现ApplicationRunner接口");
  6. }
  7. }