Spring's IOC principle

Spring's IOC principle

What is an IOC:

IOC as the name implies: (inversion of controller) inversion of control. In the process of java program development, each business logic needs at least two java objects to cooperate to realize the business. Previously, the object was applied for by the new Object() method, but this would cause coupling between programs. Through control inversion, the new The work of the object is handed over to the spring container. When the object needs to be used, the object is applied for in the container, and the responsibility of how the object gets its matching object is reversed.


How does spring work?

public static void main(String[] args){
        ApplicationContext context = new FileSystemXmlApplicationContext("applicationContext.xml");
        Animal animal = context.getBean("Animal");
        animal.say();
}

An Animal is defined in applicationContext.xml

<bean id="animal" class="com.cesec.Cat">   
        <property name="name" value="kitty" />   
</bean>  

 

 There is a com.cesec.Cat class

public class Cat implements Animal {   
    private String name;   
    public void say() {   
        System.out.println("I am " + name + "!");   
    }   
    public void setName(String name) {   
        this.name = name;   
    }   
}  

 

 

 cat implements the Animal interface

public interface Animal {  
    public void say();  
}  

 


How does spring work?

First define a bean similar to the cat mentioned above to store the properties owned by the bean

private int id;
private String type;
private Map<String,Object> properties = new HashMap<String,Object>;

 

spring starts loading configuration files

<bean id="test" class="Test">   
        <property name="testMap">   
            <map>   
                <entry key="a">   
                    <value>1</value>   
                </entry>   
                <entry key="b">   
                    <value>2</value>   
                </entry>   
            </map>   
        </property>   
</bean>  

 

How does spring save the above configuration?

if (beanProperty.element("map") != null) {   
                    Map<String, Object> propertiesMap = new HashMap<String, Object>();   
                    Element propertiesListMap = (Element) beanProperty   
                            .elements().get(0);   
                    Iterator<?> propertiesIterator = propertiesListMap   
                            .elements().iterator();   
                    while (propertiesIterator.hasNext()) {   
                        Element vet = (Element) propertiesIterator.next();   
                        if (vet.getName().equals("entry")) {   
                            String key = vet.attributeValue("key");   
                            Iterator<?> valuesIterator = vet.elements()   
                                    .iterator();   
                            while (valuesIterator.hasNext()) {   
                                Element value = (Element) valuesIterator.next();   
                                if (value.getName().equals("value")) {   
                                    propertiesMap.put(key, value.getText());   
                                }   
                                if (value.getName().equals("ref")) {   
                                    propertiesMap.put(key, new String[] { value   
                                            .attributeValue("bean") });   
                                }   
                            }   
                        }   
                    }   
                    bean.getProperties().put(name, propertiesMap);   
                }  

 

How does spring implement dependency injection, instantiate a class through reflection, and then set the properties saved to the hashmap to the instance:

public static Object newInstance(String className) {   
        Class<?> cls = null;   
        Object obj = null;   
        try {   
            cls = Class.forName(className);   
            obj = cls.newInstance();   
        } catch (ClassNotFoundException e) {   
            throw new RuntimeException(e);   
        } catch (InstantiationException e) {   
            throw new RuntimeException(e);   
        } catch (IllegalAccessException e) {   
            throw new RuntimeException(e);   
        }   
        return obj;   
    }  

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325250433&siteId=291194637