2022-01-23-Spring learning step001

Learn the Spring video of a little rain in Jiangnan of station B.

I typed the following content according to the video for my personal study only. All rights belong to a little rain in Jiangnan.

01. String opening: www.javaboy.com Org Jiangnan a little rain personal website

02. Introduction to spring:

Spring, as we usually say, refers to the Spring Framework, which is just a branch of the spring family. The spring family includes many, such as Spring Boot, Spring Framework, Spring Cloud Data, Spring Session, Spring Batch, Spring Security, and Spring Cloud. The Spring Framework is a foundation of the spring family.

Spring is created to solve the complexity of enterprise application development. Before spring, there was a heavyweight tool called EJB.

In Spring, DI is commonly referred to as dependency injection, which is actually provided in EJB. In fact, you will find that Spring itself is also a product of repeatedly building wheels, but if you build wheels well, the future is unlimited. Some people advocate not to repeat building wheels. If I am the inventor of wheels, I will certainly tell you not to repeat building wheels, Just use my wheel. If I were not the inventor of the wheel, I would doubt it.

There are two important functions in Spring. One is IOC. IOC is actually provided in Java EE. In Java EE, it has a JNDI, Java Naming and Directory Interface, Java Naming and Directory Interface. In fact, the IOC and JNDI have some similarities. The Spring Framework is equivalent to subverting the earliest development mode of Java EE to a certain extent. In the past, it was said that Java EE development had Java EE standards. Now, it is mainly operated by the set of schemes provided by Spring.

The advantages of spring, such as abstracting transactions, unified database transactions, distributed transactions, etc., and aspect oriented programming, make the operation of transactions very simple. The other is the Spring Framework. The other is spring MVC, which makes the whole framework of the spring family reach a new peak. Now we are developing RESTful interfaces or ordinary background applications. Spring MVC is a very common choice.

Explain Spring from four aspects:

  • IoC/DI
  • AOP
  • affair
  • JdbcTemplate

03. Spring Download: use Maven to add spring.

04. IoC introduction:

IOC concept: IOC (Inversion of Control), which is called control inversion in Chinese. This is a concept and an idea. Control reversal actually refers to the reversal of control over an object. For example, the following code:

class class Book {
    private Integer id;
    private String name;
    private Double price;
    // ellipsis getter/setter
}

public class User {
    private Integer id;
    private String name;
    private Integer age;

    public void doSth() {
        Book book = new Book();
        book.setId(1);
        book.setName("New story");
        book.setPrice((double) 20);
    }
}

In this case, the control of the Book object is in the User object. In this way, the Book and User are highly coupled. If the Book object needs to be used in other objects, it must be recreated, that is, the creation, initialization, destruction and other operations of the object must be completed by the developer himself. If these operations can be managed by the container, developers can be greatly relieved from the creation of objects.

After using Spring, we can leave the creation, initialization and destruction of objects to the Spring container for management. That is, when the project starts, all beans will register themselves in the Spring container (if necessary), and then if other beans need to use this Bean, they do not need to go to new themselves, but directly go to the Spring container.

05. Initial IOC experience:

05.1 idea creates Maven project and introduces spring context dependency:

<dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.2.6.RELEASE</version>
        </dependency>
    </dependencies>

05.2 create a Spring configuration file in the resource directory, ApplicationContext XML. In this file, we can configure all beans that need to be registered with the Spring container:

<?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 class="org.javaboy.ioc.model.User" id="user"/>
</beans>

The class attribute indicates the full path of the bean to be registered, and the id indicates the unique identifier of the bean. You can also use name as the identifier of the bean. In more than 99% of cases, the id and name are actually the same, but they are different in special cases.

Next, load the configuration file in the following main method:

package org.javaboy.ioc;

import org.javaboy.ioc.model.User;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

/**
 * @author konglc
 * @date 2022/1/23 15:17
 */
public class Main {
    public static void main(String[] args) {
//        User user = new User();
//        System.out.println("user = " + user);
//        FileSystemXmlApplicationContext ctx = new FileSystemXmlApplicationContext("E:\\study\\Spring\\code\\ioc01\\src\\main\\resources\\applicationContext.xml");
        ClassPathXmlApplicationContext ctx =  new ClassPathXmlApplicationContext("applicationContext.xml");
        User u1 = (User) ctx.getBean("user");
        User u2 = ctx.getBean("user",User.class);
        User u3 = ctx.getBean(User.class);
        System.out.println("u1 = " + u1);
        System.out.println("u2 = " + u2);
        System.out.println("u3 = " + u3);
    }
}

Execute the main method, the configuration file will be loaded automatically, and then initialize a User instance in Spring. At this time, we display the parameterless constructor of the specified User class and print the log in the parameterless constructor. We can see that the parameterless constructor has been executed, which proves that the object has been initialized in the Spring container.

Finally, you can get the object from the container through the getBean method.

In idea, mouse over the class and press CTRL+H to display the inheritance relationship of the class.

In idea, right-click the file and select Copy Path/Absolute Path to get the absolute path directly.

06. Basic attribute injection:

Attribute injection:

  • Attribute injection: basic data type
  • Complex property injection: object injection, array injection, Map injection and Properties injection

Proceed to 01:01 of P06.

Added by NZ_Kiwi on Wed, 26 Jan 2022 06:45:17 +0200