1, the preface
When it comes to the life cycle, you are certainly familiar with it, and this is something that will be mentioned everywhere. When I first learned Java, I talked about the life cycle of various variables (local variables, member variables, etc.), and then I learned servlet about its life cycle, and now in Spring, the objects it creates also have a life cycle.
Learning about the life cycle is so that we can better understand the process of an object from creation to destruction. Since we didn’t create these things, we have an obligation to know how they work so that we can use them better.
2. Spring Bean lifecycle
The Spring Bean lifecycle is as follows:
- The first step is to create the container according to our requirements.
ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");
Copy the code
- Then instantiate the object. That is, getBean goes to the singleton pool to see if the object has been created, and creates it if it has not.
- Once the object is instantiated, it is an empty shell that needs to be assigned to properties.
- The initialization process is complete
This is a sequential process. If an interface is implemented, the steps are executed in the following order.
- Perform AOP operations
- The object then becomes a bean
We know that beans and objects are not the same. Beans are objects that Spring creates for us. Not all objects can be called beans.
3. Code demonstration
Start by creating a MyTest class. The interface methods are implemented one by one as shown above.
public class MyTest implements BeanNameAware.BeanFactoryAware.ApplicationContextAware.InitializingBean {
@Autowired
public Student student;
public void setBeanName(String s) {
//setBeanName(s);
System.out.println("Execute the setBeanName method of the BeanNameAware interface");
}
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
System.out.println("Implement the setBeanFactory method of the BeanFactoryAware interface");
}
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
System.out.println("Execute the setApplicationContext method of the ApplicationContextAware interface");
}
public void init(a){
System.out.println("Execute init method of MyTest class");
}
public void destroy(a){
System.out.println("Execute MyTest class destroy destruction method");
}
public void afterPropertiesSet(a) throws Exception {
System.out.println("Execute afterPropertiesSet method of InitializingBean interface"); }}Copy the code
There are also pre-initialization and post-initialization methods that need to implement the BeanPostProcessor class.
public class BeanPostProcessorImpl implements BeanPostProcessor {
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
System.out.println("Perform the BeanPostProcessor interface postProcessBeforeInitialization method");
return bean;
}
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
System.out.println("Perform the BeanPostProcessor interface postProcessAfterInitialization method");
returnbean; }}Copy the code
Finally, we introduced these classes in the XML configuration, because the Student class didn’t write anything, but created a class that we don’t write here.
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="myTest" class="com.xuangong.pojo.MyTest" init-method="init" destroy-method="destroy">
</bean>
<bean id="student" class="com.xuangong.pojo.Student">
</bean>
<bean id="beanPostProcessor" class="com.xuangong.pojo.BeanPostProcessorImpl">
</bean>
</beans>
Copy the code
The test.
public class test {
@Test
public void test01(a){
ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");
MyTest myTest = (MyTest) ac.getBean("myTest"); System.out.println(myTest); }}Copy the code
The results.
The setBeanName method of the BeanNameAware interface executes the setBeanFactory method of the BeanFactoryAware interface Perform ApplicationContextAware interface method of setApplicationContext BeanPostProcessor interface postProcessBeforeInitialization method MyTest as an executive director InitializingBean interface afterPropertiesSet method init method performs the BeanPostProcessor interface postProcessAfterInitialization method Perform the BeanPostProcessor interface postProcessBeforeInitialization method performs BeanPostProcessor interface postProcessAfterInitialization method com.xuangong.pojo.MyTest@6b9651f3Copy the code