This is the 15th day of my participation in the August Text Challenge.More challenges in August

Spring IOC Inversion of control

An IoC is an Inversion of Control. It refers to transfer the right to call the object which is directly controlled by the program code to the container, and realize the assembly and management of the object through the container. Inversion of control is a transfer of control over an object from the program code itself to an external container. Objects are assembled and managed through containers.

IoC is a concept, an idea, and it can be implemented in a variety of ways. Dependency injection is the most popular implementation. Widely used.

Dependency: The classA class contains instances of classB and calls methods of classB in classA to complete functions. That is, classA has dependencies on classB.

The realization of the IOC

● Dependency Lookup: DL (Dependency Lookup). Containers provide callback interfaces and context to components.

● Dependency Injection: DI (Dependency Injection), the program code does not do location query, these work is completed by the container.

DI means that when a program is running and needs to call another object for assistance, it does not need to create the called in the code. Instead, it relies on the external container, which creates the called object and passes it to the program.

Spring’s dependency injection makes almost no demands on the caller or the called, and fully supports the management of dependencies between POJOs.

Dependency injection (DI) is currently the best decoupling method. Dependency injection allows Spring beans to be organized together in configuration files rather than hard-coded coupling.

The Spring framework implements IoC using dependency injection (DI).

The Spring container is a mega-factory that creates and manages all the Java objects, called beans. The Spring container manages the dependencies between beans in the container, and Spring uses dependency injection to manage the dependencies between beans. Use IoC to achieve decoupling and between objects.

Preparing development tools

Development tools: IDEA2017 or above

Dependency management: maven3 or above

The JDK: 1.8 above

Need to set up maven native repository:

The first Spring program

On the basis of ordinary three-tier architecture, the program is modified to Spring framework program

Example: 01 – primay

Import the Jar package

First, import the four basic JAR packages that Spring programs develop.

Log: commons.apache.org/proper/comm…

Second, import the log related Jar package.

Commons.logging.jar This file is only an implementation specification for logging and does not have an implementation (equivalent to an interface definition for logging operations).

The logging implementation here uses Log4j, so log4j.jar is also required.

Finally, import the JUnit test Jar package junit-4.9.jar.

Spring basic programming, a total of seven Jar packages can be.

Define interfaces and entity classes

Create the Spring configuration file

The name of the Spring configuration file is optional, but Spring recommends applicationContext.xml. The file constraint is in the %SPRING_HOME%\docs\spring-framework-reference\ HTML \ xsD-configuration. HTML file.

Note that the constraint file used in the Spring configuration file is an XSD file. If Eclipse does not have automatic prompt, you need to point the domain name address to be searched to the local XSD file. The corresponding XSD files are in the corresponding subdirectory of the Schema directory in the Spring framework decompression directory.

Constraint file: XML Schema Definition (XSD) XML document structure definition.

Purpose: Verify that the logical structure of an XML document is correct.

● Define what elements are in an XML document

● Define what attributes are in an XML document

● Define which child elements an element in an XML document can have, and in what order.

● Define the data types of elements and attributes in an XML document.

The constraint file spring-beans.xsd is required, so you need to look in the Beans subdirectory for the corresponding version of the constraint file.

< bean /> : Used to define an instance object. One instance corresponds to one bean element.

Id: This attribute is the unique identity of the Bean instance. The application accesses the Bean through the ID attribute, and the dependencies between beans are also related through the ID attribute.

Class: Specifies the class to which the Bean belongs. Note that this is only a class, not an interface.

Configuration cannot be networked, native constraint file

Copy the URL of the constraint file, then click Perferences under the Windows menu of Eclipse

Defining test classes

Container interfaces and implementation classes

1. ApplicationContext interface (container)

ApplicationContext loads Spring configuration files and acts as a “container” in your program. There are two implementation classes.

Ctrl +T to view:

A. The configuration file is in the classpath

If the Spring configuration file stored in the project classpath, use the ClassPathXmlApplicationContext implementation class is loaded.

B. The configuration file is in the local directory

If the Spring configuration file stored in local disk directory, use the FileSystemXmlApplicationContext implementation class is loaded.

C. The configuration file is in the project root path

If the Spring configuration file stored in root directory of the project, using the same FileSystemXmlApplicationContext implementation class is loaded.

Here is what happens when the configuration file is stored in the root path of the project, in the same directory as the SRC directory, not in SRC.

D. Assembly timing of objects in ApplicationContext

The ApplicationContext container assemps all the objects in the container at once when the container object is initialized. To use these objects in future code, you just need to fetch them directly from memory. High execution efficiency. But it takes up memory.