I am learning the initialization and destruction callbacks for a bean managed by ApplicationContext in a springboot application. I have a bean which implements InitializingBeans and DisposableBeans interfaces.I do have a @PostConstruct which gets invoked. However I am not seeing the init method invoked when i remove the implementation.What am i missing?
@Component
public class LifeCycleBean implements InitializingBean, DisposableBeans{
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public LifeCycleBean() {
// TODO Auto-generated constructor stub
System.out.println("Learning lifecycle - COnstructor invoked"+name);
}
@Override
public void destroy() throws Exception {
System.out.println("Learning lifecycle - Calling Destroy Method");
}
@Override
public void afterPropertiesSet() throws Exception {
// TODO Auto-generated method stub
System.out.println("Learning lifecycle - afterPropertiesSet
invoked"+name);
}
//This never got executed
public void init() {
System.out.println("Learning lifecycle - initMethod invoked"+name);
}
@PostConstruct
public void postConstructMethod() {
System.out.println("Learning lifecycle - postConstructMethod
invoked"+name);
}
@PreDestroy
public void preDestroyMethod() {
System.out.println("Learning lifecycle - preDestroyMethod invoked"+name);
}
}
SpringBootApplication
@SpringBootApplication
public class LifeCycleApplication {
public static void main(String[] args) {
ApplicationContext ctx = SpringApplication.run(LifeCycleApplication.class, args);
System.out.println("going to get bean definition names");
ctx.getBeanDefinitionNames();
LifeCycleBean bean = ctx.getBean(LifeCycleBean.class);
System.out.println("before setting name");
bean.setName("bean");
System.out.println("after setting name");
}
}
How and when do i see the init method invoked in springboot application?