手写一个spring模拟,实现扫描component、依赖注入、BeanPostProcessor、AOP流程

这是一个简化的Spring框架实现示例,包括组件扫描、依赖注入、初始化处理和AOP代理。`MyApplicationContext`类负责加载配置类,扫描带有`Component`注解的类,并创建bean。`BeanDefinition`存储bean信息,`ComponentScan`、`Scope`、`AutoWired`定义bean的元数据。`UserService`和`OrderService`作为示例bean,实现了`InitializingBean`接口并使用`AutoWired`注解进行依赖注入。`MyBeanPostProcessor`实现了`BeanPostProcessor`接口,用于在bean初始化前后进行处理,展示了AOP代理的概念。

 

代码结构:

myspring代码

package com.myspring;

import java.io.File;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class MyApplicationContext {

    private Class configClass;

    private ConcurrentHashMap<String, Object> singletonObjects = new ConcurrentHashMap<>();

    //  所有bean的定义
    private ConcurrentHashMap<String, BeanDefinition> beanDefinitions= new ConcurrentHashMap<>();

    // beanPostProcessor类型
    private List<BeanPostProcessor> beanPostProcessors = new ArrayList<>();

    public MyApplicationContext(Class configClass) {
        // 扫描component
        setConfigs(configClass);

        for (Map.Entry<String, BeanDefinition> entry : beanDefinitions.entrySet()) {
            String beanName = entry.getKey();
            BeanDefinition beanDefinition =  entry.getValue();
            if (beanDefinition.getScope().equals("singleton")) {
                Object bean = createBean(beanDefinition);
                singletonObjects.put(beanName, bean);
            }
        }
    }

    private void setConfigs(Class configClass) {
        this.configClass = configClass;
        // 解析配置类
        // 拿到注解,扫描路径
        ComponentScan annotation = (ComponentScan) configClass.getDeclaredAnnotation(ComponentScan.class);
        String path = annotation.value();
        path = path.replace(".", "/");
        // 扫描
        ClassLoader classLoader = MyApplicationContext.class.getClassLoader();
        URL url = classLoader.getResource(path);
        File file = new File(url.getFile());
        if (file.isDirectory()) {
            File[] files = file.listFiles();
            for (File f : files) {
                //  把target包里面的class类名取出来。
                // 把E:\mySpring\target\classes\com\service\UserService.class转化为com.service.UserService
                String fileName = f.getAbsolutePath();
                if (fileName.endsWith(".class")) {
                    int comIndex = fileName.indexOf("com");
                    int classIndex = fileName.indexOf(".class");
                    String className = fileName.substring(comIndex, classIndex);

                    className = className.replace("\\", ".");
                    Class<?>aClass = null;
                    try {
                        aClass = classLoader.loadClass(className);
                    } catch (ClassNotFoundException e) {
                        e.printStackTrace();
                    }
                    if (aClass.isAnnotationPresent(Component.class)) {
                        // 判断当前class是否实现了BeanPostProcessor这个接口
                        if (BeanPostProcessor.class.isAssignableFrom(aClass)) {
                            try {
                                BeanPostProcessor instance = (BeanPostProcessor) aClass.getDeclaredConstructor().newInstance();
                                beanPostProcessors.add(instance);
                            } catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
                                e.printStackTrace();
                            }
                        }

                        // 这个是一个bean
                        // 判断是单例bean 还是原型bean
                        Component component = aClass.getDeclaredAnnotation(Component.class);
                        String beanName = component.value();
                        String scope = "";
                        if (aClass.isAnnotationPresent(Scope.class)) {
                            scope = aClass.getDeclaredAnnotation(Scope.class).value();
                        }
                        // 放入bean的定义
                        BeanDefinition beanDefinition = new BeanDefinition(aClass, scope);
                        beanDefinitions.put(beanName, beanDefinition);
                    }
                }
            }
        }
    }

    private Object createBean(BeanDefinition beanDefinition) {
        Class clazz = beanDefinition.getClazz();
        try {
            Object instance = clazz.getDeclaredConstructor().newInstance();
            // 依赖注入
            for(Field field : clazz.getDeclaredFields()) {
                if (field.isAnnotationPresent(AutoWired.class)) {
                    Object bean = getBean(field.getName());
                    field.setAccessible(true);
                    field.set(instance, bean);
                }
            }

            for (BeanPostProcessor beanPostProcessor: beanPostProcessors) {
                instance = beanPostProcessor.postProcessBeforeInitialization(instance, clazz.getName());
            }

            // 初始化实现
            if (instance instanceof InitializingBean) {
                InitializingBean initializingBean = (InitializingBean) instance;
                initializingBean.afterPropertiesSet();
            }
            // 初始化后
            for (BeanPostProcessor beanPostProcessor: beanPostProcessors) {
                // 返回来的对象不一定是原来的对象,比如可能是AOP的代理对象
                instance = beanPostProcessor.postProcessAfterInitialization(instance, clazz.getName());
            }
            return instance;
        } catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
            e.printStackTrace();
        }
        return null;
    }

    public Object getBean(String beanName) {
        if (beanDefinitions.containsKey(beanName)) {
            BeanDefinition beanDefinition =  beanDefinitions.get(beanName);
            if (beanDefinition.getScope().equals("singleton")) {
                return singletonObjects.get(beanName);
            } else {
                // 原型 ,创建Bean
                return createBean(beanDefinition);
            }
        } else {
            System.out.println("aaa");
        }
        return null;
    }
}
package com.myspring;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface Component {
    String value(); // bean name
}
package com.myspring;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/*@Retention作用是定义被它所注解的注解保留多久,一共有三种策略,定义在RetentionPolicy枚举中.

从注释上看:

source:注解只保留在源文件,当Java文件编译成class文件的时候,注解被遗弃;被编译器忽略,比如override

class:注解被保留到class文件,但jvm加载class文件时候被遗弃,这是默认的生命周期,要在编译时进行一些预处理操作,比如生成一些辅助代码

runtime:注解不仅被保存到class文件中,jvm加载class文件之后,仍然存在。一般如果需要在运行时去动态获取注解信息,那只能用 RUNTIME 注解*/
@Retention(RetentionPolicy.RUNTIME)
// @Target说明了Annotation所修饰的对象范围:Annotation可被用于 packages、types(类、接口、枚举、Annotation类型)、类型成员(方法、构造方法、成员变量、枚举值)、
// 方法参数和本地变量(如循环变量、catch参数)。在Annotation类型的声明中使用了target可更加明晰其修饰的目标。
/*      TYPE	可用于类或者接口上
        FIELD	可用于域上
        METHOD	可用于方法上
        PARAMETER	可用于参数上
        CONSTRUCTOR	可用于构造方法上
        LOCAL_VARIABLE	可用于局部变量上
        ANNOTATION_TYPE	可用于注解类型上(被interface修饰的类型)
        PACKAGE	用于记录java文件的package信息*/
