The environment

  1. jdk1.8

  2. idea

  3. Maven – 3.6.1 track

  4. Spring – 5.2.3 requires. RELEASE

The IOC container

IOC container is a container with dependency injection function, which is responsible for the instantiation and initialization of objects, the configuration of dependencies between objects, the destruction of objects, and the search for externally provided objects. The whole life cycle of objects is controlled by the container. The objects we need to use are managed by the IOC container. We do not need to manually create objects in the new way. The IOC container will directly assemble them for us and we can directly obtain them from the IOC container when we need to use them.

So how does the Spring IOC container know which objects to manage?

We need to provide the IOC container with a configuration list that supports XML and Java annotations, lists the objects that need to be managed by the IOC container in the configuration file, and can specify how the IOC container should build these objects. When the Spring container starts, the configuration file will be loaded. These objects are then assembled for use by external visitors.

The IOC container in question is also called the Spring container.

Bean concept

The objects managed by the Spring container are collectively called Bean objects. Beans are common Java objects, and our own new objects are the same, but these objects are created and managed by Spring. We need to tell the Spring container in the configuration file which Bean objects need to be created, so we need to define the Bean objects to be created in the configuration file. These configurations, collectively known as bean definition configuration metadata, are read by the Spring container to build and assemble the objects we need.

Spring container usage steps

  1. Introduce Spring-related Maven configurations

  2. Create bean configuration files, such as bean XML configuration files

  3. Define the bean objects that need to be managed by the Spring container in the bean XML file

  4. Create a Spring container and specify the bean configuration files that the container needs to load. When the Spring container starts, these configuration files are loaded, and then the bean objects defined in the configuration files are created and placed in the container for use

  5. Get objects in the container using methods provided by the container, and then use

Spring container object

Spring provides a number of interfaces and objects that represent the Spring container. Let’s take a look at some of the more common container interfaces and specific implementation classes.

The BeanFactory interface

org.springframework.beans.factory.BeanFactory
Copy the code

A typical spring container is the BeanFactory interface, which is the top layer of the Spring container and provides the most basic functions of the container.

Several commonly used methods
Throws BeansException (String Name); throws BeansException (String name); throws BeansException (String name); throws BeansException (String name) Returns a bean object of the specified type. <T> T getBean(String name, Class<T> requiredType) throws BeansException; <T> T getBean(Class<T> requiredType) throws BeansException; <T> ObjectProvider<T> getBeanProvider(Class<T> requiredType);Copy the code

ApplicationContext interface

org.springframework.context.ApplicationContext
Copy the code

This interface inherits from the BeanFactory interface, so it contains all the functionality of the BeanFactory interface internally, and has been extended on top of it to add enterprise-level functionality such as AOP, internationalization, event support, and more.

ClassPathXmlApplicationContext class

org.springframework.context.support.ClassPathXmlApplicationContext
Copy the code

This class implements the ApplicationContext interface. Notice that the class name contains ClassPath Xml, indicating that the container class can load the bean Xml configuration file from the ClassPath and then create the bean object configured in the Xml. We’ll use this class in a later case.

AnnotationConfigApplicationContext class

org.springframework.context.annotation.AnnotationConfigApplicationContext
Copy the code

This class also implements the ApplicationContext interface. Notice that the class name contains Annotation and config. As we mentioned above, the bean definition supports both XML and annotated methods. This container will be used for loading, and the annotations will be internally parsed to build and manage the beans needed.

Annotations are more convenient than XML, and we recommend them. We’ll use them a lot later.

case

Let’s use HelloWorld to see how Spring works in detail.

Create the project Spring-Series

Create maven project spring-series with idea

< the groupId > com. Javacode2018 < / groupId > < artifactId > spring - series < / artifactId > < packaging > pom < / packaging > < version > 1.0 - the SNAPSHOT </version>Copy the code

The spring-Series project creates a sub-module, The maven parent-child structure, as shown in the following figure:

1581221060218

spring-series/pom.xml

