1 Introduction
1.1 IOC introduction
IOC (inverse of control), i.e. reverse control.
When a component in an application needs to obtain resources, the traditional way is for the component to actively obtain the required resources from the container. In this mode, developers often need to know how to obtain specific resources in specific containers, which increases the learning cost and reduces the development efficiency.
The idea of inversion control completely subverts the traditional way of obtaining resources, reverses the acquisition direction of resources, and becomes that the container actively pushes resources to the required components. Developers do not need to know how the container creates resource objects, but only need to provide a way to receive resources, which reduces the learning cost and improves the efficiency of development.
This behavior is also known as the passive form of lookup.
1.2 introduction to di
DI (Dependency Injection), that is, Dependency Injection, is the most classic implementation of IOC inversion control.
If a component running in the container needs another component, it needs to dynamically inject the required component into the original component through DI.
1.3 bottom layer principle
The underlying principles of IOC include: XML parsing, factory schema, reflection.
Using XML parsing, you can read the contents of the configuration file.
The factory mode can reduce the coupling of code.
Use reflection to further reduce coupling and create objects from the contents of the configuration file.
2 IOC interface
Before reading Bean instances through the IOC container, you need to instantiate the IOC container itself. Spring provides two implementations of the IOC container: BeanFactory and ApplicationContext.
2.1 BeanFactory
The basic implementation of IOC container is the internal infrastructure of Spring. It is oriented to Spring itself and is not provided to developers.
Objects are not created when the configuration file is loaded, but only when the configuration object is used.
2.2 ApplicationContext
The sub interface of BeanFactory provides more advanced features. For Spring users, ApplicationContext is used in almost all occasions instead of the underlying BeanFactory.
Objects are created when the configuration file is loaded.
ApplicationContext instantiates all singleton beans when initializing the context. There are two main implementation classes:
1) ClassPathXmlApplicationContext: the configuration file in XML format corresponding to the classpath.
2) FileSystemXmlApplicationContext: configuration file corresponding to XML format in the file system.
3 Guide Package
3.1 core
spring-beans-4.0.0.RELEASE.jar spring-context-4.0.0.RELEASE.jar spring-core-4.0.0.RELEASE.jar spring-expression-4.0.0.RELEASE.jar
3.2 dependence
commons-logging-1.1.3.jar
4 Introduction cases
4.1 create project and guide package
Create a normal Java project and import it into a jar package.
4.2 test code
Create a normal class and create methods in the class:
public class User { public void add() { System.out.println("add ..."); } }
Create a configuration file and configure objects of common classes in the configuration file:
<?xml version="1.0" encoding="UTF-8"?> <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.xsd"> <!-- to configure User objects creating --> <bean id="user" class="com.demo.spring.User"></bean> </beans>
Create a test class and write a test method:
public class TestSpring { @Test public void testAdd() { // Load Spring configuration file ApplicationContext context = new ClassPathXmlApplicationContext("application.xml"); // Get configured object User user = context.getBean("user", User.class); // Use object System.out.println(user); user.add(); } }
4.3 performing tests
Perform the test method and the results are as follows:
com.demo.spring.User@26a7b76d add ...
It indicates that the test is successful and the configured object is obtained through the configuration file.