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:
- package testSpring.business.iface;
- /**
- * INetPlay : Printing interface, the object needs to implement this interface to realize the function of printing the object
- *
- * @author xuejupo [email protected]
- *
- * create in 2016-2-16 9:16:52 am
- */
- public interface IPrint {
- /**
- * onLine: The object needs to implement this method to realize the function of printing the object
- * void return type
- */
- String printObject();
- }
Then there are two implementation classes:
- package testSpring.business.bean;
- import org.springframework.stereotype.Repository;
- import testSpring.business.iface.IPrint;
- /**
- * UserBean :
- * @author xuejupo [email protected]
- * create in 2016-2-16 9:22:39 am
- */
- public class UserBean implements IPrint{
- @Override
- public String printObject() {
- // TODO Auto-generated method stub
- System.out.println( "Print object UserBean:");
- return "abc";
- }
- }
- package testSpring.business.bean;
- import org.aspectj.lang.annotation.Aspect;
- import org.aspectj.lang.annotation.Pointcut;
- import org.springframework.stereotype.Service;
- import testSpring.business.iface.IPrint;
- /**
- * MyBean :
- * @author xuejupo [email protected]
- * create in 2016-2-16 10:11:43 am
- */
- public class MyBean implements IPrint{
- @Override
- public String printObject() {
- // TODO Auto-generated method stub
- System.out.println( "Print object MyBean:");
- return null;
- }
- }
Now, I want to print the first implementation class, the client code is as follows:
- IPrint print = new UserBean();
- 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:
- 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):
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
- xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
- xmlns:p="http://www.springframework.org/schema/p"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
- http://www.springframework.org/schema/tx
- http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
- http://www.springframework.org/schema/aop
- http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-3.2.xsd">
- <!-- register bean --!>
- <bean id="userBean" class="testSpring.business.bean.UserBean" />
- </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:
- //Read the configuration file (load the beans in the configuration file into memory)
- ApplicationContext ctx = new ClassPathXmlApplicationContext("/testSpring/resources/applicationContext.xml");
- // get the instance
- IPrint bean=(IPrint)ctx.getBean("userBean");
- // call the method
- bean.printObject();
result:
- Print object UserBean:
If you want to print another implementation class, just modify the configuration file:
- <bean id="userBean" class="testSpring.business.bean.MyBean" />
result:
- Print object MyBean:
Very simple, hand over control to spring's xml file.