栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

JAVA 简单实现Spring IOC 依赖注入原理

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

JAVA 简单实现Spring IOC 依赖注入原理

内容介绍

长片大论的就不说了,不浪费大家的时间,因为网上都是一样的,大家也都知道IOC是什么东西,这部分代码只是描述我对IOC的一个简单的理解,如有问题可以发布你们的看法一起讨论。 代码

    首先模拟spring的两个注解,太多了我就用一个代替了@A ≈ @controller @service @Mapper@B ≈ @autowired @Resource
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface A
{
}
import java.lang.annotation.*;

@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@documented
public @interface B
{
}
    模拟 service 层和 dao层DemoA ≈ serviceDemoB ≈ dao
@A
public class DemoA
{
    @B
    private DemoB demoB;

    public void aa(){
        System.out.println("我是DemoA");
        demoB.bb();
    }
}
@A
public class DemoB
{
    public void bb(){
        System.out.println("我是DemoB");
    }
}
主要代码,Java反射
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class BeanFactoryImpl
{
    //模拟xml配置
    private static final List xmlList = new ArrayList<>();
    //管理IOC bean
    private Map beans = new HashMap<>();

    static
    {
        xmlList.add("com.bp.paypal.demo.Test");
        xmlList.add("com.bp.paypal.demo.DemoA");
        xmlList.add("com.bp.paypal.demo.DemoB");
    }

    public BeanFactoryImpl() throws ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException
    {
        //
        for (String config : xmlList)
        {
            Class clazz = Class.forName(config);
            A annotation = clazz.getAnnotation(A.class);
            if (annotation == null)
            {
                continue;
            }
            beans.put(config, clazz.newInstance());
        }
        //

        //
        for (String config : xmlList)
        {
            Object o = beans.get(config);
            Class aClass = o.getClass();
            Field[] declaredFields = aClass.getDeclaredFields();
            for (Field field : declaredFields)
            {
                B annotation = field.getAnnotation(B.class);
                if (annotation == null)
                {
                    continue;
                }
                field.setAccessible(true);
                field.set(o, beans.get(field.getType().getName()));
            }
        }
        //
    }

    //测试代码 主要是为了获取Test测试类
    public Object getBean(String str)
    {
        return beans.get(str);
    }
}
代码使用
    可以理解controller层调用吧
@A
public class Test
{
    @B
    private DemoA demoA;

    public static void main(String[] args) throws Exception
    {
        BeanFactoryImpl beanFactory = new BeanFactoryImpl();
        Test bean = (Test) beanFactory.getBean(Test.class.getTypeName());
        bean.demoA.aa();
    }
}
结果

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/703197.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号