Tips

I will focus on the big core of IOC and AOP in Spring. We’ll talk about declarative transactions, the annotated way of starting, and the new features of Spring5.

A basic concept of Spring

It is a JavaEE open source lightweight framework. What is lightweight? In the development process, we do not need to rely on some complex JAR packages, but only need to introduce Spring core JAR packages to realize the functions of Spring. The bottom layer is to encapsulate some complex business logic and provide API interfaces, which only need us to use, so that we can get started quickly. It can solve the problems we encounter in enterprise development, can make coding simpler and reduce code redundancy, etc.

Core components

IOC: Indicates inversion of control. The core interface is BeanFactory.

It is a design principle in object-oriented programming. Spring manages the entire object creation process and usage with the implementation of reflection, factory schema, and parsed XML (in this case, see Dom4j). The most common approach is DI injection, also known as dependency injection. Its purpose is to reduce the coupling of our programCopy the code

AOP: Aspect oriented

Can be understood as our code (function) before and after the implementation of the enhancement, often to print logs, interception, etc.Copy the code

BeanFactory (B) and Aplication (A

We can have the IDEA to select (I choose here is B interface ClassPathXmlApplicationContext below the implementation class) by CRTL + Alt + u to check the class diagram.

B it is a top level interface, not directly provided for us developers to use, mainly provided for spring internal use. A is mainly provided for us developers to use. A is also A subinterface under B, which implements A lot of extension functions on the basis of BCopy the code

The difference between:

When loading the configuration file through B, the object is not created according to the content of the configuration file. When we really need to use the object, we go back to create the object. When loading the configuration file through A, the object is directly created according to the configuration file and cached.Copy the code

In my own words, it’s A (A) vertical loading, and (B) lazy loading.

Talk about SpringBean

There are two types of beans in Spring. The first type is a regular Bean and the second is a Factory Bean.Copy the code

What are the differences between regular beans and factory beans?

Ordinary Beans: Whatever type is defined in the configuration file must be consistent with the return type. Factory Bean: What type is defined in the configuration file that can be inconsistent with the return type. You need to implement FactoryBean and override the getObject method.Copy the code

Beans are managed

What is Bean management, that is, Spring can help us to create objects (reflection technology), so what are the ways of management?

  1. Manage objects in XML (by default, execute a parameterless constructor when reflecting an initialobject)
  2. DI (dependent) based on the way to manage the object 2.1 object attribute based on the set method 2.2 based on the parameter constructor implementation
  3. Set injection to manage objects

The scope of the SpringBean

  1. What is scope?

    Specifies whether the Bean is scoped as a singleton or multiple.

  2. What’s the difference between a singleton and a multiple in Bean scope? What’s the difference between singletons and multiples?

    Scope difference: The scope of a singleton is the same object every time getBean is called. The scope of the multiple cases is new every time the getBean method is called.

    Singleton Difference: Singleton: Within the same JVM, the Bean object is created only once. Multiple examples: The Bean object can be created multiple times in the same JVM. (Here we can say to a disadvantage, more memory)

  3. Does Spring default to singletons or multiples? Why is that?

    Beans are singletons by default. Singleton it can save our server memory

Life cycle of a SpringBean

When it comes to the Bean lifecycle, we can focus on the core, that is, the Bean’s postprocessor, which needs to implement the interface BeanPostProcessor, which can be operated before and after the Init method. A little extension to talk about the Servlet lifecycle.

Life cycle of servlets: After initialization calls the init () method — — — — — — — — — — – > call service () method to handle the client request — — — — — — — — — — – > destroyed before call destroy () method — — — — — — — — — — – > finally by the JVM garbage collector for garbage collection.

The life cycle of a SpringBean can be simply divided into: instantiate —–> attribute to assign —–> init method in the Bean class —–> use the object —–> container close Initialize the object (default constructor executes with no arguments) —–> Assign the object property —–> Call init method —–> Use Bean—–> Container closing (the object will be destroyed) Here, we can do code enhancement before and after initialization, we can customize the post-processor

Talk about Spring and SpringBoot relationship

I will briefly say that SpringBoot is started directly by annotating, and the bottom layer also relies on Spring/SpringMVC annotating. It can be said to do a packaging for Spring/SpringMVC.