An execution flowchart for a Bean
Spring beans in the complete life cycle, in learning the Spring framework, it is very important to understand the Spring Bean management, the following is the whole process of Spring Bean construction:
The life cycle of the Bean
- Spring starts, finds and loads beans that need to be managed by Spring, and instantiates the beans
- After the Bean is instantiated, the Bean properties are injected into values
- If the Bean implements the BeanNameAware interface, Spring calls the setBeanName() method, passing in the Bean id
- If the Bean implements the BeanFactoryAware interface, Spring calls the setBeanFactory() method to pass in the BeanFactory container instance
- If the Bean implements the ApplicationContextAware interface, Spring calls the Bean’s setApplicationContext() method, passing in a reference to the Bean’s application context.
- If the Bean implements the BeanPostProcessor interface, the Spring would call their BeanPostProcessor. PostProcessBeforeInitialization () method
- If the Bean method annotates @postconstruct, that method is executed
- If the Bean implementation InitializingBean interface, the Spring would call their InitializingBean. AfterPropertiesSet () method. Also, if the bean declares an initialization method with init-method, that method will be called
- If the Bean implements the BeanPostProcessor interface, the Spring would call their BeanPostProcessor. PostProcessAfterInitialization () () method. At this point, the Bean is ready to be created. They will remain in the application context until the application context is destroyed.
- If the method in the Bean is annotated with @predestroy, the method is executed
- If the bean implements the DisposableBean interface, Spring will call its destory() interface method, which will also be called if the bean uses destory-method to declare the destruction method.
The code instance validates the Bean’s life cycle
User class, implement BeanNameAware, BeanFactoryAware, ApplicationContextAware, InitializingBean, DisposableBean interface
public class User implements BeanNameAware.BeanFactoryAware.ApplicationContextAware.InitializingBean.DisposableBean {
private String userName;
public User(a) {
System.out.println("User is instantiated !!!!");
}
public String getUserName(a) {
return userName;
}
public void setUserName(String userName) {
System.out.println("Alfred talk about programming!!");
this.userName = userName;
}
public void init(a) {
System.out.println("Execute initMethod method!!");
}
@PostConstruct
public void postContruct(a) {
System.out.println("Execute @postconstruct method");
}
public void destory1(a) {
System.out.println("Execute destroyMethod method !!!!");
}
@PreDestroy
public void preDestroy(a) {
System.out.println("Execute @predestroy method !!!!");
}
public void afterPropertiesSet(a) throws Exception {
System.out.println("Executive InitializingBean. AfterPropertiesSet ()");
}
public void destroy(a) throws Exception {
System.out.println("DisposableBean.destroy()");
}
public void setBeanName(String name) {
System.out.println("BeanNameAware.setBeanName()" + "--->BeanName:" + name);
}
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
System.out.println("BeanFactoryAware.setBeanFactory()");
}
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
System.out.println("ApplicationContextAware.setApplicationContext()" );
}
}
Copy the code
Define the rear processor MyBeanPostProcessor, realize postProcessBeforeInitialization, postProcessAfterInitialization method
@Component
public class MyBeanPostProcessor implements BeanPostProcessor {
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
System.out.println("MyBeanPostProcessor.postProcessBeforeInitialization()--before->"+"Initialize beanName:"+beanName);
return bean;
}
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
System.out.println("MyBeanPostProcessor.postProcessAfterInitialization()--after-->"+"Initialize beanName:"+beanName);
return bean;
}
}
Copy the code
Define MyConfigUser and register User,MyBeanPostProcessor, to the Spring container. Refer to the SpringIOC summary for Bean registration
@ComponentScan(basePackages = "zfcoding.beanpostprocessor")
@Configuration
public class MyConfigUser {
@Bean(initMethod = "init",destroyMethod = "destory1")
public User user(a){
return new User();
}
}
Copy the code
Unit testing
public class BeanProcessTest {
@Test
public void test(a){
AnnotationConfigApplicationContext applicationContext=new AnnotationConfigApplicationContext(MyConfigUser.class);
User user = applicationContext.getBean(User.class);
applicationContext.close();
}
}
Copy the code
Running results:
User is instantiated !!!!
BeanNameAware.setBeanName()--->BeanName:user
BeanFactoryAware.setBeanFactory()
ApplicationContextAware.setApplicationContext()
MyBeanPostProcessor. PostProcessBeforeInitialization () - before - > initialize beanName: user
perform@PostConstructmethods
Perform InitializingBean. AfterPropertiesSet ()
Execute initMethod!!
perform@PreDestroyMethods !!!!
DisposableBean.destroy()
Execute the destroyMethod method !!!!
Copy the code
Reference: https://www.cnblogs.com/javazhiyin/p/10905294.html
That’s the whole process of creating a Spring Bean.
I am a fu, the public number “a fu chat programming” author, back-end technology to keep learning enthusiasts, I will often update JAVA technology articles, on the way to progress, mutual encouragement!
Welcome to pay attention to my public number, background reply 666, receive a full set of Java advanced actual combat courses.