标签:容器 Account 21 class genericApplicationContext IOC public GenericApplicationConte
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);
}
}
标签:容器,Account,21,class,genericApplicationContext,IOC,public,GenericApplicationConte 来源: https://blog.csdn.net/qq_41865229/article/details/121318369
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。