(一)spring beans

辉辉、發表於2024-04-27

(一)spring beans

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;
    }
}

相關文章