Spring框架簡介④

Milky-way發表於2018-08-07

bean的延遲載入

需要宣告一點, 按照之前的例子來講, User預設情況下是在容器初始化的過程中被建立的, 也就是預設並沒有使用延遲載入

所有的bean在預設情況下都是非延遲載入的, 容器建立bean就會被建立, 並且是單例的, 單例後續再講

此時的配置檔案是:

<?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-3.2.xsd">

	<bean id="user" class="com.rl.spring.model.User" ></bean>
	
</beans>

如要配置成延遲載入狀態 只需新增lazy-init=true屬性即可

<?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-3.2.xsd">

	<bean id="user" class="com.rl.spring.model.User" lazy-init="true"></bean>
	
</beans>

如需配置全域性, 還可設定預設為延遲載入

<?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-3.2.xsd" default-lazy-init="true">

	<bean id="user" class="com.rl.spring.model.User" ></bean>
	<bean id="userDao" class="com.rl.spring.dao.impl.UserDaoImpl"></bean>
</beans>

bean的作用域

此時涉及到單例(singleton)或者是多例(prototype), 預設情況下是單例的形式

單例的情況下是不管怎麼getBean都只會得到一個例項, 單例可設定延遲載入或非延遲載入(預設情況), 如圖

測試類:

package com.rl.spring.test;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.rl.spring.model.User;

public class TestSpring {

	@Test
	public void test() {
		ApplicationContext ctx =
				new ClassPathXmlApplicationContext(new String[]{"ApplicationContext1.xml"});
		User user = (User) ctx.getBean("user");
		User user1 = (User) ctx.getBean("user");
		System.out.println(user);
		System.out.println(user1);
	}
}

輸出:

com.rl.spring.model.User@650e52
com.rl.spring.model.User@650e52

 

多例的情況是必須"延遲載入", 因為容器不知道要給你建立多少個例項, 所以只能延遲載入, 等到你需要建立該例項的時候再給你建立即可

<?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-3.2.xsd" >

	<bean id="user" class="com.rl.spring.model.User" scope="prototype"></bean>
	
</beans>

 

bean的生命週期

生命週期因延遲載入跟非延遲載入而不同 下面進行測試

在User中定義兩個方法 init() 和 destroy()

package com.rl.spring.model;

public class User {
	
	private Integer userId;
	
	private String username;
	
	public User() {
		System.out.println("user被建立");
	}

	public void init(){
		System.out.println("我出來了");
	}
	public void destroy(){
		System.out.println("20年後又是一條好漢");
	}
	
	public Integer getUserId() {
		return userId;
	}

	public void setUserId(Integer userId) {
		this.userId = userId;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}
}

在配置檔案中設定這兩個方法

<?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-3.2.xsd" >

	<!-- <bean id="user" class="com.rl.spring.model.User" scope="prototype"></bean> -->
	<bean id="user" class="com.rl.spring.model.User" init-method="init" destroy-method="destroy"></bean>
	
</beans>

設定好之後, 執行測試程式碼

package com.rl.spring.test;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.rl.spring.model.User;

public class TestSpring {

	@Test
	public void test() {
		ApplicationContext ctx =
				new ClassPathXmlApplicationContext(new String[]{"ApplicationContext1.xml"});
		User user = (User) ctx.getBean("user");
		
		//強制轉換為其子類, 這樣才能進行close容器
		AbstractApplicationContext actx = (AbstractApplicationContext)ctx;
		//關閉容器
		actx.close();
	}
}

此時當預設非延遲載入建立容器的時候, 例項bean被建立出來, 之後init方法被呼叫, 當容器被關閉(銷燬), 之後呼叫destroy方法

相關文章