【仿写spring之ioc篇】四、实现bean的初始化阶段

BeanPostProcessor

在Bean的初始化阶段有前置和后置方法,这个方法是通过BeanPostProcessor来管理的,下面我们对原有的项目结构做小小的更改。
【仿写spring之ioc篇】四、实现bean的初始化阶段_第1张图片
对启动类作出修改,先检查有没有BeanPostProcessor的实现类,有的话就使用,没有就使用默认的。

		// 2.判断是否是postProcessor
        for (String name : allJavaFile) {
            Class<?> clazz = beanScanner.getClazz(name);
            if (beanScanner.isImplBeanPostProcessor(clazz)){
                Object bean = beanFactory.createBean(clazz);
                beanPostProcessor = (BeanPostProcessor) bean;
                beanFactory.getCreatedBeanSet().add(bean);
            }
        }
        if (beanPostProcessor == null){
            beanPostProcessor = new DefaultBeanPostProcessor();
        }

第二次循环先检查是不是postProcessor,是的话就跳过就行

// 3.判断是否有Component
        for (String name : allJavaFile) {
            if (beanScanner.isComponent(name)) {
                // 4.是Component就开始bean的生命周期
                Class<?> clazz = beanScanner.getClazz(name);
                boolean flag = false;
                for (Object o : beanFactory.getCreatedBeanSet()) {
                    if (o.getClass().equals(clazz)) {
                        flag = true;
                        break;
                    }
                }
                if (flag){
                    continue;
                }
                Object bean = beanFactory.createBean(clazz);
                String beanName = beanFactory.parseBeanName(bean);
                // 查看@Value注解
                // 1.找到所有@Value的字段
                ......

最后执行这些代码

				// init
                beanFactory.getBeanMap().put(beanName,bean);
                beanPostProcessor.postProcessBeforeInitialization(bean);
                beanFactory.doInitialization(bean,beanScanner);
                beanPostProcessor.postProcessAfterInitialization(bean);

BeanFactory

这些方法都大差不差,思路就是找注解/接口,去invoke执行方法

/**
     * 进行初始化
     *
     * @param bean 豆
     * @throws Exception 例外
     */
    public void doInitialization(Object bean, BeanScanner beanScanner) throws Exception {
        // @PostConstruct
        List<Method> postConstruct = beanScanner.getPostConstruct(bean);
        for (Method method : postConstruct) {
            method.invoke(bean);
        }
        // InitializingBean
        if (beanScanner.isImplInitializing(bean)) {
            doInitialize(bean);
        }
    }

测试类

package com.ez4sterben.spring.test;

import com.ez4sterben.spring.ioc.annotation.Component;
import com.ez4sterben.spring.ioc.factory.BeanPostProcessor;

/**
 * 测试bean后处理器
 *
 * @author ez4sterben
 * @date 2023/09/02
 */
@Component
public class TestBeanPostProcessor implements BeanPostProcessor {
    @Override
    public Object postProcessBeforeInitialization(Object bean) {
        System.out.println("postProcessBeforeInitialization");
        return BeanPostProcessor.super.postProcessBeforeInitialization(bean);
    }

    @Override
    public Object postProcessAfterInitialization(Object bean) {
        System.out.println("postProcessAfterInitialization");
        return BeanPostProcessor.super.postProcessAfterInitialization(bean);
    }
}

package com.ez4sterben.spring.test;

import com.ez4sterben.spring.ioc.annotation.Component;
import com.ez4sterben.spring.ioc.annotation.Value;
import com.ez4sterben.spring.ioc.context.ApplicationContext;
import com.ez4sterben.spring.ioc.factory.BeanPostProcessor;
import com.ez4sterben.spring.ioc.factory.InitializingBean;
import com.ez4sterben.spring.ioc.factory.aware.ApplicationContextAware;
import com.ez4sterben.spring.ioc.factory.aware.BeanFactoryAware;
import com.ez4sterben.spring.ioc.factory.aware.BeanNameAware;
import com.ez4sterben.spring.ioc.factory.support.BeanFactory;
import lombok.Data;

import javax.annotation.PostConstruct;

/**
 * 测试组件
 *
 * @author ez4sterben
 * @date 2023/09/01
 */
@Component
@Data
public class TestComponent implements BeanNameAware, BeanFactoryAware, ApplicationContextAware, InitializingBean {

    @Value("ez4sterben")
    private String name;

    private String age;

    @Override
    public void setBeanName(String beanName) {
        System.out.println(beanName);
    }

    @PostConstruct
    public void init(){
        System.out.println("postConstruct");
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) {
        System.out.println(applicationContext.getBeanMap().toString());
    }

    @Override
    public void setBeanFactory(BeanFactory beanFactory) {
        System.out.println(beanFactory.getBeanMap().toString());
    }

    @Override
    public void afterPropertiesSet() {
        System.out.println("afterPropertiesSet");
    }
}

结果
【仿写spring之ioc篇】四、实现bean的初始化阶段_第2张图片

你可能感兴趣的:(源码仿写,spring,java,反射,代理)