1、创建你的pojo对象
package cn.pojo;
public class UserDao {
public void queryUserInfo(){
System.out.println("hello world!!!");
}
}
2、创建resources文件夹并创建beans.properties文件
注意:cn.pojo.UserDao要对应填写你自己创建的
userDao:cn.pojo.UserDao
3、定义Bean
package cn.bean;
public class BeanDefinition {
private String beanName;
private Class beanClass;
public String getBeanName() {
return beanName;
}
public void setBeanName(String beanName) {
this.beanName = beanName;
}
public Class getBeanClass() {
return beanClass;
}
public void setBeanClass(Class beanClass) {
this.beanClass = beanClass;
}
}
4、资源加载
package cn.utils;
import cn.bean.BeanDefinition;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
public class ResourceLoader {
public static Map<String, BeanDefinition> getResource() {
Map<String, BeanDefinition> beanDefinitionMap = new HashMap<>(16);
Properties properties = new Properties();
try {
InputStream inputStream = ResourceLoader.class.getResourceAsStream("/beans.properties");
properties.load(inputStream);
Iterator<String> it = properties.stringPropertyNames().iterator();
while (it.hasNext()) {
String key = it.next();
String className = properties.getProperty(key);
BeanDefinition beanDefinition = new BeanDefinition();
beanDefinition.setBeanName(key);
Class clazz = Class.forName(className);
beanDefinition.setBeanClass(clazz);
beanDefinitionMap.put(key, beanDefinition);
}
inputStream.close();
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
return beanDefinitionMap;
}
}
5、Bean注册
package cn.bean;
import java.util.HashMap;
import java.util.Map;
public class BeanRegister {
private Map<String, Object> singletonMap = new HashMap(32);
public Object getSingletonBean(String beanName) {
return singletonMap.get(beanName);
}
public void registerSingletonBean(String beanName, Object bean) {
if (singletonMap.containsKey(beanName)) {
return;
}
singletonMap.put(beanName, bean);
}
}
6、对象工厂
package cn.factory;
import cn.bean.BeanDefinition;
import cn.bean.BeanRegister;
import cn.utils.ResourceLoader;
import java.util.HashMap;
import java.util.Map;
public class BeanFactory {
private Map<String, BeanDefinition> beanDefinitionMap = new HashMap();
private BeanRegister beanRegister;
public BeanFactory() {
beanRegister = new BeanRegister();
this.beanDefinitionMap = ResourceLoader.getResource();
}
public Object getBean(String beanName) {
Object bean = beanRegister.getSingletonBean(beanName);
if (bean != null) {
return bean;
}
return createBean(beanDefinitionMap.get(beanName));
}
private Object createBean(BeanDefinition beanDefinition) {
try {
Object bean = beanDefinition.getBeanClass().newInstance();
beanRegister.registerSingletonBean(beanDefinition.getBeanName(), bean);
return bean;
} catch (InstantiationException | IllegalAccessException e) {
e.printStackTrace();
}
return null;
}
}
7、测试
package cn;
import cn.factory.BeanFactory;
import cn.pojo.UserDao;
public class ApiTest {
public static void main(String[] args) {
BeanFactory beanFactory = new BeanFactory();
UserDao userDao1 = (UserDao) beanFactory.getBean("userDao");
userDao1.queryUserInfo();
UserDao userDao2 = (UserDao) beanFactory.getBean("userDao");
userDao2.queryUserInfo();
}
}
效果图:
总结:实际上先通过spi的方式把编译前的bean资源加载,在工厂方法中初始化,在调用bean的时候通过单例的方式获取,如果是第一次获取就通过加载过的资源实例化bean,如果不是就根据bean名称返回bean
|