[Java reflection combat] A Spring IoC is implemented by hand

Manually implement a simple Spring IoC container to manage Beans

Note: If you are not familiar with Java reflection, it is recommended to take a look : [Java Advanced] Reflection Learning II-Reflection

1. Method 1: Simple version, when initializing, manually new and then set into hashMap

  • key: the class of the interface
  • value: the instance of the implementation class corresponding to the interface

Test: Obtain directly from HashMap by key name

2. Method 2: Improved version, initialize bean through configuration file + reflection

Configuration file content: interface = implementation class

Read the content of the configuration file through the class loader, then reflect and instantiate it, and set it into the HashMap

3. Method 3: Annotate + scan all files (interface + implementation class) under the class path, instantiate by reflection, and put them in a HashMap memory (IOC container)

3.1 Annotation definition

Custom dependency injection AutoWired annotation

package com.xinzhi.Annotation;

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

/**
 * 自动注入的注解
 * @author zn
 * @date 2020/4/2
 **/
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface AutoWired {
}

Custom bean annotation, only scan the class marked with the annotation

package com.xinzhi.Annotation;

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

/**
 * @author zn
 * @date 2020/4/2
 **/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface Bean {
}

3.2 Scanning file analysis and annotation, container initialization

package com.xinzhi.reflect;

import com.xinzhi.Annotation.AutoWired;
import com.xinzhi.Annotation.Bean;

import java.io.File;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

/**
 * @author zn
 * @date 2020/4/2
 **/
public class ApplicationContext<T> {

    private HashMap<Class,Object> beanFactory = new HashMap<>();
    private String filePath;

    //获取容器的bean
    public T getBean(Class clazz){
        return (T)beanFactory.get(clazz);
    }

    //初始化容器:方式二,读取配置文件+反射
    public void initContext(){
        InputStream resource = ApplicationContext.class.getClassLoader()
                .getResourceAsStream("config/bean.config");
        Properties properties = new Properties();
        try {
            properties.load(resource);
            Set<Object> keys = properties.keySet();
            for (Object key : keys) {
                beanFactory.put(Class.forName(key.toString()),
                        Class.forName(properties.getProperty(key.toString())).newInstance()  );
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    //初始化容器:方式三,扫描+反射
    public  void initContextByAnnotation()  {
        filePath = ApplicationContext.class.getClassLoader().getResource("").getFile();
        //扫描加载类
        loadOne(new File(filePath));
        //属性注入
        assembleObject();
    }

    //是不是给所有的字符赋值
    private void assembleObject() {
        for(Map.Entry<Class,Object> entry : beanFactory.entrySet()){
            //就是咱们放在容器的对象
            Object obj = entry.getValue();
            Class<?> aClass = obj.getClass();
            Field[] declaredFields = aClass.getDeclaredFields();
            for (Field field : declaredFields){
                AutoWired annotation = field.getAnnotation(AutoWired.class);
                if( annotation != null ){
                    field.setAccessible(true);
                    try {
                        System.out.println("正在给【"+obj.getClass().getName()+"】属性【" + field.getName() + "】注入值【"+ beanFactory.get(field.getType()).getClass().getName() +"】");
                        field.set(obj,beanFactory.get(field.getType()));
                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

    /**
     * 加载一个文件夹下的类,递归
     * @param fileParent
     */
    private  void loadOne(File fileParent) {
        if (fileParent.isDirectory()) {
            File[] childrenFiles = fileParent.listFiles();
            if(childrenFiles == null || childrenFiles.length == 0){
                return;
            }
            for (File child : childrenFiles) {
                if (child.isDirectory()) {
                    //如果是个文件夹就继续调用该方法,使用了递归
                    loadOne(child);
                } else {
                    //通过文件路径转变成全类名,第一步把绝对路径部分去掉
                    //  D:\mytools
                    //  com\xinzhi\dao\UserDao.class
                    String pathWithClass = child.getAbsolutePath().substring(filePath.length() - 1);
                    //选中class文件
                    if (pathWithClass.contains(".class")) {
                        //    com.xinzhi.dao.UserDao
                        //去掉.class后缀,并且把 \ 替换成 .
                        String fullName = pathWithClass.replaceAll("\\\\", ".").replace(".class", "");
                        try {
                            Class<?> aClass = Class.forName(fullName);

                            //把非接口的类实例化放在map中
                            if(!aClass.isInterface()){
                                Bean annotation = aClass.getAnnotation(Bean.class);
                                if(annotation != null){
                                    Object instance = aClass.newInstance();
                                    //判断一下有没有接口
                                    if(aClass.getInterfaces().length > 0) {
                                        //如果有接口把接口的class当成key,实例对象当成value
                                        System.out.println("正在加载【"+ aClass.getInterfaces()[0] +"】,实例对象是:" + instance.getClass().getName());
                                        beanFactory.put(aClass.getInterfaces()[0], instance);
                                    }else{
                                        //如果有接口把自己的class当成key,实例对象当成value
                                        System.out.println("正在加载【"+ aClass.getName() +"】,实例对象是:" + instance.getClass().getName());
                                        beanFactory.put(aClass, instance);
                                    }
                                }
                            }
                        } catch (ClassNotFoundException | IllegalAccessException | InstantiationException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
    }
}

Link: https://www.jianshu.com/p/881dff233d43

https://www.bilibili.com/video/BV1RA411t7bA?p=7

●The strongest Tomcat8 performance optimization in history

Why can Alibaba resist 10 billion in 90 seconds? --The evolution of server-side high-concurrency distributed architecture

B2B e-commerce platform--ChinaPay UnionPay electronic payment function

Learn Zookeeper distributed lock, let interviewers look at you with admiration

SpringCloud e-commerce spike microservice-Redisson distributed lock solution

Check out more good articles, enter the official account--please me--excellent in the past

A deep and soulful public account 0.0

Guess you like

Origin blog.csdn.net/a1036645146/article/details/111035421