Java代码实现依赖注入-阿里云开发者社区

开发者社区> 开发与运维> 正文

Java代码实现依赖注入

简介:
这里将模仿Spring实现一种基于xml配置文件的依赖注入机制。文件中将实现3中注入,一是单值注入,包括int,floatdoublechar等,也包括String注入;二是Java容器注入,包括ListSetMap三种容器的注入,最后一种是java bean对象注入。
实现的机制是,使用Dom4jxml配置文件进行解析,这里使用dom4jElement Handler机制,一种类似与责任链模式的实现机制;对于java对象的构建使用反射机制,这里主要是针对得到的类的Field进行set赋值。我试图通过调用Methodinvoke方法调用类本身的setter方法,但是由于通过xml解析得到的值都是String,如果将这些String动态的转换为相应的确定类型是个难点,Methodinvoke方法,如果形参是int,而传入java.lang.Integer,它不会认,所以尝试失败,只能通过Fieldset方法传入特定值。
 
配置文件setting.xml
<?xml version="1.0" encoding="UTF-8"?>
 
<beans>
    <bean id="me" class="com.zj.ioc.di.imp.Person">
       <property name="name">
           <value>ZJ</value>
       </property>
       <property name="age">
           <value>26</value>
       </property>
       <property name="height">
           <value>1.78</value>
       </property>
    </bean>
    <bean id="you" class="com.zj.ioc.di.imp.Person">
       <property name="name">
           <value>Mary</value>
       </property>
       <property name="age">
           <value>27</value>
       </property>
       <property name="height">
           <value>1.66</value>
       </property>
    </bean>
    <bean id="myList" class="com.zj.ioc.di.imp.ListOne">
       <property name="msg">
           <list>
              <value>java</value>
              <value>c</value>
              <value>windows</value>
           </list>
       </property>
    </bean>
    <bean id="mySet" class="com.zj.ioc.di.imp.SetOne">
       <property name="msg">
           <set>
              <value>tom</value>
              <value>cat</value>
              <value>dog</value>
           </set>
       </property>
    </bean>
    <bean id="myMap" class="com.zj.ioc.di.imp.MapOne">
       <property name="msg">
           <map>
              <entry key="c">
                  <value>CHINA</value>
              </entry>
              <entry key="j">
                  <value>JAPAN</value>
              </entry>
              <entry key="k">
                  <value>KOREA</value>
              </entry>
           </map>
       </property>
    </bean>
    <bean id="us" class="com.zj.ioc.di.imp.Persons">
       <property name="i">
           <ref bean="me" />
       </property>
       <property name="u">
           <ref bean="you" />
       </property>
    </bean>
</beans>
 
依据setting.xml,这里将构建两个Person类的实例meyou
Person.java
package com.zj.ioc.di.imp;
 
public class Person {
    private String name;
    private int age;
    private float height;
 
    public String getName() {return name;}
    public void setName(String name) {this.name = name;}
    public int getAge() {return age;}
    public void setAge(int age) {this.age = age;}
    public float getHeight() {return height;}
    public void setHeight(float height) {this.height = height;}
}
紧接着,构建一个ListOne的实例myList
ListOne.java
package com.zj.ioc.di.imp;
import java.util.List;
 
public class ListOne {
    private List<String> msg;
 
    public List<String> getMsg() {return msg;}
    public void setMsg(List<String> msg) {this.msg = msg;}
}
紧接着,构建一个SetOne的实例mySet
SetOne.java
package com.zj.ioc.di.imp;
import java.util.Set;
 
public class SetOne {
    private Set<String> msg;
 
    public Set<String> getMsg() {return msg;}
    public void setMsg(Set<String> msg) {this.msg = msg;}
}
紧接着,构建一个MapOne的实例myMap
MapOne.java
package com.zj.ioc.di.imp;
import java.util.Map;
 
public class MapOne {
    private Map<String,String> msg;
 
    public Map<String, String> getMsg() {return msg;}
    public void setMsg(Map<String, String> msg) {this.msg = msg;}
}
最后构建一个Persons类的实例us,其中包含meyou两个已经构建好的对象:
Persons.java
package com.zj.ioc.di.imp;
 
public class Persons {
    private Person i;
    private Person u;
   
    public Person getI() {return i;}
    public void setI(Person i) {this.i = i;}
    public Person getU() {return u;}
    public void setU(Person u) {this.u = u;}
}
 
主要的实现机制是(代码BeanFactory.java以及工程见附件),
1.通过一个HashMap保存构造好的对象,key就是beanid属性,value就是这个对象;
private Map<String, Object> beanMap = new HashMap<String, Object>();
……
public Object getBean(String beanId) {
    Object obj = beanMap.get(beanId);
    return obj;
}
查询时
BeanFactory factory = new BeanFactory();
factory.init("setting.xml");
Person p1 = (Person) factory.getBean("me");
 
