Spring object lifecycle

A, spring object life cycle

UserInfo create a first class, which includes name attribute, constructor with no arguments, static and non-static configuration method, get, and set methods, init and destroy methods, and each method has a corresponding output:

package club.affengkuang.vo;

public class UserInfo {
	
	private String name;
	
	public UserInfo() {
		System.out.println("构造方法");
	}

	{
		System.out.println("非静态代码块");
	}
	
	static {
		System.out.println("静态代码块");
	}

	

	public String getName() {
		System.out.println("get方法");
		return name;
	}

	public void setName(String name) {
		System.out.println("set方法");
		this.name = name;
	}

	public void init() {
		System.out.println("init");
	}
	
	public void destroy() {
		System.out.println("destroy");
	}
}


The class is then added to application.xml and call name assigned to a property set methods provided init-method and destroy-method value init () and destroy () method, these two properties are meant object is created when you call init () method, call the destroy () method is destroyed:

<?xml version="1.0" encoding="UTF-8"?>
<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="userInfo" class="club.affengkuang.vo.UserInfo" lazy-init="true" init-method="init" destroy-method="destroy">
		<property name="name" value="Tom"></property>
	</bean>
</beans>

 Then create a test class IOC container, and to obtain a UserInfo objects, then the life cycle of spring object on a glance at the console:

package club.affengkuang.test;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {

	public static void main(String[] args) {
		ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("application.xml");
		Object object = applicationContext.getBean("userInfo");
		System.out.println(object);
		applicationContext.close();
	}
}

When the object from the acquired UserInfo IOC container, similar to java objects in the initialization process:

First call UserInfo class static and non-static block (and before execution of non-static after static), and then call the constructor UserInfo class, then call set methods for name assignment to create the final object such as finished then perform init ( ) method, which is the whole process of object creation;

Time and destroy () method is executed when the IOC when the container is closed, when the object is destroyed.

 

Second, attention

destroy () method will only be performed when the scope attribute value of singleton,

role scope, see blog:  detailed scope effect

If the value is set to scope prototype, then the test class code execution will not destroy () method:

<?xml version="1.0" encoding="UTF-8"?>
<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="userInfo" class="club.affengkuang.vo.UserInfo" lazy-init="true" scope="prototype" init-method="init" destroy-method="destroy">
		<property name="name" value="Tom"></property>
	</bean>
</beans>

Published 99 original articles · won praise 81 · views 5104

Guess you like

Origin blog.csdn.net/DangerousMc/article/details/104451115