手写IoC容器

目录

1、Spring IoC

2、反射

3、工厂模式

4、手写IoC容器

4.1 解析xml,获得class路径

4.2 创建工厂

4.3 启动类

4.4 实体类


1、Spring IoC

        为什么要使用IoC,原因是之前的对象都是通过手动new的方式进行创建,代码会比较冗余,并且没有统一的管理。而使用了Spring,把对象的创建过程与使用统一都交给我们的Spring来进行管理。IoC容器的底层实现技术:反射+解析spring.xml+工厂模式。

2、反射

        JAVA反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法;对于任意一个对象,都能够调用它的任意一个方法和属性;这种动态获取的信息以及动态调用对象的方法的功能称为java语言的反射机制。要想解剖一个类,必须先要获取到该类的字节码文件对象。而解剖使用的就是Class类中的方法.所以先要获取到每一个字节码文件对应的Class类型的对象。

3、工厂模式

        使用工厂模式可以降低代码的耦合度,统一的管理和维护我们每个对象创建于使用的过程。

基于Spring MVC 开发模式进行简单的演示。

/**
* 数据库操作层
*/
public class UserDao(){
	//增加数据
    public void addUser(){
        ...
    } 
}
/**
* User对象工厂
*/
public class UserDaoFactory(){
    public static UserDao getUserDao(){
        return new UserDao();
    }
}
/**
* 业务层
*/
public class UserService(){
    public void addUser(){
        UserDaoFactory.getUserDao().addUser();
    }
}

4、手写IoC容器

xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="Account" class="com.mingfa.entity.Account"></bean>

</beans>

4.1 解析xml,获得class路径

package com.mingfa;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.net.MalformedURLException;

public class Dom4jAccount {
    public String getAccountClass() throws MalformedURLException, DocumentException {
        File xmlFile =  new File(this.getClass().getResource("/")+"spring.xml");

        SAXReader saxReader = new SAXReader();
        Document document = saxReader.read(xmlFile);

        //开始解析
        //获取根节点
        Element rootElement = document.getRootElement();
        //获取bean结点
        Element bean = rootElement.element("bean");
        //获取calss的属性值
        String aClass = bean.attributeValue("class");
        return aClass;
    }
}

4.2 创建工厂

package com.mingfa.Factory;

import com.mingfa.Dom4jAccount;
import com.mingfa.entity.Account;
import org.dom4j.DocumentException;
import java.net.MalformedURLException;

public class AccountFactory {

    public static Account getAccountEntity() throws ClassNotFoundException, IllegalAccessException, InstantiationException, MalformedURLException, DocumentException {
        //解读xml文件
        //1、获取<bean id="" class=""/>类的完整路径地址
        String accountClass = new Dom4jAccount().getAccountClass();
        //2、使用到反射技术初始化对象
        Class<?> aClass = Class.forName(accountClass);
        //默认执行无参数构造函数
        //3、使用工厂模式封装初始化对象
        Account account = (Account) aClass.newInstance();
        return account;
    }
}

4.3 启动类

package com.mingfa;

import com.mingfa.Factory.AccountFactory;
import org.dom4j.DocumentException;

import java.net.MalformedURLException;

public class test {
    public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException, MalformedURLException, DocumentException {
        AccountFactory.getAccountEntity().testIoC();
    }
}

4.4 实体类

package com.mingfa.entity;

import lombok.Data;

//@Data
public class Account {
    private String cno;
    private String name;
    private String CLASS;

    public void testIoC(){
        System.out.println("这是Account对象");

    }
}

其实实现IoC容器机制是比较简单的,难就难在如何理解IoC以及如何进行实际的开发应用。读者可以日后多多深挖。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值