Introduction to Spring framework, linux learning video

IoC – reverse of control, which reverses the creation right of the object to Spring!!

Using IOC can solve the problem of high program coupling!!

Control reversal

Suppose I need to do a function. In this function, I need to call the service layer, and then call the dao layer to get the data. In traditional Java EE development, I directly go to a new service and then a new dao. However, in the spring framework, let's hand over the rights of new service and new dao to a spring framework. If I need to use it, I will directly look for it in the spring framework. It means that the right to create my resources is handed over to the spring framework, which is called control inversion.


Just now we said that the creation of resources was entrusted to sring. We can find spring for what we need. This process is like a factory model. But what objects it needs to create in the spring framework, it needs a configuration file. This configuration file tells spring which resources need to be created.

For example, suppose I need to go to the database to query the data display page

When the program starts, the spring framework looks for the configuration file to create resources, places the resources in another container, starts running, the front end requests data, looks for the controller layer in spring, then looks for the service layer, and then looks for the dao layer to ask for data. Finally, the data returns to the controller in the original way and is displayed on the page. The service is injected into the controller layer by spring, and the dao layer is injected into the service layer by spring. The division of labor in this process is clear. Each floor performs its own duties. In a traditional development, new is directly in the servlet, then the data is checked, and then the data is returned to the interface. In case of operating more than one judgment and querying different tables, the code of this servlet becomes very bloated. Not to mention slow development, it's hard for you to read the code after development. So control inversion can be used to decouple

What is AOP?


  • In the software industry, AOP is the abbreviation of Aspect Oriented Programming, which means Aspect Oriented Programming

  • AOP is a programming paradigm, which belongs to the category of software engineering and guides developers how to organize program structure

  • AOP was first proposed by the organization of AOP alliance and formulated a set of specifications. Spring introduces the idea of AOP into the framework and must abide by the specifications of AOP alliance

  • A technology of unified maintenance of program functions through precompiled mode and runtime dynamic agent

  • AOP is the continuation of OOP, a hot spot in software development, an important content in Spring framework, and a derivative paradigm of functional programming

  • AOP can isolate each part of business logic, reduce the coupling between each part of business logic, improve the reusability of program, and improve the efficiency of development

In fact, AOP can enhance the program without modifying the source code!!

The underlying implementation of AOP in Spring framework

1. The underlying AOP technology of spring framework also adopts proxy technology. There are two proxy methods

1. Dynamic agent based on JDK

 It must be interface oriented. Only classes that implement specific interfaces can generate proxy objects 

2. Dynamic agent based on CGLIB

For classes that do not implement interfaces, you can also generate proxies and subclasses of this class 

2. Spring's traditional AOP uses different proxy methods according to whether the class implements the interface

  1. If the class interface is implemented, use JDK dynamic proxy to complete AOP
  1. If the interface is not implemented, CGLIB dynamic agent is used to complete AOP

Dynamic agent of JDK

Note: you need to implement the class interface

Example: suppose I have two jobs, job 1 and job 2

//Write an interface

public interface Working {

    void wokingOne();

    void WorkingTwo();


//Interface implementation class

public class WorkingImpl implements Working {


public void wokingOne() {

    System.out.println("Do task 1");



public void WorkingTwo() {

    System.out.println("Do task 2");



> OK, now I'm going to do task 1 first, and then task 2:

public static void main(String[] args) {

    Working working = new WorkingImpl();

    working.wokingOne();//Do task 1

    working.WorkingTwo();//Do task 2


> OK, here comes the wonderful place,I need to rest for 10 minutes before I do task 2, but I can't modify the source code. What shall I do? That's when we need it JDK Dynamic proxy. The code is as follows:

> First write a tool class for the agent. Let's take a ten minute break before we do task 2 again

public class MyProxyUtils {

public static Working getProxy(final Working working) {

    // Generating Proxy objects using Proxy classes

    Working proxy = (Working) Proxy.newProxyInstance(working.getClass().getClassLoader(),

            working.getClass().getInterfaces(), new InvocationHandler() {

                // When the proxy object method is a straight line, the invoke method executes once

                public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

                    //I'll take a 10 minute break before I do another job 2

                    if ("WorkingTwo".equals(method.getName())) {

                        System.out.println("Take a 10 minute break");


                    //The work continued

                    return method.invoke(working, args);



    // Return proxy object

    return proxy;



Tags: Java Linux Spring Back-end Programmer

Posted on Thu, 09 Sep 2021 16:05:39 -0400 by huzefahusain