Spring初探之手写依赖注入

本文介绍了Spring框架中的依赖注入(DI)原理,通过手动实现BeanDefinition和BeanFactory来阐述DI过程。利用Java反射机制,详细解析了DI的实现步骤,并提供了测试类TestMain的示例。阅读本文旨在帮助读者深入理解Spring DI的核心概念。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

要想实现依赖注入,首先我们需要在BeanDefined中定义属性的键值对:

	//存放属性键值对
    private Map<String, String> propertyMap = new HashMap<>(16);

在BeanFactory中返回实例化对象前,给对象赋值。
实现setvalue方法:

private void setValue(Object instance, Class beanField, Map<String, String> propertyMap) throws Exception {
        //获取所有的方法
        Method[] methods = beanField.getDeclaredMethods();
        //对properMap进行解析,获取键值对,也就是需要注入的属性和属性值
        Set<String> propertySet = propertyMap.keySet();
        Iterator<String> propertyIterator = propertySet.iterator();
        while (propertyIterator.hasNext()){
            String propertyKey = propertyIterator.next();
            String propertyValue = propertyMap.get(propertyKey);
            //获取的属性
            Field field = beanField.getDeclaredField(propertyKey);
            //属性对应的set方法
            String methodName = "set" + field.getName();
            //对方法进行过滤
            for (int i = 0; i < methods.length; i ++){
                Method method = methods[i];
                if (methodName.equalsIgnoreCase(method.getName())){
                    //判断set方法的类型 也就是属性的类型
                    Class fieldType = field.getType();
                    if (fieldType == String.class){
                        method.invoke(instance, propertyValue);
                    } else if (fieldType == Integer.class){
                        method.invoke(instance, Integer.valueOf(propertyValue));
                        //这里只对List类型做判断
                    } else if (fieldType == List.class){
                        List<String> tmpList = new ArrayList<>(16);
                        String[] itemArray = propertyValue.split(",");
                        for (int j = 0; j < itemArray.length; j ++){
                            tmpList.add(itemArray[i]);
                        }
                    }else {
                        throw new RuntimeException("暂不该支持属性类型注入");
                    }
                }
            }
        }
    }

实现逻辑不难,就是一层一层解析,基本步骤可以看注解。
测试类TestMain

 @Test
    public void testPropertyMap() throws Exception {
        //注册bean
        BeanDefined beanObj = new BeanDefined();
        beanObj.setBeanId("user");
        beanObj.setClassPath("com.lks.bean.User");

        //设置propertyMap 类似Spring中依赖注入属性值
        Map<String, String> propertyMap = beanObj.getPropertyMap();
        propertyMap.put("name", "萧峰");
        propertyMap.put("age", "31");
        propertyMap.put("sex", "男");

        List<BeanDefined> beanDefineds = new ArrayList<>(16);
        beanDefineds.add(beanObj);

        //声明BeanFactory,类似于Spring中的ApplicationContext
        BeanFactory factory = new BeanFactory(beanDefineds);

        //实际调用
        User user = (User) factory.getPropertyBean("user");
        System.out.println(user.toString());
    }

该章节内容是在上一章的基础上实现的,主要是为了对 Spring 依赖注入的基本实现有一个了解和掌握,从上面的实例中就可以看出,Spring的依赖注入主要依赖Java的反射机制。
代码地址:https://github.com/kaisongli/spring-demo.git

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值