目录结构
类关系
类分析:
- BeanFactory接口:定义一个得到bean的接口
package main.java.com.pickle.springframework.factory;
import main.java.com.pickle.springframework.factory.config.BeanDefinition;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/**
* @author Pickle
* @version V1.0
* @date 2024/3/21 15:20
*/
public interface BeanFactory {
Object getBean(String beanName);
}
- SingletonBeanRegistry接口:定义一个获得单例对象的接口
package main.java.com.pickle.springframework.factory.config;
/**
* @author Pickle
* @version V1.0
* @date 2024/3/21 16:05
*/
public interface SingletonBeanRegistry {
Object getSingleton(String beanName);
}
- DefaultSingletonBeanRegistry类:SingletonBeanRegistry的实现类,还有单例对象容器,实现了单例对象的注册和获取
package main.java.com.pickle.springframework.factory.support;
import main.java.com.pickle.springframework.factory.config.SingletonBeanRegistry;
import java.util.HashMap;
import java.util.Map;
/**
* 持有一个单例容器,管理单例对象的存取
*
* @author Pickle
* @version V1.0
* @date 2024/3/21 16:06
*/
public class DefaultSingletonBeanRegistry implements SingletonBeanRegistry {
//单例对象容器
private final Map<String, Object> singletonObjects = new HashMap<>();
/**
* 在容器中获取单例对象
*
* @param beanName 单例对象名称
* @return 单例对象
*/
@Override
public Object getSingleton(String beanName) {
return singletonObjects.get(beanName);
}
/**
* 在容器中添加单例对象
*
* @param beanName 单例对象名称
* @param singletonObject 单例对象
*/
protected void addSingleton(String beanName, Object singletonObject) {
singletonObjects.put(beanName, singletonObject);
}
}
- AbstractBeanFactory抽象类:实现了BeanFactory的接口
package main.java.com.pickle.springframework.factory.support;
import main.java.com.pickle.springframework.BeansException;
import main.java.com.pickle.springframework.factory.BeanFactory;
import main.java.com.pickle.springframework.factory.config.BeanDefinition;
/**
* @author Pickle
* @version V1.0
* @date 2024/3/21 16:11
*/
public abstract class AbstractBeanFactory extends DefaultSingletonBeanRegistry implements BeanFactory {
/**
* @param beanName bean name
* @return bean
*/
@Override
public Object getBean(String beanName) {
//直接从单例容器中拿到bean对象
Object bean = getSingleton(beanName);
if (bean != null) {
return bean;
}
//如果单例容器中拿不到,就去拿到该bean的注册信息,创建一个实例并保存到单例容器中
BeanDefinition beanDefinition = getBeanDefinition(beanName);
bean = createBean(beanName, beanDefinition);
addSingleton(beanName, bean);
return bean;
}
protected abstract BeanDefinition getBeanDefinition(String beanName) throws BeansException;
protected abstract Object createBean(String beanName, BeanDefinition beanDefinition) throws BeansException;
}
- BeanDefinitionRegistry接口:定义了BeanDefinition注册方法
package main.java.com.pickle.springframework.factory.support;
import main.java.com.pickle.springframework.factory.config.BeanDefinition;
/**
* @author Pickle
* @version V1.0
* @date 2024/3/21 16:44
*/
public interface BeanDefinitionRegistry {
/**
* 向注册表注册bean的class信息
* @param beanName beanName
* @param beanDefinition bean的class的包装类
*/
void registerBeanDefinition(String beanName, BeanDefinition beanDefinition);
}
- AbstractAutowireCapableBeanFactory抽象类:实现创建对象自动装入容器
package main.java.com.pickle.springframework.factory.support;
import main.java.com.pickle.springframework.BeansException;
import main.java.com.pickle.springframework.factory.config.BeanDefinition;
/**
* @author Pickle
* @version V1.0
* @date 2024/3/21 16:33
*/
public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFactory {
@Override
protected Object createBean(String beanName, BeanDefinition beanDefinition) throws BeansException {
//创建bean,并且加入单例容器
Object bean;
try {
bean = beanDefinition.getBeanClass().newInstance();
} catch (InstantiationException | IllegalAccessException e) {
throw new BeansException("Instantiation of bean failed",e);
}
addSingleton(beanName,bean);
return bean;
}
}
- DefaultListableBeanFactory类:Bean工厂
package main.java.com.pickle.springframework.factory.support;
import main.java.com.pickle.springframework.BeansException;
import main.java.com.pickle.springframework.factory.config.BeanDefinition;
import java.util.HashMap;
import java.util.Map;
/**
* 存放bean的class注册信息
*
* @author Pickle
* @version V1.0
* @date 2024/3/21 16:46
*/
public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFactory implements BeanDefinitionRegistry{
private final Map<String,BeanDefinition> beanDefinitionMap = new HashMap<>();
@Override
protected BeanDefinition getBeanDefinition(String beanName) throws BeansException {
final BeanDefinition beanDefinition = beanDefinitionMap.get(beanName);
if (beanDefinition == null) throw new BeansException("No bean named " + beanName + "is defined");
return beanDefinition;
}
@Override
public void registerBeanDefinition(String beanName, BeanDefinition beanDefinition) {
beanDefinitionMap.put(beanName,beanDefinition);
}
}
- 异常类
package main.java.com.pickle.springframework;
/**
* @author Pickle
* @version V1.0
* @date 2024/3/21 16:25
*/
public class BeansException extends RuntimeException{
public BeansException(String message) {
super(message);
}
public BeansException(String message, Throwable cause) {
super(message, cause);
}
}
测试类
package test.java.com.pickle.springframeword.test.bean;
import main.java.com.pickle.springframework.entity.UserService;
import main.java.com.pickle.springframework.factory.config.BeanDefinition;
import main.java.com.pickle.springframework.factory.support.DefaultListableBeanFactory;
import org.junit.Test;
/**
* @author Pickle
* @version V1.0
* @date 2024/3/21 15:27
*/
public class UserServiceTest {
@Test
public void test_BeanFactory() {
//1、初始化BeanFactory
final DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
//2、注册bean
beanFactory.registerBeanDefinition("userService", new BeanDefinition(UserService.class));
//3、第一次获取bean
final UserService userService = (UserService) beanFactory.getBean("userService");
userService.queryUserInfo(); //查询用户信息
//4、第二次获取bean
final UserService userService_singleton = (UserService) beanFactory.getBean("userService");
userService_singleton.queryUserInfo(); //查询用户信息
//5、看看是不是单例
System.out.println(userService == userService_singleton); //true
}
}
标签:java,实现,Spring,beanName,bean,Small,pickle,main,com
From: https://www.cnblogs.com/poteitoutou/p/18088035