@PostConstruct
public void postConstructDemo(){
// 执行相关业务
System.out.println("PostConstruct初始化数据");
}
2、实现InitializingBean接口
@Component
public class InitializingBeanDemo implements InitializingBean {
@Override
public void afterPropertiesSet() throws Exception {
// 执行相关业务
System.out.println("InitializingBeanDemo初始化数据");
}
}
3、使用@Bean注解的initMethod执行初始化方法
@Bean(initMethod = "init")
public InitMethodDemo initMethodTest(){
return new InitMethodDemo();
}
public class InitMethodDemo {
public void init(){
// 执行相关业务
System.out.println("InitMethodDemo初始化数据");
}
}
4、实现CommandLineRunner 接口
@Component
public class CommandLineRunnerDemo implements CommandLineRunner {
@Override
public void run(String... args) throws Exception {
// 执行相关业务
System.out.println("CommandLineRunnerDemo初始化数据");
}
}
5、实现ApplicationRunner 接口
@Component
public class ApplicationRunnerDemo implements ApplicationRunner {
@Override
public void run(ApplicationArguments args) throws Exception {
// 执行相关业务
System.out.println("ApplicationRunnerDemo初始化数据");
}
}
6、实现ApplicationListener接口
@Component
public class ApplicationListenerDemo implements ApplicationListener, Ordered {
@Override
public void onApplicationEvent(ApplicationStartedEvent applicationStartedEvent) {
// 执行相关业务
System.out.println("ApplicationListenerDemo初始化数据");
}
@Override
public int getOrder() {
return LOWEST_PRECEDENCE;
}
}
持续更新中...
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)