21-Spring5 使用GenericApplicationContext创建IOC容器

1.创建容器的方式

这是常用的几种创建IOC容器的方式
在这里插入图片描述

2. 使用GenericApplicationContext创建IOC容器

package com.limi.test;
import com.limi.entity.Account;
import org.junit.Test;
import org.springframework.context.support.GenericApplicationContext;

public class MyTest {

    @Test
    public void test1(){

        //1. 创建 GenericApplicationContext 对象
        GenericApplicationContext genericApplicationContext = new GenericApplicationContext();

        //2. 调用 context 的方法对象注册
        genericApplicationContext.refresh();  //清空genericApplicationContext创建的容器, 并不会清空其他方式创建的容器
        //将对象实例注册到容器中
        genericApplicationContext.registerBean("account1", Account.class, ()->new Account(9, "lucy", 9.99));

        //3. 从genericApplicationContext创建的IOC容器中获取实例
        Account account = genericApplicationContext.getBean("account1", Account.class);
        System.out.println(account);
    }
}

在这里插入图片描述

注意: genericApplicationContext.registerBean(“account1”, Account.class, ()->new Account(9, “lucy”, 9.99)) 中的第一个参数(实例名称)可以不写, 那么默认注册到IOC的对象实例名称为全类名.
修改演示一下
在这里插入图片描述

代码

package com.limi.test;
import com.limi.entity.Account;
import org.junit.Test;
import org.springframework.context.support.GenericApplicationContext;

public class MyTest {

    @Test
    public void test1(){

        //1. 创建 GenericApplicationContext 对象
        GenericApplicationContext genericApplicationContext = new GenericApplicationContext();

        //2. 调用 context 的方法对象注册
        genericApplicationContext.refresh();  //清空genericApplicationContext创建的容器, 并不会清空其他方式创建的容器
        //将对象实例注册到容器中
        genericApplicationContext.registerBean(Account.class, ()->new Account(9, "lucy", 9.99));

        //3. 从genericApplicationContext创建的IOC容器中获取实例
        Account account = genericApplicationContext.getBean("com.limi.entity.Account", Account.class);
        System.out.println(account);
    }
}


在这里插入图片描述

3.项目代码

在这里插入图片描述
Account

package com.limi.entity;

public class Account {

    private Integer id;

    private String name;

    private Double money;

    public Account(Integer id, String name, Double money) {
        this.id = id;
        this.name = name;
        this.money = money;
    }

    @Override
    public String toString() {
        return "Account{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", money=" + money +
                '}';
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Double getMoney() {
        return money;
    }

    public void setMoney(Double money) {
        this.money = money;
    }
}

测试类MyTest

package com.limi.test;
import com.limi.entity.Account;
import org.junit.Test;
import org.springframework.context.support.GenericApplicationContext;

public class MyTest {

    @Test
    public void test1(){

        //1. 创建 GenericApplicationContext 对象
        GenericApplicationContext genericApplicationContext = new GenericApplicationContext();

        //2. 调用 context 的方法对象注册
        genericApplicationContext.refresh();  //清空genericApplicationContext创建的容器, 并不会清空其他方式创建的容器
        //将对象实例注册到容器中
        genericApplicationContext.registerBean(Account.class, ()->new Account(9, "lucy", 9.99));

        //3. 从genericApplicationContext创建的IOC容器中获取实例
        Account account = genericApplicationContext.getBean("com.limi.entity.Account", Account.class);
        System.out.println(account);
    }
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值