1.beanDefinition 首先是承载class的载体,里面包含了许多如是否单例,属性值等内容。以下只是建议代码,重在理解概念
package org.springframework.spring.beans.factory.config;
/**
* @ClassName : BeanDefinition //类名
* @Description : //描述
* @Author : 10300 //作者
* @Date: 2024/4/27 16:24
*/
public class BeanDefinition {
private Class beanClass;
public BeanDefinition(Class beanClass) {
this.beanClass = beanClass;
}
public Class getBeanClass() {
return this.beanClass;
}
public void setBeanClass(Class beanClass) {
this.beanClass = beanClass;
}
}
2.DefaultListableBeanFactory 可以理解是bean的最外层容器,Map键值对
package org.springframework.spring.beans.factory.support;
import org.springframework.spring.beans.BeansException;
import org.springframework.spring.beans.factory.config.BeanDefinition;
import java.util.HashMap;
import java.util.Map;
/**
* @ClassName : DefaultListableBeanFactory //类名
* @Description : //描述
* @Author : 10300 //作者
* @Date: 2024/4/27 19:39
*/
public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFactory implements BeanDefinitionRegistry{
private final Map<String, BeanDefinition> beanDefinitionMap = new HashMap<>();
@Override
public void registerBeanDefinition(String beanName, BeanDefinition beanDefinition) {
beanDefinitionMap.put(beanName, beanDefinition);
}
@Override
public BeanDefinition getBeanDefinition(String beanName) throws BeansException {
BeanDefinition beanDefinition = beanDefinitionMap.get(beanName);
if (beanDefinition == null) throw new BeansException("No bean named '" + beanName + "' is defined");
return beanDefinition;
}
}
3.调用DefaultListableBeanFactory.getBean(beanName),AbstractAutowireCapableBeanFactory继承AbstractBeanFactory类型,该AbstractBeanFactory类往下继承DefaultSingletonBeanRegistry类
package org.springframework.spring.beans.factory.support;
import org.springframework.spring.beans.factory.config.SingletonBeanRegistry;
import java.util.HashMap;
import java.util.Map;
/**
* @ClassName : DefaultSingletonBeanRegistry //类名
* @Description : //描述
* @Author : 10300 //作者
* @Date: 2024/4/27 19:16
*/
public class DefaultSingletonBeanRegistry implements SingletonBeanRegistry {
private final Map<String, Object> SingletionObjects = new HashMap<>();
@Override
public Object getSingleton(String beanName) {
return this.SingletionObjects.get(beanName);
}
public void addSingleton(String beanName, Object bean) {
this.SingletionObjects.put(beanName, bean);
}
}
AbstractBeanFactory类会判断beanName是否在DefaultSingletionBeanRegistry存在,不存在则去构建bean对象
package org.springframework.spring.beans.factory.support;
import org.springframework.spring.beans.BeansException;
import org.springframework.spring.beans.factory.BeanFactory;
import org.springframework.spring.beans.factory.config.BeanDefinition;
import org.springframework.spring.beans.factory.config.SingletonBeanRegistry;
/**
* @ClassName : AbstractBeanFactory //类名
* @Description : //描述
* @Author : 10300 //作者
* @Date: 2024/4/27 19:31
*/
public abstract class AbstractBeanFactory extends DefaultSingletonBeanRegistry implements BeanFactory {
@Override
public Object getBean(String name) throws BeansException {
Object singleton = this.getSingleton(name);
if (singleton!=null){
return singleton;
}
BeanDefinition beanDefinition = getBeanDefinition(name);
return createBean(name,beanDefinition);
}
protected abstract BeanDefinition getBeanDefinition(String beanName) throws BeansException;
protected abstract Object createBean(String beanName, BeanDefinition beanDefinition) throws BeansException;
}
package org.springframework.spring.beans.factory.support;
import org.springframework.spring.beans.BeansException;
import org.springframework.spring.beans.factory.config.BeanDefinition;
/**
* @ClassName : AbstractAutowireCapableBeanFactory //类名
* @Description : //描述
* @Author : 10300 //作者
* @Date: 2024/4/27 19:36
*/
public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFactory {
public Object createBean(String beanName, BeanDefinition beanDefinition) throws BeansException{
Object bean;
try {
bean = beanDefinition.getBeanClass().newInstance();
} catch (InstantiationException | IllegalAccessException e) {
throw new BeansException("Instantiation of bean failed", e);
}
addSingleton(beanName,bean);
return bean;
}
}
标签:spring,beanName,springframework,beans,org,public From: https://www.cnblogs.com/jichenghui/p/18162441