Of complicated to simple, quick to realize how to use Spring AOP system log

1 Introduction

The concept of Spring AOP not go into detail, so a lot of articles online, I would not have put it better to talk than others, so do not long-winded.

Why use Spring AOP it? Less write code , focus on their business logic implementation (focus on own business and not think about other things, such as security, transactions, logs, etc.), with a grade point saying: realize the function we want to achieve through a non-intrusive way .

Why do we use Spring AOP to implement the system log it? Features of the system log is to be used in multiple modules in the system, in order to achieve unified management of logs, we generally have the following three approaches:

  1. Custom Log implementation class, create an instance where you need to log in to make calls;
  2. Define a log interfaces and implementation classes, use local logs to inherit this interface (Why To define an interface, rather than directly define the class, because the interface to achieve multiple inheritance, and we have a lot of background class itself is to inherit the appropriate interface, so that will not affect the introduction of the log interface to achieve our other business);
  3. Introducing Spring AOP, aspect-oriented programming, to achieve non-intrusive logging.

The above analysis of three ways, you can implement the system log function:

  • But the first one kind of too cumbersome, and made everywhere, does not look good ah, also appears to be low;
  • Although the second kind, unlike the first one kind of the same, but you have to be log-inheritance interfaces, have a series of method calls, passing parameters, always felt a lot of extra burden;
  • A third, not bad, just add an annotation on the method to be logged on the line, can record the dynamic log contents (different from the many online article describes the contents of the records are static content, simple is simple, the , but not practical ah) can be quite rich, good code is clean, it looked cool.

2, Spring AOP steps to implement the system log?

The elephant steps to install the refrigerator? The first step to open the refrigerator, put the elephant loaded into the second step, third step of closing the door, live together.
Similarly, Spring AOP steps to implement the system log it?

Step custom annotation class;
the second step section defined class;
the third step was added a custom log annotation ordinary methods in the class, live together.
The following diagram illustrates one:

Of complicated to simple, quick to realize how to use Spring AOP system log

Understand the above figure, you would be able to implement your own system log using Spring AOP.

System logs are generally stored in a database for easy navigation query management personnel. In addition, we need to do a different type of operation to record different content, such as adding or deleting the data, add or remove details to be recorded, if you made changes to the log to record values before and after modification, and so on, these work in the second step is defined aspect class implementation.

About the detailed code, we can refer to my video: https://edu.51cto.com/sd/091c7 , there is not posted out in detail

3, Spring AOP necessary knowledge

Although I do not want to explain the concept of Spring AOP, but necessary concepts we still have to clear, otherwise it will Yunshanwuzhao, Zhaomaohuahu might draw will not.

For the following concept of AOP Spring, I try to tell people saying:

A connection point (the JoinPoint) : the foregoing method, and behind the point of attachment is abnormal, when the application type is ProceedingJoinPoint;
entry point (Pointcut) : the method is in front of, or behind, or both before and after weaving, the corresponding annotation: @Before , @ After, @ Around;
weaving (weaving) : the method is in front of, or behind, or both before and after weaving; corresponding to annotation: @Retention, include three types, SOURCE, CLASS, RUNTIME, indicating annotation lifecycle rUNTIME commonly used (annotation runtime);
target (target) : a new method is added to the method attributes corresponding to the annotation: @Target;
section (Aspect) : specific work category corresponding annotation: @Aspect;

Another agent (proxy), notification (Advice), the introduction (introduction) these three concepts, namely three concepts easy to understand you better AOP mechanisms. (Not appropriate, like object-oriented programming, it is necessary to understand encapsulation, inheritance and polymorphism, like), to understand the best, do not understand the impact will not be too carry out your work.

Guess you like

Origin blog.51cto.com/3058076/2470130