1、创建一个Bean对象,相当于Spring中的标签,用于定义id和classPath
package javaNote.aopSingleton;
public class BeanDefined {
private String id;
private String classPath;
//默认为singleton
private String scope = "singleton";
public String getScope() {
return scope;
}
public void setScope(String scope) {
this.scope = scope;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getClassPath() {
return classPath;
}
public void setClassPath(String classPath) {
this.classPath = classPath;
}
}
2、实体类
package javaNote.aopSingleton;
public class Student {
private String sName;
private int age;
public Student() {
this.age = 19;
this.sName = "Mike";
}
public String getsName() {
return sName;
}
public void setsName(String sName) {
this.sName = sName;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Student{" +
"sName='" + sName + '\'' +
", age=" + age +
'}';
}
}
3、工厂模式,BeanFactory来获取实例,利用HashMap来保存实例
package javaNote.aopSingleton;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
public class BeanFactory {
//存放bean的集合
private List<BeanDefined> beanDefinedList;
//存放已经创建好的实例
private Map<String, Object> springIoc;
public BeanFactory(List<BeanDefined> beanDefinedList) throws Exception {
this.beanDefinedList = beanDefinedList;
springIoc = new HashMap<>();
for (BeanDefined bean : beanDefinedList){
//如果参数是singleton,那么就创建实例
if (Objects.equals(bean.getScope(), "singleton")){
Class<?> classFile = Class.forName(bean.getClassPath());
Object instance = classFile.getDeclaredConstructor().newInstance();
//以singleton为键值保存在hashMap中
springIoc.put(bean.getScope(), instance);
}
}
}
public Object getBean(String beanId) throws Exception {
Object instance = null;
//获取beaId
for (BeanDefined bean : beanDefinedList){
//判断bean的id是否相同
if (beanId.equals(bean.getId())){
//判断是否prototype,原型模式,如果是,那么就创建实例
if (Objects.equals(bean.getId(), "prototype")){
String classPath = bean.getClassPath();
Class<?> classFile = Class.forName(classPath);
instance = classFile.getDeclaredConstructor().newInstance();
return instance;
}
//如果不是,那么就从springIoc中取出来
return springIoc.get("singleton");
}
}
return null;
}
}
4、测试类
package javaNote.aopSingleton;
import java.util.ArrayList;
import java.util.List;
public class BeanTest {
public static void main(String[] args) throws Exception {
//1、声明注册
BeanDefined defined = new BeanDefined();
defined.setId("student");
defined.setClassPath("javaNote.aopSingleton.Student");
List<BeanDefined> beanList = new ArrayList<>();
beanList.add(defined);
//2、声明一个BeanFactory,类似于Spring中的ApplicationContext
//被标记的类
BeanFactory factory = new BeanFactory(beanList);
//开发人员向BeanFactory索要实例
Student student1 = (Student)factory.getBean("student");
System.out.println(student1.hashCode());
Student student2 = (Student)factory.getBean("student");
System.out.println(student2.hashCode());
//打印出来的结果,说明 scope 属性的singleton,标志的类会存储在Spring框架中
// 214126413
//214126413
}
}