(A5,二)利用java反射机制----手写SpringIOC框架

利用java反射机制—-手写SpringIOC框架


什么是SpringIOC

就是把每一个bean(实体类)与bean(实体了)之间的关系交给第三方容器进行管理。

applicationContext.xml配置:

<?xml version="1.0" encoding="UTF-8"?>
<beans>
    <bean id="user1" class="com.wangys.reflect.UserEntity">
        <property name="userId" value="0001"></property>
        <property name="userName" value="王永圣"></property>
    </bean>
    <bean id="user2" class="com.wangys.reflect.UserEntity">
        <property name="userId" value="0002"></property>
        <property name="userName" value="张晨博"></property>
    </bean>
</beans>

java代码:

         //读取xml配置
        ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        //获取bean对象
        UserEntity bean = (UserEntity) classPathXmlApplicationContext.getBean("user1");
        System.out.printf("使用反射获取bean:" + bean.getUserId() + "---" + bean.getUserName());

什么是SpringIOC底层实现原理

1.读取bean的XML配置文件。
2.使用beanId查找bean配置,并获取配置文件中class地址。
3.使用Java反射技术实例化对象。
4.获取属性配置,使用反射技术进行赋值。
详细步骤:

1.利用传入的参数获取xml文件的流,并且利用dom4j解析成Document对象
2.对于Document对象获取根元素对象后对下面的标签进行遍历,判断是否有符合的id.
3.如果找到对应的id,相当于找到了一个UserEntity 元素,开始创建对象,先获取class属性,根据属性值利用反射建立对象.
4.遍历标签下的property标签,并对属性赋值.注意,需要单独处理int,float类型的属性.因为在xml配置中这些属性都是以字符串的形式来配置的,因此需要额外处理.
5.如果属性property标签有ref属性,说明某个属性的值是一个对象,那么根据id(ref属性的值)去获取ref对应的对象,再给属性赋值.
6.返回建立的对象,如果没有对应的id,或者下没有子标签都会返回null

建立实体类:

public class UserEntity {

    private String userId;
    private String userName;
    public String getUserId() {
        return userId;
    }
    public void setUserId(String userId) {
        this.userId = userId;
    }
    public String getUserName() {
        return userName;
    }
    public void setUserName(String userName) {
        this.userName = userName;
    }
}

使用反射技术完成Java代码:

public class ClassPathXmlApplicationContext {
    private String xmlPath;

    /**
     *
     * @param xmlPath
     *            spring xml 配置路径
     */
    public ClassPathXmlApplicationContext(String xmlPath) {
        this.xmlPath = xmlPath;
    }

    public Object getBean(String beanId) throws Exception {

        // 解析xml器
        SAXReader saxReader = new SAXReader();
        Document read = null;
        try {
            // 从项目根目录路径下 读取
            read = saxReader.read(this.getClass().getClassLoader().getResourceAsStream(xmlPath));
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (read == null) {
            return null;
        }
        // 获取根节点资源
        Element root = read.getRootElement();
        List<Element> elements = root.elements();
        if (elements.size() <= 0) {
            return null;
        }
        Object oj = null;
        for (Element element : elements) {
            String id = element.attributeValue("id");
            if (StringUtils.isEmpty(id)) {
                return null;
            }
            if (!id.equals(beanId)) {
                continue;
                // throw new Exception("使用beanId:" + beanId + ",未找到该bean");
            }
            // 获取实体bean class地址
            String beanClass = element.attributeValue("class");
            // 使用反射实例化bean
            Class<?> forNameClass = Class.forName(beanClass);
            oj = forNameClass.newInstance();
            // 获取子类对象
            List<Element> attributes = element.elements();
            if (attributes.size() <= 0) {
                return null;
            }
            for (Element et : attributes) {
                // 使用反射技术为方法赋值
                String name = et.attributeValue("name");
                String value = et.attributeValue("value");
                Field field = forNameClass.getDeclaredField(name);
                field.setAccessible(true);
                field.set(oj, value);

            }
        }
        return oj;
        // 1.使用beanId查找配置文件中的bean。
        // 2.获取对应bean中的classpath配置
        // 3.使用java反射机制实体化对象
    }
    public static void main(String[] args) throws Exception {
        ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext(
                "applicationContext.xml");
        UserEntity bean = (UserEntity) applicationContext.getBean("user1");
        System.out.println("使用反射获取bean:" + bean.getUserId() + "---" + bean.getUserName());
    }
}

运行结果:
这里写图片描述

Maven坐标

<dependencies>
        <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
        <dependency>
            <groupId>org.dom4j</groupId>
            <artifactId>dom4j</artifactId>
            <version>2.0.0</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>5.0.2.RELEASE</version>
        </dependency>
    </dependencies>
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值