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);
}
}