Last active
November 11, 2020 06:40
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.piyush; | |
import org.junit.jupiter.api.Test; | |
import org.springframework.boot.test.context.SpringBootTest; | |
@SpringBootTest | |
public class MyApplicationTest { | |
@Test | |
public void contextLoads() { | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ---"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 ---