Skip to content

Instantly share code, notes, and snippets.

@piyusht007
Last active November 11, 2020 06:40
Show Gist options
  • Save piyusht007/4879e80f46eeb3ddbf13416a96037570 to your computer and use it in GitHub Desktop.
Save piyusht007/4879e80f46eeb3ddbf13416a96037570 to your computer and use it in GitHub Desktop.
Spring bean lifecycle - A Spring bean that tap into various lifecycle extension points
package org.piyush;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
public class MyApplicationTest {
@Test
public void contextLoads() {
}
}
package org.piyush;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
public class MySpringBean implements BeanNameAware,
ApplicationContextAware,
InitializingBean,
DisposableBean {
private String message;
public void sendMessage(String message) {
this.message = message;
}
public String getMessage() {
return this.message;
}
@Override
public void setBeanName(String name) {
// BeanNameAware#setBeanName
System.out.println("--- BeanNameAware#setBeanName executed ---");
}
@Override
public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException {
// ApplicationContextAware#setApplicationContext
System.out.println("--- ApplicationContextAware#setApplicationContext executed ---");
}
@PostConstruct
public void postConstruct() {
System.out.println("--- JSR-250 - @PostConstruct executed ---");
}
@Override
public void afterPropertiesSet() {
// IntializingBean#afterPropertiesSet
System.out.println("--- IntializingBean#afterPropertiesSet executed ---");
}
public void init() {
System.out.println("--- init-method executed ---");
}
@PreDestroy
public void preDestroy() {
System.out.println("--- JSR-250 - @PreDestroy executed ---");
}
@Override
public void destroy() throws Exception {
// DisposableBean#destroy
System.out.println("--- DisposableBean#destroy executed ---");
}
public void destroyMethod() {
System.out.println("--- destroy-method executed ---");
}
}
package org.piyush;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.stereotype.Component;
@Component
public class MySpringBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException {
System.out.println("--- MySpringBeanFactoryPostProcessor#postProcessBeanFactory ---");
final BeanDefinition mySpringBean = configurableListableBeanFactory.getBeanDefinition("mySpringBean");
System.out.println(mySpringBean.getFactoryBeanName()); // appConfig
System.out.println(mySpringBean.getInitMethodName()); // init
}
}
package org.piyush;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Component;
// BeanPostProcessor has to be a bean too, that is why it is annotated as @Component.
@Component
public class MySpringBeanPostProcessor implements BeanPostProcessor {
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName)
throws BeansException {
if (bean instanceof MySpringBean) {
System.out.println("--- postProcessBeforeInitialization executed ---");
}
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName)
throws BeansException {
if (bean instanceof MySpringBean) {
System.out.println("--- postProcessAfterInitialization executed ---");
}
return bean;
}
}
package org.piyush;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class AppConfig {
// We can alternatively annotate MySpringBean with @Component to create bean.
@Bean(initMethod = "init", destroyMethod = "destroy")
public MySpringBean mySpringBean(){
return new MySpringBean();
}
}
@piyusht007
Copy link
Author

piyusht007 commented Nov 11, 2020

The order of execution is as follows:

--- MySpringBeanFactoryPostProcessor#postProcessBeanFactory ---
appConfig
init
--- BeanNameAware#setBeanName executed ---
--- ApplicationContextAware#setApplicationContext executed ---
--- MySpringBeanPostProcessor#postProcessBeforeInitialization executed ---
--- JSR-250 - @PostConstruct executed ---
--- InitializingBean#afterPropertiesSet executed ---
--- Bean#init-method executed ---
--- MySpringBeanPostProcessor#postProcessAfterInitialization executed ---
--- JSR-250 - @PreDestroy executed ---
--- Bean#destroy executed ---

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment