1. Introduction to Spring
It goes without saying that Spring is a lightweight framework for control inversion (IoC) and face-to-face programming (AOP). IoC and AOP are almost essential for interviews, so their ideas and applications need to be well understood in practical development.
1. Spring Advantages
- Spring is an open source, free framework.
- Spring is a lightweight, non-invasive framework. (Add new features without modifying the original code)
- Control Inversion (IoC), Face Oriented Programming (AOP). [Key]Core
- Supports transaction processing.
2. SSM
SSM(Spring + SpringMVC + MyBatis) is the most popular combination of enterprise application development frameworks, so Spring is actually a very basic but very important framework in Java.
3. IoC Control Inversion (Spring Core Ideas)
Inversion of Control Inversion is Spring's core idea. It's a quick and easy way to understand IOC: "Don't Invert" when we create an object, we need to change the original code where we use it and replace it with the object we create, such as object A in a line of code. When we want to switch to object B here, we need to change object A to object B after class B has been defined. That is to say, from a certain point of view, which object a program will use, even the running of the program, depends on the program itself, because the code of the program has been written to death before, in order to change the original using object or the running behavior of the program, you need to change the original code; It's like giving control of the program's operation to the programmer or the program itself. If you want to "reverse" this control so that we add a new class, when we want to use this class, we can use the objects created by this class without changing the original code, or we want to change the behavior of the program. Instead of modifying the original code, you just need to add a class, so that the control of the behavior of the program is not in the program itself, but "reverse" to third-party programs (even users), and add or remove the corresponding classes from third-party programs according to business needs. This description is not necessarily 100% accurate. The focus is on understanding the reversal of "control", who controls what, and who controls whom. IoC is an idea, not just a programming language. Of course, to learn Spring, you first need to understand the idea of IoC controlling inversion.
In fact, there are many ways to implement IoC idea. In Spring, the IoC container is used to invert IoC control. The implementation method is DI (Dependency Injection) dependent injection.
4. AOP Face Oriented Programming
AOP face-to-face programming, which in general means customizing some special methods without changing the existing code and letting them execute before or after a specified method is executed, such as I want to do some filtering before processing the request after receiving it, and some formatting before returning the response to the browser. AOP Face-Oriented Programming involves terms such as entry point, facet, etc. These terms may not be well understood at first contact and can be understood with practice.
5. Maven Dependent Injection
Some of the Maven dependencies that may be used when learning or using Spring are listed here so that I don't forget to search again when I get them==!
<dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.26</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.7</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.3.12</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>5.3.12</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.9.7</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>2.0.6</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.20</version> </dependency> </dependencies>
2. Hello World
1. Hello World example
This Hello world is very simple and needs to focus on understanding the IoC control inversion idea applied to it (see Code Notes).
bean class definition:
public class MyBean { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "MyBean{" + "name='" + name + '\'' + '}'; } }
beans.xml configuration:
Direct new -> xml Configuration File -> Spring Config in IDEA generates a spring xml 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"> <!-- Each configured here bean Is an object, xml Objects in the Spring To create, Use directly in Spring Just grab it in a container. --> <bean id="myBean" class="com.yun.pojo.MyBean"> <!-- have access to property Assign values to the object properties after creation. But the premise is that this property must have set Method--> <property name="name" value="zhangsan"/> </bean> </beans>
Test:
import com.yun.pojo.MyBean; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MyTest { public static void main(String[] args) { // Create a Spring context object (which can be understood as a container, the Spring container) ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); // Obtaining the objects we configure in the xml from the container shows that our own program only defines a class. // Then I configured a bean in the xml and did not actively use the new keyword to create objects in the code, so the IoC idea is reflected here. // Control over the creation of objects is "reversed" from the program itself in traditional programming thinking to a third party, Spring. // In the future, control of creating objects is given to Spring, and we only need to configure bean s in xml. MyBean myBean = (MyBean) context.getBean("myBean"); System.out.println(myBean); } }
2. How Spring objects are created
Regardless of how you create objects using Spring, when the xml configuration loads, all the bean objects are created together, not when you go to the Spring container to get them.
2.1 Create objects using parameterless construction by default
Spring uses parameterless construction to create objects by default. We know that Java will provide a default parameterless construction method if no construction method is defined when defining a class, but if a parameterized construction method is defined in a class, Java will not provide parameterless construction and needs to define it itself. So if we don't define a construction method when defining bean s, Spring uses the default parameterless construction method to create objects.
bean class definition:
public class MyBean { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "MyBean{" + "name='" + name + '\'' + '}'; } }
xml configuration:
<bean id="myBean" class="com.yun.pojo.MyBean"> <!-- have access to property Assign values to the object properties after creation. But the premise is that this property must have set Method--> <property name="name" value="zhangsan"/> </bean>
2.2 Create an object from the parameter list index of a parametric construction method
If a class defines a parametric construct, you can also create an object in an xml configuration using the parameter list index with a parametric construct.
bean class definition:
public class Student { private int id; private String name; private int age; public Student(int id, String name, int age) { this.id = id; this.name = name; this.age = age; } @Override public String toString() { return "Student{" + "id=" + id + ", name='" + name + '\'' + ", age=" + age + '}'; } }
xml configuration:
<?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"> <bean id="student" class="com.yun.pojo.Student"> <!-- Create objects using a parametric construction method: constructor-arg Labels represent assignments to object attributes in a parametric construction method. index Subscript to the list of parameters in the represented parametric construction method, starting at 0 --> <constructor-arg index="0" value="0"/> <constructor-arg index="1" value="zhangsan"/> <constructor-arg index="2" value="18"/> </bean> </beans>
2.3 Create objects from parameter names that are constructed with parameters
If a class defines a parametric construct, you can also create an object in an xml configuration using the parameter name of the parametric construct.
bean class definition:
public class Student { private int id; private String name; private int age; public Student(int id, String name, int age) { this.id = id; this.name = name; this.age = age; } @Override public String toString() { return "Student{" + "id=" + id + ", name='" + name + '\'' + ", age=" + age + '}'; } }
xml configuration:
<?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"> <bean id="student" class="com.yun.pojo.Student"> <!-- Create objects using a parametric construction method: constructor-arg Labels represent assignments to object attributes in a parametric construction method. name Name of corresponding parameter in the represented parametric construction method --> <constructor-arg name="id" value="0"/> <constructor-arg name="name" value="zhangsan"/> <constructor-arg name="age" value="18"/> </bean> </beans>