Spring三種創建bean對象的方式/作用範圍/生命週期
項目結構
配置文件
-
pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.xiaoge</groupId> <artifactId>bean</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.0.2.RELEASE</version> </dependency> </dependencies> </project>
-
bean.xml**(創建bean對象的方式/作用範圍/生命週期)**
<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"> <!-- 把對象的創建交給spring來管理 --> <!-- spring對bean的管理細節 1. 創建bean的三種方式 2. bean對象的作用範圍 3. bean對象的生命週期 --> <!-- 創建Bean的三種方式 --> <!-- 第一種方式: 使用默認構造函數創建. 早spring的配置文件中使用bean標籤. 配以id和class屬性之後. 且沒有其他屬性和標籤時. 採用的就是默認構造函數創建bean對象. 此時如果類中沒有默認構造函數. 則對象無法創建. <bean id="accountService" class="com.xiaoge.service.impl.AccountServiceImpl"></bean> --> <!-- 第二種方式: 使用普通工廠中的方法創建對象 (使用某個類中的方法創建對象, 並存入spring容器) <bean id="instanceFactory" class="com.xiaoge.factory.InstanceFactory"></bean> factory-bean: 指定工廠Bean factory-method: 指定工廠方法來獲取對象 <bean id="accountService" factory-bean="instanceFactory" factory-method="getAccountService"></bean> --> <!-- 第三種方式: 使用工廠中的靜態方法創建對象 (使用某個類中的靜態方法創建對象, 並存入spring容器) <bean id="accountService" class="com.xiaoge.factory.StaticFactory" factory-method="getAccountService"></bean> --> <!-- bean的作用範圍調整 bean標籤的scope屬性: 作用: 用於指定bean的作用範圍 取值: 常用的就是單例的和多例的 singleton: 單例的(默認值) prototype: 多例的 request: 作用於web應用的請求範圍 session: 作用於web應用的會話範圍 global-session: 作用於集羣環境的會話範圍(全局會話範圍), 當不是集羣環境時, 它就是session <bean id="accountService" class="com.xiaoge.service.impl.AccountServiceImpl" scope="prototype"></bean> --> <!-- bean對象的生命週期 單例對象 出生: 當容器創建時對象出生 活着: 只要容器還在, 對象一直活着 死亡: 容器銷燬, 對象消亡 總結: 單例對象的生命週期和容器相同 多例對象 出生: 當我們使用對象時spring框架爲我們創建 活着: 對象只要是在使用過程中就一直活着. 死亡: 當對象長時間不用, 且沒有別的對象引用時, 由Java的垃圾回收器回收 --> <bean id="accountService" class="com.xiaoge.service.impl.AccountServiceImpl" scope="prototype" init-method="init" destroy-method="destroy"></bean> </beans>
工廠
-
InstanceFactory
package com.xiaoge.factory; import com.xiaoge.service.AccountService; import com.xiaoge.service.impl.AccountServiceImpl; /** * @Author: 瀟哥 * @DateTime: 2020/3/26 下午9:32 * @Description: 模擬一個工廠類(該類可能是存在於jar包中的, 我們無法通過修改源碼的方式來提供默認構造函數) */ public class InstanceFactory { public AccountService getAccountService(){ return new AccountServiceImpl(); } }
-
StaticFactory
package com.xiaoge.factory; import com.xiaoge.service.AccountService; import com.xiaoge.service.impl.AccountServiceImpl; /** * @Author: 瀟哥 * @DateTime: 2020/3/26 下午9:32 * @Description: 模擬一個工廠類(該類可能是存在於jar包中的, 我們無法通過修改源碼的方式來提供默認構造函數) */ public class StaticFactory { public static AccountService getAccountService(){ return new AccountServiceImpl(); } }
業務層接口
-
AccountService
package com.xiaoge.service; /** * @Author: 瀟哥 * @DateTime: 2020/3/19 下午7:26 * @Description: 賬戶業務層接口 */ public interface AccountService { /** * 模擬保存 */ public void saveAccount(); }
業務層實現類
-
AccountServiceImpl
package com.xiaoge.service.impl; import com.xiaoge.service.AccountService; /** * @Author: 瀟哥 * @DateTime: 2020/3/19 下午7:28 * @Description: 賬戶業務層實現類 */ public class AccountServiceImpl implements AccountService { public AccountServiceImpl(){ System.out.println("對象已創建"); } public void saveAccount() { System.out.println("service中的saveAccount方法執行了"); } public void init() { System.out.println("對象初始化..."); } public void destroy() { System.out.println("對象銷燬..."); } }
main
-
Client
package com.xiaoge.ui; import com.xiaoge.service.AccountService; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * @Author: 瀟哥 * @DateTime: 2020/3/19 下午7:35 * @Description: 模擬表現層, 調用業務層 */ public class Client { /** * @param args */ public static void main(String[] args) { // 1. 獲取核心容器對象 ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean.xml"); // 2. 獲取對象 AccountService as = (AccountService)applicationContext.getBean("accountService"); // 3. 執行方法 as.saveAccount(); // 4. 手動釋放容器 applicationContext.close(); // 運行結果 對象已創建 對象初始化... service中的saveAccount方法執行了 } }