两种方法:
@PostConstruct
spring 初始化bean之后会调用。
@PostConstruct
public void setHashOperation() {
hashOperation = stringRedisTemplate.opsForHash();
}
实现ApplicationLIstener
spring 容器加载完成后调用。
@Component
public class ProcessStarter implements ApplicationListener<ContextRefreshedEvent> {
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
applicationContext = event.getApplicationContext();
startMainInNewThread();
}
}
实现ApplicationRunner
spring 容器加载完成后调用。
@Component
public class ProcessStarter implements ApplicationRunner {
@Override
public void run(ApplicationArguments args) throws Exception {
}
}