spring手写v2-di

关键类和关键方法

对象实例化

MyCapableBeanFactory的instantiateBean:将MyBeanDefinition转换为MyBeanWrapper并放入factoryBeanInstance的map中

属性赋值

MyCapableBeanFactory的populateBean给对象的属性设置值

代码

package formework.beans;
public interface MyBeanFactory {
    /**
     * 获取bean实例
     * @param beanName
     * @return
     */
    Object getBean(String beanName);
}

package formework.beans.factory.support;
import formework.annotation.GPAutowired;
import formework.annotation.GPService;
import formework.aop.MyProxyConfig;
import formework.aop.MyProxyFactory;
import formework.beans.MyBeanFactory;
import formework.beans.MyBeanWrapper;
import formework.beans.factory.config.MyBeanDefinition;
import formework.context.MyDefaultListableBeanFactory;
import java.lang.reflect.Field;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
public class MyCapableBeanFactory implements MyBeanFactory {
    //factaryName --> BeanWrapper
    private final Map factoryBeanInstance = new ConcurrentHashMap();
    private MyDefaultListableBeanFactory defaultListableBeanFactory;
    private MyProxyConfig proxyConfig;
    private Properties properties;

    private MyCapableBeanFactory() {
    }

    public MyCapableBeanFactory(MyDefaultListableBeanFactory defaultListableBeanFactory, Properties properties) {
        this.defaultListableBeanFactory = defaultListableBeanFactory;
        this.properties = properties;
    }

    public MyBeanWrapper getBeanWrapper(String beanName){
        return factoryBeanInstance.get(beanName);
    }

    @Override
    public Object getBean(String beanName) {
        if(factoryBeanInstance.containsKey(beanName)){
            return factoryBeanInstance.get(beanName).getWrappedInstance();
        }

        MyBeanDefinition beanDefinition = defaultListableBeanFactory.getBeanDefinition(beanName);
        MyBeanWrapper myBeanWrapper = instantiateBean(beanDefinition);

        populateBean(beanDefinition, myBeanWrapper);

        return myBeanWrapper.getWrappedInstance();
    }

    /**
     * 构建对象
     * @param beanDefinition
     * @return
     */
    private MyBeanWrapper instantiateBean(MyBeanDefinition beanDefinition){
        if(null == beanDefinition){
            return null;
        }

        Object originalInstance = null;
        try {
            originalInstance = Class.forName(beanDefinition.getBeanClassName()).newInstance();
        } catch (Exception e) {
            e.printStackTrace();
        }

        MyProxyFactory proxyFactory = new MyProxyFactory();
        proxyFactory.setInstance(originalInstance);
        proxyFactory.setProxyConfig(getProxyConfig());
        Object wrapperedInstance = originalInstance;
        if(originalInstance.getClass().isAnnotationPresent(GPService.class)){
            wrapperedInstance = proxyFactory.getProxy();
        }

        MyBeanWrapper beanWrapper = new MyBeanWrapper()
                .setWrappedInstance(wrapperedInstance)
                .setOriginalInstance(originalInstance)
                .setWrappedClass(wrapperedInstance.getClass());

        factoryBeanInstance.put(beanDefinition.getFactoryBeanName(), beanWrapper);

        return beanWrapper;
    }

    private MyProxyConfig getProxyConfig(){
        if(proxyConfig != null){
            return proxyConfig;
        }

        MyProxyConfig proxyConfig = new MyProxyConfig()
                .setPointCut(properties.getProperty("pointCut"))
                .setAspectClazz(properties.getProperty("aspectClass"))
                .setAspectBefore(properties.getProperty("aspectBefore"))
                .setAspectAfter(properties.getProperty("aspectAfter"))
                .setAspectAfterThrow(properties.getProperty("aspectAfterThrow"))
                .setAspectAfterThrowingName(properties.getProperty("aspectAfterThrowingName"));
        this.proxyConfig = proxyConfig;
        return proxyConfig;
    }

    /**
     * 设置属性值
     * @param beanDefinition
     * @param beanWrapper
     */
    private void populateBean(MyBeanDefinition beanDefinition, MyBeanWrapper beanWrapper){
        if(null == beanDefinition || null == beanWrapper){
            return;
        }

        //给属性赋值
        Class originalClazz = beanWrapper.getOriginalInstance().getClass();
        for (Field field : originalClazz.getDeclaredFields()) {
            if(null == field.getAnnotation(GPAutowired.class)){
                continue;
            }

            if(!factoryBeanInstance.containsKey(field.getName())){
                getBean(field.getName());
            }

            if(factoryBeanInstance.containsKey(field.getName())){
                try {
                    field.setAccessible(true);
                    field.set(beanWrapper.getOriginalInstance(), factoryBeanInstance.get(field.getName()).getWrappedInstance());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public MyDefaultListableBeanFactory getDefaultListableBeanFactory() {
        return defaultListableBeanFactory;
    }
}

你可能感兴趣的:(spring手写v2-di)