[Spring] source to learn how to complete the spring of IOC container

First, the whole idea

1, the whole idea

 

2, organize your thoughts abstract algorithm

/ ** 
     * real spring containers to instantiate a template substantially algorithm object 
     * @param A 
     * @return 
     * / 
    public Object of createObject (Class A) {
         // Step1: obtaining instances of the type according to the type of 
        Object O = the createInstance ( a);
         // Step2: Get all the properties of the object to be implanted in accordance with the type of 
        List <Class> classes = getObjectByAutoWrite (a);
         IF (CollectionUtils.isEmpty (classes)) {
             // no injection property, it exits the recursive 
            return O; 
        } 
        // Step3: create instances of the object dependency 
        for (Class CLS: classes) {
             //Global object from the context, or container, it is determined whether the object is instantiated. [Solve the problem of duplicate objects created, and a cyclic dependency problem] 
            Object O1 = isAlreadyCreateObject (CLS);
             IF (O1 == null ) { 
                O1 = of createObject (CLS); 
            } 
            // Step4: writing dependent 
            doAutoWrite (o, o1 ); 
        } 
        return O; 
    } 


    / ** 
     * Create an instance of the class object 
     * 
     * @param a 
     * @return 
     * / 
    public object the createInstance (class a) { 
        object object = null ;
         the try {
            Object = a.newInstance (); 
        } the catch (an InstantiationException is E) { 
            e.printStackTrace (); 
        } the catch (IllegalAccessException E) { 
            e.printStackTrace (); 
        } 
        return Object; 
    } 

    / ** 
     * is determined from the context, or global object if the current class has been created objects 
     * as already created, an object instance returns 
     * without creating, return null 
     * 
     * @param A 
     * @return 
     * / 
    public Object isAlreadyCreateObject (class A) {
         return  null ; 
    } 


    / **
     * Get the current class required to complete the injection spring collection of objects 
     * 
     * @param A 
     * @return 
     * / 
    public List <Class> getObjectByAutoWrite (Class A) {
         return  null ; 
    } 

    / ** 
     * write dependence 
     * 
     * @param O is dependent object 
     * @param the depend dependent objects
      * / 
    public  void doAutoWrite (Object O, the depend Object) { 

    }
View Code

 

Guess you like

Origin www.cnblogs.com/shangxiaofei/p/12287593.html