Ask a question
Instantiation of beans in Spring is an important part of the bean life cycle. Usually, the bean will not change after initialization.
So what is the process of Spring instance bean? !
Spring instantiation bean process analysis
To get a bean object, you must first obtain it through the getBean() method of BeanFactory. During this period, a series of steps will be passed to instantiate the bean object:
Step 1: Call the default constructor of the bean (of course, it can also be other constructors specified) to generate a bean instance: bean1.
Step 2: Check whether the attribute value of the bean is injected into the bean configuration file. If there is injection, inject its attributes based on the bean1 instance, overwrite the original bean1 to form a new bean instance: bean2.
Step 3: Check whether the bean implements the InitializingBean interface. If this interface is implemented, call the afterPropertiesSet() method to perform corresponding operations on bean2, and overwrite bean2 to form a new bean instance: bean3.
Step 4: Check whether the init-method property is specified in the bean configuration file. If it is specified, call the corresponding method of this property and perform corresponding operations on bean3, and finally overwrite bean3 to form a new instance: bean4.
Through the above steps, we found that when Spring instances a bean, the bean is constantly changing!
Spring instantiation bean process code demonstration
To better illustrate the above steps, please see the following code:
Entity Class:
/** * Entity class*/public class Employee implements InitializingBean, DisposableBean, BeanNameAware {private String id;// employee number private String name;// employee name private String sex;// employee gender private String age;// employee age private String nativePlace;// employee place private String department;// employee department private String beanName;// bean name public Employee() {System.out.println("************Step 1: Call the default constructor of the bean*************"); this.id = "bean1:G080405214";System.out.println("bean1's value: " + this);System.out.println("*********Step 2: Check whether the Bean's property value is injected into the bean configuration file*******************");}public void afterPropertiesSet() throws Exception {System.out.println("bean2's value: " + this);System.out.println("************ Step 3: Check whether the bean implements the InitializingBean interface*************");this.name = "bean3:Li Xiaohong";this.sex = "bean3:female";this.age = "bean3:25";System.out.println("bean3's value: " + this);}public void init() {System.out.println("************ Step 4: Check whether the init-method is specified in the bean configuration file****************");this.nativePlace = "bean3:Beijing";System.out.println("bean4's value: " + this);}public void destroy() throws Exception {System.out.println("************Service Stop*************");}public void setBeanName(String arg0) {System.out.println("********* Set the name of the bean****************");this.beanName = "myBeanName";}public String getId() {return id;}public void setId(String id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getSex() {return sex;}public void setSex(String sex) {this.sex = sex;}public String getAge() {return age;}public void setAge(String age) {this.age = age;}public String getNativePlace() {return nativePlace;}public void setNativePlace(String nativePlace) {this.nativePlace = nativePlace;}public String getDepartment() {return department;}public void setDepartment(String department) {this.department = department;}public String getBeanName() {return beanName;}@Override public String toString() {return "Employee [id=" + id + ", name=" + name + ", sex=" + sex + ", age=" + age + ", nativePlace=" + nativePlace + ", department=" + department + ", beanName=" + beanName + "]";}}Tools:
/** * Bean context tool class */public class BeanContextHelper {private static ApplicationContext _instance;static {if (_instance == null) _instance = buildApplicationContext();}private BeanContextHelper() {}/** * Rebuild the ApplicationContext object*/public static ApplicationContext buildApplicationContext() {return new ClassPathXmlApplicationContext("applicationContext-base.xml");}/** * Get an ApplicationContext object*/public static ApplicationContext getApplicationContext() {return _instance;}}Spring's Bean configuration:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <!--================ Test the process of instantiating a bean in Spring BeanFactory --> <bean id="employee" init-method="init" destroy-method="destroy"> <!--The default department is the R&D department --> <property name="department"> <value>bean2: R&D department</value> </property> </bean> </beans>
Test class:
/** * BeanFactory instantiation Bean engineering test class*/public class Test {public static void main(String args[]) {Test test = new Test();test.test();}public void test() {ApplicationContext context = BeanContextHelper.getApplicationContext();Employee employee = (Employee) context.getBean("employee");System.out.println("************From Spring The final bean instance obtained by BeanFactory**********");System.out.println("The value of the final bean:" + employee);}}Running results:
************Step 1: Call the default constructor of the bean**************** The value of bean1: Employee [id=bean1:G080405214, name=null, sex=null, age=null, nativePlace=null, department=null, beanName=null] ************Stand the name of the bean*******************Set the name of the bean******************* The value of bean2: Employee [id=bean1:G080405214, name=null, sex=null, age=null, nativePlace=null, department=bean2: R&D department, beanName=myBeanName] ********* Step 3: Check whether the bean has implemented the InitializingBean interface******************* The value of bean3: Employee [id=bean1:G080405214, name=bean3:Li Xiaohong, sex=bean3:female, age=bean3:25, nativePlace=null, department=bean2: R&D department, beanName=myBeanName] ********* Step 4: Check whether init-method is specified in the bean configuration file. This attribute**************** The value of bean4: Employee [id=bean1:G080405214, name=bean3:Li Xiaohong, sex=bean3:female, age=bean3:25, nativePlace=bean3:Beijing, department=bean2:R&D department, beanName=myBeanName] *************The final bean instance obtained from Spring BeanFactory************* The final bean value: Employee [id=bean1:G080405214, name=bean3:Li Xiaohong, sex=bean3:female, age=bean3:25, nativePlace=bean3:Beijing, department=bean2:R&D department, beanName=myBeanName]
Judging from the operation results, we should be very clear about the specific process of bean instantiation.
Employee implements 3 interfaces:
InitializingBean: This interface provides the afterPropertiesSet() method, which is used to provide bean with the function of defining initialization.
DisposableBean: This interface provides destroy() method, which is used to provide operational functions before bean instance is destroyed.
BeanNameAware: This interface provides the setBeanName() method, which is used to provide the function of setting the bean name. From the above running results, this method is carried out in the second step.
Summarize
The above is the entire content of this article about the process of instantiating a bean in Spring. I hope it will be helpful to everyone. Interested friends can continue to refer to this site:
" Two ways to customize the acquisition of beans in Spring "
" Java's Spring Annotation Configuration Bean Instance Code Analysis "
" Detailed explanation of the life cycle of Spring configuration and use "
If there are any shortcomings, please leave a message to point it out. Thank you friends for your support for this site!