Respect for originality: original and source code address
There is a problem with spring in the project, and you do not want to inject with the spring framework, write a simple spring that implements XML parsing and class injection. All the frameworks can be used in Java projects, the use of Java provides the basic class, so the use of the principle is also more (I just started working for 2 months, said the wrong please understand). So spring uses the reflection mechanism injected into Java, and AOP uses the proxy mechanism of java. Summarize your design:
Design Range Three layer:
The first layer is the entity layer, which abstracts two object entities according to their own defined XML rules, one is the entity object Bean (defined in the program as Beandefinemodel) and the other is the attribute entity object (defined in the program as Paramdefinemodel).
The second layer is the XML parsing layer, which parses the XML and generates the Beandefinefactory object's connection. (The third-party dom4j jar package used here) is parsed to parse the XML into a single object chain.
The third layer is instantiated, and property injection is performed on the instantiated object. ApplicationContext
(This simulation spring uses Dom4j.jar and Commom.collection.jar and Common.lang.jar) source code is provided in the resources behind. All frameworks begin with defining XML specifications, parsing the content of the specification, and eventually generating an interface to access the Bean's HashMap, in the following XML format:
<?xml version= "1.0" encoding= "UTF-8"?>
<[email protected] Chunlei original:url--><beans> <bean name= "Bluehair" class = "Com.test.source.Hair" > <property name= "color" value= "Blue" ></property> <property name= "Hair" value= "good" > </property> <property name= "stringlist" > <list> <li value= "Zhang" ></li > <li value= "Chun" ></li> </list> </property> </bean> < Bean name = "Body" class = "Com.source.Body" ><property name= "name" Value= "Zhang" ></property>< Property name= "Hair" ref= "Bluehair" ></property> </bean></beans>
As a bean tag only name and class are of course later you can add attributes such as type, but the model parsing needs to be modified, property's properties are defined as Name,value,ref,list,map, and set. The map and set are not implemented, others are implemented, and the time will continue to fill their framework. Where the list label must start with Li, so that Li can exist under the Name,value,ref,list and other properties, to implement the Param model reuse. Recursion can also be used when parsing and instantiating.
The problem with completing this task is where it is injected, because using the reflection mechanism to find the method can use method (Methodname,propertytype) to obtain the methods, but it is easy to error, because we inject in the XML when it is possible to use entity class injection, It is possible to use interface injection. Later using a more troublesome approach, first stitching the method name, and then get all the methods of the bean, traverse all methods according to the method name to find the method, and then find the method parameter type, determine whether and injection property is the same, if the same is directly called reflection mechanism to inject, if different to get the parameters of all interfaces, Traverse the interface to determine whether and parameter interface to inject, the specific code is as follows:, the final code in the company, did not take, now the code is their own back to do overtime, only consider the entity class injection, issued up, later will be modified. Resources in the back to share, the Mid-Autumn festival time is full, but also to see SOLR source code, etc., a lot of homework. Understanding. Important code, Recursive injection:
<pre name= "code" class= "java" >package com.test;import java.lang.reflect.method;import Java.util.HashMap; Import Java.util.linkedlist;import Java.util.list;import Org.apache.commons.collections.collectionutils;import Org.apache.commons.lang.stringutils;public class Applicationcontext{private Static hashmap<string,object> HashMap = new hashmap<string,object> ();//used to store bean object public static hashmap<string,object> Getbeanmap ( String xmlpath) {list<beandefine> beandefinelist = Beandefinefactory.getbeandefinelist (XmlPath);// Get beandefinelist if (Collectionutils.isnotempty (beandefinelist)) {for (Beandefine beandefine:beandefinelist) {// Traverse Beandefinelist to get all Beandefineif (beandefine! = null) {String beanname = Beandefine.getbeanname ();// Put Beanname as key into Beanmap Object object = Getbeanobject (beandefine);//convert Beandefine to Bean objects hashmap.put (beanname, object);}}} return HASHMAP;} public static Object Getbeanobject (Beandefine beandefine) {String beanclass = Beandefine.getbeanclass ();//GetThe type of bean is Object beanobject = Getobjectbyclass (Beanclass);//The object is obtained by the Bean object list<paramdefine> paramdefinelist = Beandefine.getpropertylist ();//bean gets Propertylistif (Collectionutils.isnotempty (paramdefinelist)) {for ( Paramdefine paramdefine:paramdefinelist) {//Traversal property list for Injection if (paramdefine! = null) {String paramname = Paramdefine.getpropertyname (); Object paramobject = Getparamobject (paramdefine);//Get Paramdefine Object beanobject = Getregistbeanobject (beanobject,paramname,paramobject);//Inject Bean object}}}return beanobject;} public static Object Getparamobject (Paramdefine paramdefine) {object paramobject = null; String paramvalue = Paramdefine.getpropertyvalue ();//injected with string type if (Stringutils.isnotempty (paramvalue)) { Paramobject = new String (paramvalue); return paramobject;} String propertyref = Paramdefine.getpropertyref ();//Determine if injection is a ref type if (Stringutils.isnotempty (PropertyRef)) {if ( Hashmap.containskey (PropertyRef)) {paramobject = Hashmap.get (propertyref);} return paramobject;} list<paramdefine> paramdefinelist = paramDefine.getlist ();//Determine if the injection is a list type if (Collectionutils.isnotempty (paramdefinelist)) {list<object> list = new Linkedlist<object> (); for (Paramdefine tempparamdefine:paramdefinelist) {Object Tempparamobject = Getparamobject (Tempparamdefine); List.add (tempparamobject);} return paramobject = list;} set<paramdefine> Paramdefineset = Paramdefine.getset ();//Determine if injection is set type//if (Collectionutils.isnotempty ( Paramdefinelist)) {//object = Registobjectbyset (object,paramname,paramdefinelist);//return object;//}//Map< string,paramdefine> Paramdefinemap = Paramdefine.getmap ();//Determine if the map type is//if (paramdefinemap! = null && Paramdefinemap.size ()!=0) {//object = Registobjectbymap (object,paramname,paramdefinemap);//return Object;//}return Paramobject;} /** * Using the Java reflection mechanism to inject a bean object * @param beanobject Bean Object * @param paramname property name * @param paramobject attribute parameter * @return */publi C Static Object Getregistbeanobject (Object beanobject,string paramname,object paramobject) {String methodName = "Set" + PA RAmname.substring (0,1). toUpperCase () +paramname.substring (1);//Gets the name of the method System.out.println (Paramobject.getclass (). ToString ()); System.out.println (MethodName); try {method = Beanobject.getclass (). GetMethod (MethodName, Paramobject.getclass ());//by class type, Method name and parameter type to get method Method.invoke (Beanobject,paramobject);//java reflection Call Set method, set property} catch (Exception e) {//TODO Auto-generated catch Blocke.printstacktrace ();} return beanobject;} /** * Creates a bean object based on the class attribute value of the bean in the XML: the object that is not set property when the Bean object is here * @param Classnamee * @return */public static Object Getobjectbycl The Beanclass (String) {//classname generates an object based on the Beanobject = null;try {beanobject = Class.forName (Beanclass). Newinstance ();//Call Class Drive instantiation object} catch (Exception e) {//TODO auto-generated catch Blocke.printstacktrace ();} return beanobject;}}
There is a defect in the amount of injected code, such as post-finishing better after the, while allowing both set and map can, the general structure has been designed.
Simulate Spring injection