This will mimic spring to implement a dependency injection mechanism based on an XML configuration file. The file will implement 3 injection, one is a single value injection, including Int,float,double,char, also include string injection, Java container injection, including List,set,map three container injection, the last one is Java Bean object injection.
The mechanism of implementation is to parse the XML configuration file using dom4j, where the element handler mechanism of DOM4J is used, a similar implementation mechanism with the responsibility chain pattern, and the reflection mechanism is used for building Java objects, which is mainly for the field of the resulting class Set assignment. I tried to invoke the setter method of the class itself by invoking the Invoke method of methods, but since the values parsed by XML are strings, if the conversion of these string dynamics to the corresponding determining type is a difficulty, the Invoke method of methods, if the formal parameters is an int, and the incoming Java.lang.Integer is not recognized, so the attempt fails, and only a specific value is passed through the field's set method.
Configuration file 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>