@Target(ElementType.TYPE)
@Scope("singleton")
public @interface ComponentScan {
    String value(); // 扫描路径
}
package com.myspring;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface Scope {
    String value(); // 单例
}
package com.myspring;

public class BeanDefinition {
    private Class clazz;

    private String scope;

    public BeanDefinition(Class clazz, String scope) {
        this.clazz = clazz;
        this.scope = scope;
    }

    public Class getClazz() {
        return clazz;
    }

    public void setClazz(Class clazz) {
        this.clazz = clazz;
    }

    public String getScope() {
        return scope;
    }

    public void setScope(String scope) {
        this.scope = scope;
    }
}
package com.myspring;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.FIELD})
public @interface AutoWired {
}
package com.myspring;

public interface InitializingBean {
    void afterPropertiesSet();
}
package com.myspring;

public interface BeanPostProcessor {
    Object postProcessBeforeInitialization(Object bean, String beanName);

    Object postProcessAfterInitialization(Object bean, String beanName);

}

service

package com.service;

public interface UserInterface {
    void test();
}
package com.service;

import com.myspring.AutoWired;
import com.myspring.Component;
import com.myspring.InitializingBean;
import com.myspring.Scope;

@Component("userService")
@Scope("singleton")
public class UserService implements InitializingBean, UserInterface{

    @AutoWired
    OrderService orderService;

    private String name;

    @Override
    public void test() {
        System.out.println("业务逻辑");
    }

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

    public void setName(String name) {
        this.name = name;
    }
}
package com.service;

import com.myspring.Component;
import com.myspring.Scope;

@Component("orderService")
@Scope("singleton")
public class OrderService {
}
package com.service;

import com.myspring.BeanPostProcessor;
import com.myspring.Component;
import com.myspring.Scope;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

// 一个特殊的bean
@Component("myBeanPostProcessor")
public class MyBeanPostProcessor implements BeanPostProcessor {
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) {
        System.out.println("初始化" + beanName);
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) {
        // AOP
        if (beanName.equals("com.service.UserService")) {
            UserInterface proxyInstance = (UserInterface)Proxy.newProxyInstance(MyBeanPostProcessor.class.getClassLoader(), bean.getClass().getInterfaces(), new InvocationHandler() {
                @Override
                public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                    System.out.println("代理逻辑");
                    return method.invoke(bean, args);
                }
            });
            return proxyInstance;
        }
        return bean;
    }
}

测试函数

import com.myspring.ComponentScan;

@ComponentScan("com.service")
public class AppConfig {
}
import com.myspring.MyApplicationContext;
import com.service.UserInterface;

public class Main {
    public static void main(String[] args) {

        MyApplicationContext applicationContext = new MyApplicationContext(AppConfig.class);
        UserInterface userService = (UserInterface) applicationContext.getBean("userService");
        System.out.println(userService);
        userService.test();
    }

}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值