Three core ideas of Spring: IOC

Three core ideas of Spring: IOC

 First of all, what is the core and most basic concept of spring? Comparing spring to java, the core and most basic concept of java is object. In java, all operations are for objects (except basic types). In java, everything is an object, and everything is an object. By analogy, the most basic concept in spring is the bean. In spring, all classes can be considered as a bean. (In my opinion) all files in spring can be considered as registered beans and unregistered beans. All operations in spring are operations for beans. Naturally, the objects operated in the three core ideas of spring are also beans.

 

         One of the three core ideas of spring: IOC, inversion of control.

         To understand what inversion of control is, we must first understand what is no inversion of control.

         See the code below:

         First there is an interface class:

Java code   Favorite code
  1. package testSpring.business.iface;  
  2.   
  3. /** 
  4.  * INetPlay : Printing interface, the object needs to implement this interface to realize the function of printing the object 
  5.  *  
  6.  * @author xuejupo [email protected] 
  7.  *  
  8.  * create in 2016-2-16 9:16:52 am 
  9.  */  
  10.   
  11. public interface IPrint {  
  12.     /**   
  13.     * onLine: The object needs to implement this method to realize the function of printing the object 
  14.     * void return type    
  15.     */  
  16.     String printObject();  
  17. }  

     Then there are two implementation classes:

Java code   Favorite code
  1. package testSpring.business.bean;  
  2.   
  3. import org.springframework.stereotype.Repository;  
  4.   
  5. import testSpring.business.iface.IPrint;  
  6.   
  7. /**   
  8.  *  UserBean :  
  9.  * @author xuejupo  [email protected]  
  10.  * create in 2016-2-16 9:22:39 am     
  11.  */  
  12. public class UserBean implements IPrint{  
  13.     @Override  
  14.     public String printObject() {  
  15.         // TODO Auto-generated method stub  
  16.         System.out.println( "Print object UserBean:");  
  17.         return "abc";  
  18.     }  
  19.   
  20. }  

 

Java code   Favorite code
  1. package testSpring.business.bean;  
  2.   
  3. import org.aspectj.lang.annotation.Aspect;  
  4. import org.aspectj.lang.annotation.Pointcut;  
  5. import org.springframework.stereotype.Service;  
  6.   
  7. import testSpring.business.iface.IPrint;  
  8.   
  9. /**   
  10.  *  MyBean :  
  11.  * @author xuejupo  [email protected]  
  12.  * create in 2016-2-16 10:11:43 am     
  13.  */  
  14. public class MyBean implements IPrint{  
  15.   
  16.     @Override  
  17.     public String printObject() {  
  18.         // TODO Auto-generated method stub  
  19.         System.out.println( "Print object MyBean:");  
  20.         return null;  
  21.     }  
  22. }  

 Now, I want to print the first implementation class, the client code is as follows:

Java code   Favorite code
  1. IPrint print = new UserBean();  
  2. print.printObject();  

       Very simple code. At this time, the creation of the second implementation class UserBean object is completed by the client. Equivalent to the client controls the creation of the UserBean object. There is no problem with the code, and the execution result is as follows:

Java code   Favorite code
  1. Print object UserBean:  

     Very normal execution result. But at this time, I want to modify it and print the second implementation class. At this time, I need to modify the code. For a formal system, modifying the code is not in line with the open-closed principle after all, and it is more troublesome. So I wonder if I can use the configuration file to replace the modification of the code. At this time, you need to use spring's inversion of control: the creation of specific objects of specific implementation classes that inherit the interface IPrint does not depend on calling his client code, but gives control to spring (in fact, it is an xml file) , which is Inversion of Control.

    Below is the xml file (registered bean, will):

Java code   Favorite code
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"  
  4.     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"  
  5.     xmlns:p="http://www.springframework.org/schema/p"  
  6.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
  7.     http://www.springframework.org/schema/beans/spring-beans-3.2.xsd  
  8.     http://www.springframework.org/schema/tx  
  9.     http://www.springframework.org/schema/tx/spring-tx-3.2.xsd  
  10.     http://www.springframework.org/schema/aop    
  11.     http://www.springframework.org/schema/aop/spring-aop-3.2.xsd   
  12.     http://www.springframework.org/schema/context  
  13.     http://www.springframework.org/schema/context/spring-context-3.2.xsd">  
  14.     <!-- register bean --!>  
  15.     <bean id="userBean" class="testSpring.business.bean.UserBean" />  
  16.   
  17. </beans>  

       Among them, I have not tested what is similar to the URL in the file header. It is said on the Internet that it is the path of the spring's label specification inspection file. First of all, he will look for your local label specification file (usually there are), and if it is not found locally, it will go to his url to find it.

        The test code is as follows:

 

Java code   Favorite code
  1. //Read the configuration file (load the beans in the configuration file into memory)  
  2.         ApplicationContext ctx = new ClassPathXmlApplicationContext("/testSpring/resources/applicationContext.xml");  
  3.         // get the instance    
  4.         IPrint bean=(IPrint)ctx.getBean("userBean");    
  5.         // call the method    
  6.         bean.printObject();  

 result:

Java code   Favorite code
  1. Print object UserBean:  

 If you want to print another implementation class, just modify the configuration file:

Java code   Favorite code
  1. <bean id="userBean" class="testSpring.business.bean.MyBean" />  

 result:

Java code   Favorite code
  1. Print object MyBean:  

      Very simple, hand over control to spring's xml file.

Personal classification:  reprint

Guess you like

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