
正文
springboot框架实现启动项目执行指定代码
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
说明:
当有写代码需要在项目启动时执行的时候(即项目启动完成前),可以使用这个方法。
步骤:
- 创建一个启动类并在类上打上@Component注解
- 让这个类实现CommandLineRunner接口
- 重写run()方法
- 在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");
}
}
代码截图示例:









