通过之前的学习使用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采用立即加载创建对象