2.init方法读入配置文件setting.xml,并直接定位到beans下的bean元素,并实例化一个ElementHandler对其处理。
public void init(String xmlUri) throws Exception {
    SAXReader saxReader = new SAXReader();
    File file = new File(xmlUri);
    try {
       saxReader.addHandler("/beans/bean"new BeanHandler());
       saxReader.read(file);
    catch (DocumentException e) {
       System.out.println(e.getMessage());
    }
}
 
3.ElementHandlerdom4jElementHandler接口有两个方法,一个是onStart(),它主要用于处理该元素的属性以及动态增加新的Handler类;另一个是onEnd(),它主要用于获得该元素的Text文本以及删除已添加的Handler
BeanHandler
private class BeanHandler implements ElementHandler {
    Object obj = null;
 
    public void .Start(ElementPath path) {
       Element beanElement = path.getCurrent();
       Attribute classAttribute = beanElement.attribute("class");
 
       Class<?> bean = null;
       try {
           bean = Class.forName(classAttribute.getText());
       catch (ClassNotFoundException e) {
           e.printStackTrace();
       }
       Field fields[] = bean.getDeclaredFields();
       Map<String, Field> mapField = new HashMap<String, Field>();
       for (Field field : fields)
           mapField.put(field.getName(), field);
       try {
           obj = bean.newInstance();
       catch (InstantiationException e) {
           e.printStackTrace();
       catch (IllegalAccessException e) {
           e.printStackTrace();
       }
 
       path.addHandler("property"new PropertyHandler(mapField, obj));
    }
 
    public void .End(ElementPath path) {
       Element beanElement = path.getCurrent();
       Attribute idAttribute = beanElement.attribute("id");
       beanMap.put(idAttribute.getText(), obj);
       path.removeHandler("property");
    }
}
   
PropertyHandler
private class PropertyHandler implements ElementHandler {
    Map<String, Field> mapField;
    Object obj;
 
    public PropertyHandler(Map<String, Field> mapField, Object obj) {
       this.mapField = mapField;
       this.obj = obj;
    }
 
    public void .Start(ElementPath path) {
       Element propertyElement = path.getCurrent();
       Attribute nameAttribute = propertyElement.attribute("name");
       path.addHandler("value"new ValueHandler(mapFieldobj,
              nameAttribute));
       path.addHandler("list"new ListHandler(mapFieldobj,
              nameAttribute));
       path.addHandler("set"new SetHandler(mapFieldobj,
              nameAttribute));
       path.addHandler("map"new MapHandler(mapFieldobj,
              nameAttribute));
       path.addHandler("ref"new RefHandler(mapFieldobj,
              nameAttribute));
    }
 
    public void .End(ElementPath path) {
       path.removeHandler("value");
       path.removeHandler("list");
       path.removeHandler("set");
       path.removeHandler("map");
       path.removeHandler("ref");
    }
}
 
根据setting.xml,我们可以得到各种注入元素的Handler类处理流程图。
 
4. setFieldValue()基于反射机制和相应的类信息得到Field的类型,并根据setting.xml设置它的值。
private void setFieldValue(Object obj, Field field, String value) {
    String fieldType = field.getType().getSimpleName();
    try {
       if (fieldType.equals("int"))
           field.setInt(obj, new Integer(value));
       else if (fieldType.equals("float"))
           field.setFloat(obj, new Float(value));
       else if (fieldType.equals("boolean"))
           field.setBoolean(obj, new Boolean(value));
       else if (fieldType.equals("char"))
           field.setChar(obj, value.charAt(0));
       else if (fieldType.equals("double"))
           field.setDouble(obj, new Double(value));
       else if (fieldType.equals("long"))
           field.setLong(obj, new Long(value));
       else
           field.set(obj, value);
    catch (IllegalArgumentException e) {
       e.printStackTrace();
    catch (IllegalAccessException e) {
       e.printStackTrace();
    }
}
 
private void setFieldValue(Object obj, Field field, List<String> value) {
    try {
       field.set(obj, value);
    catch (IllegalArgumentException e) {
       e.printStackTrace();
    catch (IllegalAccessException e) {
       e.printStackTrace();
    }
}
 
5.测试
public static void main(String[] args) {
    try {
       BeanFactory factory = new BeanFactory();
       factory.init("setting.xml");
 
       Person p1 = (Person) factory.getBean("me");
       System.out.print(p1.getName() + " ");
       System.out.print(p1.getAge() + " ");
       System.out.println(p1.getHeight());
 
       Person p2 = (Person) factory.getBean("you");
       System.out.print(p2.getName() + " ");
       System.out.print(p2.getAge() + " ");
       System.out.println(p2.getHeight());
 
       ListOne list = (ListOne) factory.getBean("myList");
       System.out.println(list.getMsg());
 
       SetOne set = (SetOne) factory.getBean("mySet");
       System.out.println(set.getMsg());
 
       MapOne map = (MapOne) factory.getBean("myMap");
       System.out.println(map.getMsg());
 
       Persons us = (Persons) factory.getBean("us");
       System.out.println(us.getI());
       System.out.println(us.getU());
    catch (Exception e) {
       e.printStackTrace();
    }
}
测试结果:
ZJ 26 1.78
Mary 27 1.66
[java, c, windows]
[cat, tom, dog]
{c=CHINA, j=JAPAN, k=KOREA}
com.zj.ioc.di.imp.Person@1a5ab41
com.zj.ioc.di.imp.Person@18e3e60


本文转自zhangjunhd51CTO博客,原文链接:http://blog.51cto.com/zhangjunhd/126545,如需转载请自行联系原作者

版权声明:本文首发在云栖社区,遵循云栖社区版权声明:本文内容由互联网用户自发贡献,版权归用户作者所有,云栖社区不为本文内容承担相关法律责任。云栖社区已升级为阿里云开发者社区。如果您发现本文中有涉嫌抄袭的内容,欢迎发送邮件至:developer2020@service.aliyun.com 进行举报,并提供相关证据,一经查实,阿里云开发者社区将协助删除涉嫌侵权内容。

分享:
开发与运维
使用钉钉扫一扫加入圈子
+ 订阅

集结各类场景实战经验,助你开发运维畅行无忧

其他文章