The main content of this article

  • Spring Overview (Basics)
  • Core ideas IoC and AOP
  • Handwritten implementation of IoC and AOP (custom Spring Framework)
  • Spring IoC advanced applications
  • Basic knowledge of
  • Advanced features
  • Spring IoC source in depth analysis
  • Very elegant design
  • Design patterns
  • Note: principles, methods and techniques
  • Spring AOP advanced applications
  • Declarative transaction control
  • Spring AOP source depth analysis
  • Necessary notes, necessary diagrams, easy to understand language to resolve knowledge difficulties

Summary of the Spring

Spring is a hierarchical full-stack lightweight open source framework, based on IoC and AOP as the kernel, provides a large number of enterprise application technologies such as presentation layer SpringMVC and business layer transaction management, but also can integrate many well-known third-party frameworks and class libraries in the open source world. It has become the most used Open source framework for Java EE enterprise applications.

Development history of Spring

  • In 1997, IBM proposed the idea of EJB;
  • In 1998, SUN developed the development standard specification EJB1.0;
  • In 1999, EJB 1.1 was released;
  • In 2001, EJB 2.0 was released;
  • In 2003, EJB 2.1 was released;
  • In 2006, EJB 3.0 was released;
  • The latest version of Spring, Spring 5.0 Universal (GA), was released in September 2017.

The advantage of the Spring

  • Easy to decouple and simplify development
  • Support for AOP programming
  • Support for declarative transactions
  • Convenient program testing
  • Easy integration of various excellent frameworks
  • Reduce the difficulty of using JavaEE apis
  • Source code is a classic Java learning paradigm

The core structure of Spring

Spring is a lightweight framework with very clear layers and dependencies and responsibilities. It consists of several large modules: Spring relies on the data processing module, The Web module, the AOP (Aspect Oriented Programming /Aspects) module, the Core Container module, and the Test module, as shown below. Implements a lightweight framework that happily integrates existing solutions with zero intrusion.

  • The Core Container is the Core part of the Spring framework. It manages the creation, configuration, and management of beans in Spring applications. In this module, the Spring Bean factory is included, which provides DI functionality for Spring. Based on the bean factory, we will also find multiple implementations of Spring application contexts. All Spring modules are built on top of the core container.
  • Aspect Oriented Programming (AOP) /Aspects Spring provides rich support for aspect oriented programming. This module is the foundation of the development aspect in Spring applications, and like DI, AOP helps decouple application objects.
  • Spring’s JDBC and DAO modules encapsulate a lot of boilerplate code to keep our database code clean and focused on our business, and to avoid problems caused by database resource release failures. In addition, Spring AOP provides transaction management services for data access, while Spring also integrates ORM, such as Hibernate, MyBatis and so on. This module consists of JDBC, Transactions, ORM, OXM and JMS modules.
  • Web This module provides the SpringMVC framework for Web applications and provides a variety of remote invocation schemes for building interactions with other applications. The SpringMVC framework improves the level of loose coupling of applications at the Web layer.
  • Test To make it easy for developers to Test, Spring provides a Test module dedicated to testing Spring applications. Through this module, Spring provides a series of mock object implementations for writing unit tests using servlets, JNDI, and so on.

What is IoC?

  • Note that this is a technical idea, not a technical implementation description: Java development domain object creation, management issues
  • Traditional development methods: for example, class A depends on class B, and usually new an object of B in class A
  • Development mode under IoC idea: We don’t give yourself to the new object, but by the IoC container (Spring framework) to help us to instantiate objects and management of it, we need to use which object, ask the IoC container can we lost a right (create, manage, object of rights), got a welfare (without considering object creation, management and a series of things)

Why is it called inversion of control?

  • Control: Refers to the right to create (instantiate, manage) objects
  • Reverse: Control is given to the external environment (Spring framework, IoC container)

What problem has the IoC solved

  • IoC addresses the coupling problem between objects

