Spring的Bean的生命週期

Bean的初始化:【注:自動執行

1.在目標類中定義初始化方法:

public interface UserService {
	// 添加用戶犯法
	public void AddUser();
	// 初始化Bean方法
	public void Init(); 
	// 銷燬Bean方法
	public void Destroy();
}

2.在Spring中配置初始化方法:

<?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="userServiceId" class="com.jishuai.bean_life.UserServiceImpl"
    	  init-method="Init" destroy-method="Destroy"
    	  scope="prototype"></bean>
   
</beans>

3.在測試類中實例化Bea,執行Bean對象方法:

@Test
	public void f() throws Exception {
		// spring配置文件路徑
		String xmlPath = "com/jishuai/bean_life/bean.xml";
		// 實例化ApplicationContext對象
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
		// 執行getBean方法 獲取Bean對象
		UserService userService = applicationContext.getBean("userServiceId",UserService.class);
		// 執行Beam對象方法
		userService.AddUser();
	}

4.查看結果,Init()方法被執行


Bean的銷燬:【注:銷燬方法執行的前提是,spring容器必須關閉

1.在目標類中定義銷燬方法:

public interface UserService {
	// 添加用戶犯法
	public void AddUser();
	// 初始化Bean方法
	public void Init(); 
	// 銷燬Bean方法
	public void Destroy();
}

2. spring中配置銷燬方法:

<?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="userServiceId" class="com.jishuai.bean_life.UserServiceImpl"
    	  init-method="Init" destroy-method="Destroy"
    	  scope="prototype"></bean>
   
</beans>

3. 在測試類中實例化Bean對象,執行銷燬方法【這裏有兩種實現方法】

3.1 如果你定義的ApplicationContext對象,銷燬方式如下:

@Test
	public void f() throws Exception {
		// spring配置文件路徑
		String xmlPath = "com/jishuai/bean_life/bean.xml";
		// 實例化ApplicationContext對象
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
		// 執行getBean方法 獲取Bean對象
		UserService userService = applicationContext.getBean("userServiceId",UserService.class);
		// 執行Beam對象方法
		userService.AddUser();
		// 執行Bean的銷燬方法
		applicationContext.getClass().getMethod("close").invoke(applicationContext);
	}

3.2 如果你定義的是ClassPathXmlApplicationContext對象,銷燬方式如下:

@Test
	public void f() throws Exception {
		// spring配置文件路徑
		String xmlPath = "com/jishuai/bean_life/bean.xml";
		// 實例化ApplicationContext對象
		ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext(xmlPath);
		// 執行getBean方法 獲取Bean對象
		UserService userService = classPathXmlApplicationContext.getBean("userServiceId",UserService.class);
		// 執行Beam對象方法
		userService.AddUser();
		// 執行Bean的銷燬方法
		classPathXmlApplicationContext.close();
	}

兩者區別:

因爲ApplicationContext是一個接口,在接口中未定義close()方法,所以得通過反射進行銷燬,

ClassPathXmlApplicationContext爲ApplicationContext的實現類,在該類中定義了close()銷燬方法,所以可以直接調用

注意:

destory方法只能銷燬單例模式下的Bean對象,即作用域爲singlot的Bean對象。

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章