1. Introduction to Spring

Spring – > Spring

1.1 What is Spring

The Spring Framework is an open source J2EE application framework that is a lightweight container for managing the Bean lifecycle. Its main function is to integrate other frameworks and manage them in a unified and common way.

1.2 What is a Bean

Objects managed by the Spring container are called beans.

1.3 Spring concept

It makes the existing technology easier to use, solves the complexity of enterprise application development, and integrates the existing technology framework.

1.4 Spring official website address

Spring’s official website https://spring.io

1.5 Spring advantages

  • Spring is an open source and free framework
  • Spring is a lightweight, non-intrusive framework
  • Inversion of Control (IOC), Dependency Injection (DI), Aspect Oriented Programming (AOP)
  • Support for transaction processing, which is particularly good for AOP reasons
  • Support for framework integration

1.6 Spring integration with other frameworks

  • SSH: Struct2 + Spring + Hibernate (fully automated persistence layer framework)
  • SSM: SpringMVC + Spring + MyBatis (semi-automatic persistence layer framework, more customizable, more flexible SQL)

1.7 Spring core technology

  • IOC/DI
  • AOP

2. IOC of Spring core technology

2.1 What is IOC

Inversion of Control (IOC) is at the heart of the Spring Framework, and it is usually up to us to create and manage assembly objects. Through Inversion of Control, we no longer have the initiative to create the management assembly object, but become passive to receive this transfer of control, we call it Inversion of Control, it is a design idea. DI (Dependency Injection) is one way of implementing IoC, or we use Dependency Injection to implement our Inversion of Control.

Concept: The Spring container manages the creation of objects, and the Spring container controls the life cycle of objects (create/initialize/use/destroy). Action: Decreases coupling between code.

2.2 Spring introduction case

Create a project

  • Create an empty folder in IdeaProjets, jsd2103projects
  • Open this folder by IDEA
  • When opened, the directory structure is as follows
  • Create module -> spring_1_demo_ioc

2.2.1 Introduction of the JAR package via the pom.xml in the Maven project

<! Spring Framework </ grouppid > <artifactId>spring-context</artifactId>. Spring Framework </ grouppid > <artifactId>spring-context</artifactId> < version > 5.2.11. RELEASE < / version > < / dependency >

2.2.2 Create POJO classes

  • Create the POJO package in your project, and create the User class in your project with the following directory structure
Public class User {public void say(){System.out.println(" Spring Framework "); }}

2.2.3 Create the Application.xml configuration file under Resources

XML templates in Spring

<? 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 https://www.springframework.org/schema/beans/spring-beans.xsd"> </beans>

Basic configuration of XML

<? 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.xsd"> <! Spring container managed object id: The unique identifier of an object in the Spring container cannot duplicate the common class name first letter lowercase class: The full pathname of the class package name. The name of the class -- -- > < bean id = "user" class = "com. Jt. Pojo. User" > < / bean > < / beans >

2.2.4 Test whether the User object created can be obtained

The JAR package is introduced by means of the pom.xml in the Maven project

<! > <dependency> <groupId> jUnit </groupId> <artifactId> jUnit </artifactId> <version>4.12</version> </dependency>

Create a test class and test it

@test public void testSpring01(){@test public void testSpring01(){//1. By loading the configuration file, create a Spring container object container to create the object creation ApplicationContext context = new ClassPathXmlApplicationContext (" application. XML "); // 1) User = (User) context.getBean(" User "); // 2) User = Context.getBean (User. Class); // 3) User user2 = Context.getBean (" User ", user.class); System.out.println(user == user1); System.out.println(user == user2); System.out.println(user1 == user2); //3. Execute user.say(); }}

3. How Spring creates objects

3.1 Reflection mechanism

We can use reflection to create objects, but reflection requires a constructor with no arguments. Here is the test case:

Testing:

Add a no-argument construct to the User class

Public class User(){/* * * public class User(){public class User(){/* * public class User(){/* * public class User(){/* * public class User(){ System.out.println(" I am a no-argument construct "); } // public User(int I){// System.out.println(" I am a constructor with arguments "); //} public void say(){System.out.println(" Spring Framework "); }}

Add a test method to the test class

When we manually add a constructor with arguments ourselves to the User class, the no-arguments constructor in the class is automatically overwritten, and we report an error when we execute the following test method.

The error message is as follows:

When we manually add a constructor without arguments and add an output statement to the constructor, when we execute the following test method, it executes normally and prints the output statement to the console.

This means that when we create an object through reflection, the class to which the object is to be created must have a constructor without arguments.

@Test public void demo() throws ClassNotFoundException, IllegalAccessException, InstantiationException {/* * IOC * The bottom line of our Spring framework is that we use reflection to retrieve and instantiate class objects, * then helps us store the object in the Spring container. Here we use the Map<K,V> to store the id in the bean as the key in the Map, and the instantiated object as the Value * If we want to get the object from the container, we can get the object (value) from the Map set by ID (key) */ //1. Instantiate a type through the path of a class... Class userClass = Class.forName("com.jt.pojo.User"); //2. Instance = (User) userClass. NewInstance (); user.say(); }

3.2 Static factory mode

3.2.1 Edit the static factory class

Public class StaticFactory {/* * Calendar is an abstract class of the Util class. * public static Calendar * public static Calendar * public static Calendar * public static Calendar * public static Calendar * public static Calendar * public static Calendar * public static Calendar * public static Calendar * public static Calendar * public static Calendar getCalendar(){ return Calendar.getInstance(); }}

3.2.2 Edit the static factory configuration file

<! - 1. The writing method of static factory instantiation objects must be static - > < bean id = "calendar1" class = "com. Jt. Factory. StaticFactory" factory-method="getCalendar"/>

3.2.3 test class

/** * @Test public void testStatic(){ApplicationContext Context = new ClassPathXmlApplicationContext("application.xml"); Calendar calendar1 = (Calendar) context.getBean("calendar1"); System.out.println(" Get the current time :"+calendar1.getTime()); }

3.2 Instance factory pattern

.