Difference between IoC and DI

  • DI: Dependancy Injection

How to understand:

  • IOC and DI describe the same thing, but from different angles

What is the AOP

  • AOP: Aspect Oriented Programming
  • AOP is a continuation of OOP, starting with OOP
  • OOP is characterized by encapsulation, inheritance, and polymorphism
  • Oop is a vertical inheritance system

What problem is AOP solving

In the case of not changing the original business logic, enhance the cross-cutting logic code, fundamentally decouple, avoid cross-cutting logic code repetition

Why is it called faceted programming

  • “Cutting” : refers to the crosscutting logic. We cannot move the original business logic code, but can only operate the crosscutting logic code, so we face the crosscutting logic
  • “Planes” : Cross-cutting logic code often affects many methods, each of which is like a point, with multiple points constituting planes and the concept of a plane inside

Spring IOC source in depth analysis

Benefits:

Improve and cultivate code architecture thinking and in-depth understanding of frameworks

The principle of

Macro principle: Focus on the source code structure and business process from the perspective of God (play down the details of writing a specific line of code)

Read source code methods and skills

Breakpoint (Observing the Call Stack) Counterlead (Find Lead) Experience (doXXX in the Spring Framework, where specific processing is done)

Spring source code construction

Install Gradle 5.6.3 (similar to Maven) Idea 2019.1 Jdk 11.0.5 Core-oxm-context-beans-aspects – AOP) project – > Tasks – >compileTestJava

Spring IoC’s container architecture

IoC container is the core module of Spring, which abstracts object management and dependency management framework solution. Spring provides a number of containers, of which the BeanFactory is the top-level (root) container and cannot be instantiated. It defines a set of principles that all IoC containers must follow. Specific container implementations can add additional functionality, such as the common ApplicationContext. Its more specific implementation such as ClassPathXmlApplicationContext contains content, parse the XML and a series of AnnotationConfigApplicationContext is includes annotations and so on a series of content. The Spring IoC container inheritance system is smart enough to use whichever layer you need, rather than the full-featured one. The BeanFactory top-level interface method stack is as followsBeanFactory container inheritance system

Spring AOP applications

Before getting into AOP terminology, let’s take a look at the following two diagrams, which are extensions of the requirements of the case in Part 3 (the requirements for these extensions will be analyzed only, and AOP will be reviewed further on this basis, not implemented)The figure above depicts a program designed without AOP thinking, which can lead to a lot of rework when we highlight the methods in the red box. The program is filled with a lot of duplicate code, so that the independence of our program is very poor. The following figure is the use of AOP thought design of the program, it is the red box part of the code extracted at the same time, the use of dynamic proxy technology, at run time to use the business logic method to enhance.

AOP terminology

AOP proxy selection in Spring

By default, Spring chooses whether to use JDK or CGLIB based on whether the proxied object implements an interface. Spring selects CGLIB when the proxied object does not implement any interface. When the proxied object implements the interface, Spring chooses the JDK’s official proxy technology, but we can configure Spring to enforce the use of CGLIB.

How AOP is configured in Spring

  • In Spring’s AOP configuration, as in the IoC configuration, three types of configuration are supported.
  • Type one: Using XML configuration
  • The second type: combined configuration using XML+ annotations
  • Class 3: Using a pure annotation configuration

AOP implementation in Spring

Requirement: The cross-cutting logic code is to print the log, and you want to weave the log logic into a specific location of the target method (service layer transfer method)

conclusion

Thank you for reading here, the article has any deficiencies also please correct, think the article is helpful to you remember to give me a thumbs-up!

Due to the lack of space, I can only draw these notes. First, I have arranged the whole set of notes into A PDF document, which is as follows:Welcome to pay attention to the public number: bright future, get this complete spring source summary notes! There are also a line of large factory Java interview questions summary + the knowledge learning thinking guide + a 300 page PDF document Java core knowledge summary!