Spring核心容器及兩個核心容器引發的問題
項目結構
配置文件
-
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>spring</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
<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來管理 --> <bean id="accountService" class="com.xiaoge.service.impl.AccountServiceImpl"></bean> <bean id="accountDao" class="com.xiaoge.dao.impl.AccountDaoImpl"></bean> </beans>
持久層接口
-
AccountDao
package com.xiaoge.dao; /** * @Author: 瀟哥 * @DateTime: 2020/3/19 下午7:31 * @Description: TODO */ public interface AccountDao { public void saveAccount(); }
持久層實現類
-
AccountDaoImpl
package com.xiaoge.dao.impl; import com.xiaoge.dao.AccountDao; /** * @Author: 瀟哥 * @DateTime: 2020/3/19 下午7:31 * @Description: TODO */ public class AccountDaoImpl implements AccountDao { public void saveAccount() { System.out.println("保存成功!"); } }
業務層接口
-
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.dao.AccountDao; import com.xiaoge.dao.impl.AccountDaoImpl; import com.xiaoge.service.AccountService; /** * @Author: 瀟哥 * @DateTime: 2020/3/19 下午7:28 * @Description: 賬戶業務層實現類 */ public class AccountServiceImpl implements AccountService { private AccountDao accountDao = new AccountDaoImpl(); public AccountServiceImpl(){ System.out.println("對象已創建"); } public void saveAccount() { accountDao.saveAccount(); } }
main
-
Client
package com.xiaoge.ui; import com.xiaoge.service.AccountService; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.xml.XmlBeanFactory; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; /** * @Author: 瀟哥 * @DateTime: 2020/3/19 下午7:35 * @Description: 模擬表現層, 調用業務層 */ public class Client { /** * 獲取spring的Ioc核心容器, 並根據id獲取對象 * * ApplicationContext的三個常用實現類 * 1. ClassPathXmlApplicationContext: 它可以加載類路徑下的配置文件, 要求配置文件必須在類路徑下. 不在的話. 加載不了. (1-2比較 1更常用) * 2. FileSystemXmlApplicationContext: 它可以加載磁盤任意路徑下的配置文件(必須有訪問權限) * * * 3. AnnotationConfigApplicationContext: 它是用於讀取註解創建容器的. * * 核心容器的兩個接口引發的問題: * ApplicationContext: 單例對象使用 採用此接口 * 它在構建核心容器時, 創建對象採取的策略是採用立即加載的方式. 也就是說, 只要一讀取完配置文件馬上就創建配置文件中的對象. * BeanFactory: 多例對象使用 * 它在構建核心容器時, 創建對象採取的策略是採用延遲加載的方式. 也就是說, 什麼時候根據id獲取對象了, 什麼時候才真正的創建對象. * @param args */ public static void main(String[] args) { // 1. 獲取核心容器對象 /* ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml"); 注意: mac 或 linux 在絕對路徑前要加個 /(源碼中對路徑做了處理, 把去掉了開始的/) ApplicationContext ac = new FileSystemXmlApplicationContext("/" + "/Users/xiaoge/Desktop/bean.xml"); */ // 2. 根據id獲取Bean對象 // ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml"); /* 兩種方式都行: 第一種: 獲取的是object類型自己強轉 第二種: 傳遞字節碼, 自動幫你轉換成對應的類型 */ // AccountService as = (AccountService)ac.getBean("accountService"); // AccountDao ad = ac.getBean("accountDao", AccountDao.class); // System.out.println(as); // System.out.println(ad); /*********************BeanFactory 這種已經棄用***************************/ Resource resource = new ClassPathResource("bean.xml"); BeanFactory factory = new XmlBeanFactory(resource); AccountService as = (AccountService)factory.getBean("accountService"); System.out.println(as); // 運行結果 對象已創建 com.xiaoge.service.impl.AccountServiceImpl@6ea6d14e } }