博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
简单模拟Spring的注入
阅读量:7114 次
发布时间:2019-06-28

本文共 1974 字,大约阅读时间需要 6 分钟。

主要就是读XML技术和反射技术。

在xml中读出相关配置信息,然后利用反射将其实例化为对象,并调用其构造方法,在实例化的过程中将属性注入实例。

实例化和属性注入这些操作都交给了框架,不再需要自己的去new,相当于将控制权交给了框架,称为控制反转(IOC),又称为依赖注入(DI)

 

xml文件:

<beans>

    <bean id="u" class="com.bjsxt.dao.impl.UserDAOImpl" />
    <bean id="userService" class="com.bjsxt.service.UserService" >
        <property name="userDAO" bean="u"/>
    </bean>
</beans>

 

读xml文件:

 

package com.bjsxt.spring;

public interface BeanFactory {

    public Object getBean(String id);
}

 

package com.bjsxt.spring;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jdom.Document;

import org.jdom.Element;
import org.jdom.input.SAXBuilder;

public class ClassPathXmlApplicationContext implements BeanFactory {

   
    private Map<String , Object> beans = new HashMap<String, Object>();
   
   
    //IOC Inverse of Control DI Dependency Injection
    public ClassPathXmlApplicationContext() throws Exception {
        SAXBuilder sb=new SAXBuilder();
       
        Document doc=sb.build(this.getClass().getClassLoader().getResourceAsStream("beans.xml")); //构造文档对象
        Element root=doc.getRootElement(); //获取根元素HD
        List list=root.getChildren("bean");//取名字为disk的所有元素
        for(int i=0;i<list.size();i++){
           Element element=(Element)list.get(i);
           String id=element.getAttributeValue("id");
           String clazz=element.getAttributeValue("class");
           Object o = Class.forName(clazz).newInstance();
           System.out.println(id);
           System.out.println(clazz);
           beans.put(id, o);
          
           for(Element propertyElement : (List<Element>)element.getChildren("property")) {
               String name = propertyElement.getAttributeValue("name"); //userDAO
               String bean = propertyElement.getAttributeValue("bean"); //u
               Object beanObject = beans.get(bean);//UserDAOImpl instance
              
               String methodName = "set" + name.substring(0, 1).toUpperCase() + name.substring(1);
               System.out.println("method name = " + methodName);
              
               Method m = o.getClass().getMethod(methodName, beanObject.getClass().getInterfaces()[0]);
               m.invoke(o, beanObject);
           }
          
          
        } 
     
    }

 

    public Object getBean(String id) {

        return beans.get(id);
    }

}

转载地址:http://yfghl.baihongyu.com/

你可能感兴趣的文章
poj 1129 也算是遍历的吧 两种方法
查看>>
Linux iptables原理和使用
查看>>
179-Geolocation-with-MaxMind-s-GeoIP-and-the-geoip-city-RubyGemInstall
查看>>
开源的杀毒软件
查看>>
irb的子会话 - 相思雨 - 博客园
查看>>
MDCC为移动开发者服务:一看、一聊、一聚
查看>>
python 网络编程
查看>>
aspx 页面数据绑定 前台数据绑定
查看>>
《推荐系统实践》样章:如何利用用户标签数据
查看>>
U-Boot Makefile文件分析
查看>>
Puppetmaster高可用和可扩展的方案设计
查看>>
[2013EJDE]Osgood type regularity criterion for the $3$D Newton-Boussinesq equations
查看>>
黄疸案
查看>>
[转载]ASP.NET伪静态页面的实现和伪静态在IIS7.0中的配置
查看>>
【转】Android源代码编译命令m/mm/mmm/make分析--不错
查看>>
动态规划——最大子段和
查看>>
Linux gtypist
查看>>
如何用Maven创建web项目(具体步骤)
查看>>
MATLAB如何定义函数
查看>>
LIMITS.H
查看>>