Java Code Implementation Dependency Injection

Source: Internet
Author: User

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>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.