springIOC实现原理

`xml文件中的引入bean``
public class User {

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;
}

}


实现步骤:1.读取xml所在的位置
                  2.获取bean的位置 即class的位置
                  3.利用类的反射机制为其私有属性赋值(看蚂蚁课堂的感悟)
代码如下`
public class ClassPathXmlApplicationContext {
    private String pathXml = null;

    public ClassPathXmlApplicationContext(String pathXml) {
        this.pathXml = pathXml;
    }

    public Object getBean(String beanId) throws Exception {
        if (StringUtils.isEmpty(beanId)) {
            throw new Exception("beanId is null");
        }
        SAXReader saxReader = new SAXReader();
        Document read = saxReader.read(this.getClass().getClassLoader().getResource(pathXml));
        // 获取到根节点
        Element rootElement = read.getRootElement();
        // 根节点下所有的子节点
        List<Element> elements = rootElement.elements();
        for (Element element : elements) {
            // 获取到节点上的属性
            String id = element.attributeValue("id");
            if (StringUtils.isEmpty(id)) {
                continue;
            }
            if (!id.equals(beanId)) {
                continue;
            }

            // 使用java反射机制初始化对象
            String beanClass = element.attributeValue("class");
            Class<?> forName = Class.forName(beanClass);
            Object newInstance = forName.newInstance();
            List<Element> propertyElementList = element.elements();
            for (Element el : propertyElementList) {
                String name = el.attributeValue("name");
                String value = el.attributeValue("value");
                Field declaredField = forName.getDeclaredField(name);
                //此方法必须打开才能给私有属性赋值(注意)
                declaredField.setAccessible(true);
                declaredField.set(newInstance, value);
            }
            return newInstance;



        }
        return null;
    }
    

测试代码:
      public static void main(String[] args) throws Exception {
            ClassPathXmlApplicationContext classPath = new ClassPathXmlApplicationContext("applicationContext.xml");
            User user = (User) classPath.getBean("user1");
            System.out.println(user.getUserId() + "---" + user.getUserName());
        }
xml文件
<beans>
    <bean id="user1" class="com.Use">
        <property name="userId" value="0001"></property>
        <property name="userName" value="zz"></property>
    </bean>
    <bean id="user2" class="com.User">
        <property name="userId" value="0002"></property>
        <property name="userName" value="ss"></property>
    </bean>
</beans>
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值