Skip to content

Instantly share code, notes, and snippets.

@ShakalakaB
Last active July 18, 2022 10:20
Show Gist options
  • Save ShakalakaB/706d6e4b304536c80bf25dd652b5202c to your computer and use it in GitHub Desktop.
Save ShakalakaB/706d6e4b304536c80bf25dd652b5202c to your computer and use it in GitHub Desktop.
bean lifecycle-1.java
@Component
public class LifeCycleDemoBean implements InitializingBean, DisposableBean, BeanNameAware,
BeanFactoryAware, ApplicationContextAware {
public LifeCycleDemoBean() {
System.out.println("## 1. I'm in the LifeCycleBean Constructor");
}
/**
* {@link BeanNameAware}
*/
@Override
public void setBeanName(String name) {
System.out.println("## 2. My Bean Name is: " + name);
}
/**
* {@link BeanFactoryAware}
*/
@Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
System.out.println("## 3. Bean Factory has been set");
}
/**
* {@link ApplicationContextAware}
*/
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
System.out.println("## 4. Application context has been set");
}
/**
* {@link LifeCycleDemoBeanPostProcessor}
*/
public void beforeInit(){
System.out.println("## 5. Before Init - Called by BeanPostProcessor: LifeCycleDemoBeanPostProcessor");
}
@PostConstruct
public void postConstruct(){
System.out.println("## 6. The Post Construct annotated method has been called");
}
/**
* {@link InitializingBean}
*/
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("## 7. The LifeCycleBean has its properties set!");
}
/**
* {@link LifeCycleDemoBeanPostProcessor}
*/
public void afterInit(){
System.out.println("## 8. After init called by Bean Post Processor");
}
@PreDestroy
public void preDestroy() {
System.out.println("## 9. The PreDestroy annotated method has been called");
}
/**
* {@link DisposableBean}
*/
@Override
public void destroy() throws Exception {
System.out.println("## 10. The Lifecycle bean has been terminated");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment