springboot框架实现启动项目执行指定代码

说明:

当有写代码需要在项目启动时执行的时候(即项目启动完成前),可以使用这个方法。

步骤:

  1.  创建一个启动类并在类上打上@Component注解
  2. 让这个类实现CommandLineRunner接口
  3. 重写run()方法
  4. 在run()中调用执行的逻辑。

代码:

import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

@Component
public class RunTest implements CommandLineRunner {

    @Override
    public void run(String... args) throws Exception {
        System.err.println("hello beauty");
    }
}

代码截图示例:

猜你喜欢

转载自www.cnblogs.com/lu51211314/p/9564897.html