有时需要在项目启动时加载一部分数据,这时就要有一部分代码在项目启动时运行。
Springboot项目实现启动时运行代码的方式有四种。
1. PostConstruct注解
1 2 3 4 5 6 7 8 9 10 11 12 13
| import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
@Component public class Init1 {
@PostConstruct public void init() { System.out.println("init1"); }
}
|
2. 实现InitializingBean接口
1 2 3 4 5 6 7 8 9 10
| import org.springframework.beans.factory.InitializingBean; import org.springframework.stereotype.Component;
@Component public class Init2 implements InitializingBean { @Override public void afterPropertiesSet() throws Exception { System.out.println("init2"); } }
|
3. 实现CommandLineRunner接口
可以通过在类型加@Order
注解来控制执行顺序。
1 2 3 4 5 6 7 8 9 10
| import org.springframework.boot.CommandLineRunner; import org.springframework.stereotype.Component;
@Component public class Init3 implements CommandLineRunner { @Override public void run(String... args) throws Exception { System.out.println("init3"); } }
|
4. 放在SpringBoot启动类中
1 2 3 4 5 6 7 8 9
| @SpringBootApplication public class PandaBaseApplication {
public static void main(String[] args) { SpringApplication.run(PandaBaseApplication.class, args); System.out.println("init4"); }
}
|