<? The XML version = "1.0" encoding = "utf-8"? > < project XMLNS = "http://maven.apache.org/POM/4.0.0" XMLNS: xsi = "http://www.w3.org/2001/XMLSchema-instance" Xsi: schemaLocation = "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" > < modelVersion > 4.0.0 < / modelVersion > < groupId > com. Javacode2018 < / groupId > < artifactId > spring - series < / artifactId > <packaging> POM </packaging> <version> 1.0-snapshot </version> <modules> <module>lesson-001</module> </modules> <properties>  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <! - configure maven compile the compiler version - > < maven.compiler.com pilerVersion > 1.8 < / maven.compiler.com pilerVersion > <! If the source code does not match this version, an error will be reported. Maven uses this configuration when compiling. The default is 1.5. Source >1.8</maven.compiler.source> <! This command is used to specify which vm version the generated class file is guaranteed to be compatible with. Maven uses this configuration when compiling. The default is 1.5. --> <maven.compiler.target>1.8</maven.compiler.target> <spring.version>5.2.3.RELEASE</spring.version> </properties> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring.version}</version> </dependency> </dependencies> </dependencyManagement></project>Copy the code

Currently, we are using the latest spring version 5.2.3.RELEASE, and we need to introduce three artifacts provided by Spring

Spring-core, spring-context, spring-beansCopy the code

lesson-001\pom.xml

<? The XML version = "1.0" encoding = "utf-8"? > < project XMLNS = "http://maven.apache.org/POM/4.0.0" XMLNS: xsi = "http://www.w3.org/2001/XMLSchema-instance" Xsi: schemaLocation = "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" > < the parent > < artifactId > spring - series < / artifactId > < groupId > com. Javacode2018 < / groupId > < version > 1.0 - the SNAPSHOT < / version > < / parent > The < modelVersion > 4.0.0 < / modelVersion > < artifactId > lesson # - 001 < / artifactId > < dependencies > < the dependency > <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> </dependency> </dependencies></project>Copy the code

Create HelloWord class in lesson-001

package com.javacode2018.lesson001.demo1; /** * public number: passer a Java, work 10 years of former Ali P7 share Java, algorithm, database technology dry goods! Firmly believe in using technology to change fate, so that families live a more decent life! */public class HelloWorld {public void say() {system.out.println ("hello, welcome! ); }}Copy the code

In HelloWord we create a say method that will type a text.

Using the Spring container

Let’s create the HelloWord object using the Spring container, get the object from the container, and call its say method to output the text.

Create the bean XML configuration file

Create a new file with the following path:

spring-series\lesson-001\src\main\resources\com\javacode2018\lesson001\demo1\bean.xml
Copy the code

Bean.xml contains the following contents:

<? The 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-4.3.xsd "> <! -- Define a bean id: the bean's unique identifier that can be used to fetch the bean object from the container: This type of bean, full class name - > < bean id = "helloWorld" class = "com. Javacode2018. Lesson001. Not. The helloWorld" / > < / beans >Copy the code

Above is the bean definition file. Each XML can define multiple bean elements. Through the bean element, you can define objects that need to be managed by the Spring container

  • The id represents the bean’s identity, which needs to be unique in the container and can be used to retrieve the object from the container.

  • Class is used to specify the full class name of the bean

In the configuration file above we define a HellWorld type bean object with the helloWorld identifier.

Creating a test class

Create a Client class as follows:

package com.javacode2018.lesson001.demo1; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * public number: passer a Java, work 10 years of former Ali P7 share Java, algorithm, database technology dry goods! Firmly believe in using technology to change fate, so that families live a more decent life! */public class Client {public static void main(String[] args) {//1.bean configuration file location String beanXml = "classpath:/com/javacode2018/lesson001/demo1/beans.xml"; / / 2. Create ClassPathXmlApplicationContext container, To the container need to load bean configuration file specified ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext (beanXml); HelloWorld = context.getBean(" HelloWorld ", helloWorld.class); //4. Use object helloworld.say (); }}Copy the code

In the main method above, you need to create the container object. When you create the container object, you need to specify the location of the bean XML file. After the container is started, these configuration files will be loaded, and then the objects will be built.

The code retrieves the HellWorld object from the container via the container-provided getBean method. The first parameter is the ID of the bean in the XML and the second parameter is the corresponding Class object of the bean.

conclusion

This article mainly introduces the concept of Spring container, bean concept, common Spring container, and the use of spring container steps; In the next article we will explain the definition of beans in detail.

This information mainly includes Java foundation, data structure, JVM, multithreading and so on. Due to the limited space, only a small part of the interview questions are shown below. If you need a complete version of the interview questions, you can click a link to get the link: Click here for free download, get the code: nuggets