IOC introduction of Spring
Spring's IOC idea
One of Spring's core ideas: IOC (Inversion Of Control) Inversion Of Control.
Inversion Of Control (IOC): it is an important feature of the framework to hand over the creation, initialization, destruction and other work of objects to the framework. It's not an object-oriented programming term. Inversion Of Control includes dependency injection and dependency lookup.
-
What does inversion of control mean?
- The creation of the object is done by the external container. This is the inversion of control.
- Spring uses inversion of control to implement the use of objects (so that this part of the program does not have to be written dead in the program).
-
So how to deal with the dependency between objects in Spring?
- Dependency injection.
- In Spring, dependency injection is used to implement dependencies between objects.
- After an object is created, its relational processing is dependency injection.
Inversion of control is done through an external container, and Spring provides a container called IOC container. No matter how many objects are created, how many dependencies are handled, when they are created, or how many they are created, they are all configured in the IOC container provided by Spring.
The core of the idea of IOC is that resources are not managed by the resource side, but by the third-party management (IOC container), which brings many benefits:
- The centralized management of resources realizes the configurable and easy management of resources.
- It reduces the dependence of both sides of using resources, that is, the coupling problem.
Spring uses IOC container to manage objects
How does Spring's IOC container manage objects? Let's take a look at the steps of Spring.
1. Import dependency
<!--Spring Core dependency jar package--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>4.1.7.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>4.1.7.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.1.7.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-expression</artifactId> <version>4.1.7.RELEASE</version> </dependency> <!--log4j journal--> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> <!--Note related jar package--> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.2</version> </dependency>
2. Write profile
- Configuration files for Spring( applicationContect.xml).
<?xml version="1.0" encoding="UTF-8"?> <!--Root tag--> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> </beans>
3. Create entity class
- Create resources( User.java ).
public class User { private String id; private String name; //getter and setter methods omitted }
4. IOC container management object
- We used to create objects using the new User method. For example: User user = new User();
- Now use the IOC's container. You can use the IOC container to create objects applicationContext.xml Add the corresponding information of User in the file.
<?xml version="1.0" encoding="UTF-8"?> <!--Root tag--> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <!--use bean Nodes to create objects id Property represents an object class Property indicates the full path of the object to be handed over to container management--> <bean id="user" class="com.tulun.bean.User"/> </beans>
5. Get User object through container
public class App { public static void main(String[] args) { //Get container of IOC ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); //Get the current object through the container User user = (User) applicationContext.getBean("user"); System.out.println(user); } }
- Execution result:
Introduction to Spring's IOC container
The core module in Spring: IOC container, which solves the creation of objects and the dependency between objects.
- So how to get IOC container?
The above figure shows the inheritance relationship of the interface in the Spring IOC container, in which ApplicationContext is one of the sub interfaces of beanfactory. In other words, beanfactory is the lowest interface in the IOC container, and ApplicationContext is one of its high-level interfaces, which makes many functional extensions to beanfactory. In most application scenarios, ApplicationConext is used as the container of Spring IOC.
Common implementation classes of ApplicationContext
Common implementation classes of ApplicationContext | explain |
---|---|
Classpathxmlapplicationcontext (common) | Load the configuration file under the classpath. The configuration file must be under the classpath |
FileSystemXmlApplicationContext | You can load the configuration file under any path of the disk (you must have access rights) |
XmlWebApplicationContext | Read configuration information in Web Environment |
AnnotationContigApplicationContext | Used to read annotation creation container |
- There are three ways to read configuration files:
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
FileSystemXmlApplicationContext xmlApplicationContext = new FileSystemXmlApplicationContext("c:/java/applicationContext.xml");
XmlWebApplicationContext wa = new XmlWebApplicationContext()//Container not initialized wa.setServletContext(sevletcontext);//servletContext object needs to be specified wa.setConfigLocation("/applicationContext.xml");//Specifies the path of the configuration file, starting with the path with the web as the root directory wa.refresh();//Initialize container
The difference between BeanFactory and ApplicationContext
- When ApplicationContext builds the core container, the object is created in the way of immediate loading. That is, as soon as the configuration file is read, the objects configured in the configuration file are created.
- When BeanFactory builds the core container, the policy of creating objects is to delay loading. That is to say, when to get the object according to the id and when to create the object.
Analysis of Spring container principle
- 1. Using dom4j technology, parse the XML configuration file to get all the information in it.
- 2. Based on the value of the class attribute, an object is created with reflection.
- 3. Put the object created by reflection into the map, and the key is its corresponding id value.