通過之前的學習使用BeanFactory通過延遲加載實現了全部對象的創建並存儲到了容器map中
今天學習SpringIOC的spring-context如何實現立即加載!
預習:
- 需要引入依賴:spring-context
- bean標籤的解釋:
- ①id:從容器中準確取出對象的唯一標識,接口類文件名(開頭字母小寫)
- ②class:所要創建對象的實現類全路徑
- ③scope:代表創建對象的範圍:
- 1>singleton:default 單例模式:即全局使用的都是同一個對象 後期對象由Spring維護(生存&銷燬)
- 創建出來的對象地址全部一致
- 2>prototype:多例模式:需要時就創建一個新的對象,不保證唯一 後期對象不由Spring維護
- 創建出來的對象的地址有可能不一致
- 1>singleton:default 單例模式:即全局使用的都是同一個對象 後期對象由Spring維護(生存&銷燬)
- 生命週期:
- init-method:對象創建之後執行的方法
- destroy-method:對象銷燬之前執行的方法
一.案例展示
1.測試準備
項目準備:
2.代碼展示:
***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.howie</groupId>
<artifactId>springIOC_eager_load</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<!--單元測試-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<!--spring-context-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.8.RELEASE</version>
</dependency>
</dependencies>
</project>
***ApplicationContext.xml
<?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="accountDao" class="com.howie.dao.impl.AccountDaoImpl"
init-method="initShow" destroy-method="destoryShow"
scope="singleton"
/>
<bean id="accountService" class="com.howie.service.impl.AccountServiceImpl"
init-method="initShow" destroy-method="destoryShow"
scope="prototype"
/>
</beans>
***Account.java
package com.howie.pojo;
/**
* @Author weihuanwen
* @Date 2019/8/13 21:53
* @Version 1.0
*/
public class Account {
private String id;
private String accountName;
private double money;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getAccountName() {
return accountName;
}
public void setAccountName(String accountName) {
this.accountName = accountName;
}
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
@Override
public String toString() {
return "Account{" +
"id='" + id + '\'' +
", accountName='" + accountName + '\'' +
", money=" + money +
'}';
}
}
***AccountDao.java
package com.howie.dao;
import com.howie.pojo.Account;
/**
* @Author weihuanwen
* @Date 2019/8/13 22:09
* @Version 1.0
*/
public interface AccountDao {
/**
* 保存賬戶信息
* @param account
*/
void saveAccount(Account account);
}
***AccountDaoImpl.java
package com.howie.dao.impl;
import com.howie.dao.AccountDao;
import com.howie.pojo.Account;
/**
* @Author weihuanwen
* @Date 2019/8/13 22:09
* @Version 1.0
*/
public class AccountDaoImpl implements AccountDao {
/**
* 爲了觀察生命週期,在空參構造中加入提示信息
*/
public AccountDaoImpl() {
System.out.println("調用了AccountDaoImpl的空參構造創建對象!");
}
public void initShow(){
System.out.println("AccountDaoImpl對象創建成功!");
}
public void destoryShow(){
System.out.println("AccountDaoImpl對象即將被銷燬!");
}
/**
* 保存賬戶信息
* 模擬保存數據至數據庫的操作
* @param account
*/
public void saveAccount(Account account) {
System.out.println(account);
System.out.println("賬戶信息保存成功!");
}
}
***AccountService.java
package com.howie.service;
import com.howie.pojo.Account;
/**
* @Author weihuanwen
* @Date 2019/8/13 21:54
* @Version 1.0
*/
public interface AccountService {
/**
* 保存賬戶信息
* @param account
*/
void saveAccount(Account account);
}
***AccountServiceImpl.java
package com.howie.service.impl;
import com.howie.dao.AccountDao;
import com.howie.pojo.Account;
import com.howie.service.AccountService;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @Author weihuanwen
* @Date 2019/8/13 21:54
* @Version 1.0
*/
public class AccountServiceImpl implements AccountService {
private AccountDao accountDao;
/**
* 爲了觀察生命週期,在空參構造中加入提示信息
*/
public AccountServiceImpl() {
//獲取spring容器對象
ClassPathXmlApplicationContext context =
new ClassPathXmlApplicationContext("ApplicationContext.xml");
this.accountDao = context.getBean("accountDao",AccountDao.class);
System.out.println("調用了AccountServiceImpl的空參構造創建對象!");
}
public void initShow(){
System.out.println("AccountServiceImpl對象創建成功!");
}
public void destoryShow(){
System.out.println("AccountServiceImpl對象即將被銷燬!");
}
/**
* 保存用戶信息
* @param account
*/
@Override
public void saveAccount(Account account) {
accountDao.saveAccount(account);
}
}
***AccountTest.java
package com.howie.controller;
import com.howie.dao.AccountDao;
import com.howie.pojo.Account;
import com.howie.service.AccountService;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @Author weihuanwen
* @Date 2019/8/13 22:21
* @Version 1.0
*/
public class AccountTest {
/**
* 保存賬戶
*/
@Test
public void saveAccount(){
ClassPathXmlApplicationContext context =
new ClassPathXmlApplicationContext("ApplicationContext.xml");
AccountService accountService = context.getBean("accountService", AccountService.class);
Account account = new Account();
account.setId("2");
account.setAccountName("Lily");
account.setMoney(1000);
accountService.saveAccount(account);
}
/**
* 測試多例模式
*/
@Test
public void testScope(){
ClassPathXmlApplicationContext context =
new ClassPathXmlApplicationContext("ApplicationContext.xml");
AccountService accountService1 = context.getBean("accountService", AccountService.class);
System.out.println("accountService1 ::: "+accountService1);
AccountDao accountDao1 = context.getBean("accountDao", AccountDao.class);
System.out.println("accountDao1 ::: "+accountDao1);
AccountService accountService2 = context.getBean("accountService", AccountService.class);
System.out.println("accountService2 ::: "+accountService2);
AccountDao accountDao2 = context.getBean("accountDao", AccountDao.class);
System.out.println("accountDao2 ::: "+accountDao2);
}
/**
* 測試聲明週期
*/
@Test
public void testLif(){
System.out.println("容器即將創建!");
ClassPathXmlApplicationContext cpxac =
new ClassPathXmlApplicationContext("ApplicationContext.xml");
System.out.println("容器即將關閉!");
//關閉容器時對象會銷燬
cpxac.close();
}
}
3.測試
①執行saveAccount()控制檯輸出
調用了AccountDaoImpl的空參構造創建對象!
AccountDaoImpl對象創建成功!
調用了AccountDaoImpl的空參構造創建對象!
AccountDaoImpl對象創建成功!
調用了AccountServiceImpl的空參構造創建對象!
AccountServiceImpl對象創建成功!
Account{id='2', accountName='Lily', money=1000.0}
賬戶信息保存成功!
②執行testScope()控制檯輸出(這裏只截取有效信息)
accountService1 ::: com.howie.service.impl.AccountServiceImpl@436e852b
accountService2 ::: com.howie.service.impl.AccountServiceImpl@29b5cd00
accountDao1 ::: com.howie.dao.impl.AccountDaoImpl@32d2fa64
accountDao2 ::: com.howie.dao.impl.AccountDaoImpl@32d2fa64
③執行testLife()控制檯輸出
容器即將創建!
調用了AccountDaoImpl的空參構造創建對象!
AccountDaoImpl對象創建成功!
容器即將關閉!
AccountDaoImpl對象即將被銷燬!
二.總結:
1.對象的創建時機?
spring在程序啓動的時候就會將所有的對象全部創建出來置入容器內
需要使用對象的時候,只需要從容器內取出即可
2.單例/多例
bean標籤中的scope屬性:
singleton | 單例模式(默認) |
prototype | 多例模式 |
通過上面的測試結果可以看得出二者的區別,這裏不再贅述!
3.SpringIOC採用立即加載創建對象