Whether to log the results returned by the service, what kind of log should be logged, and why?

This is the back-end small class of the monastery. Each article is shared from

[background introduction] [knowledge analysis] [common problems] [solutions] [coding practice] [extended thinking] [more discussion] [References]

Eight aspects of in-depth analysis of back-end knowledge / skills. This article shares:

[do you want to log the results returned by the service, what kind of log should you log, and why?]

[small java class of the Academy] do you want to log the results returned by the service, what kind of log should you log, and why?

Hello, I'm an honest, pure and kind-hearted java programmer. Today, I'd like to share with you the knowledge points in in in-depth thinking - whether to log the results returned by the service, what kind of log to log, and why?

1. Background introduction

In software development, Functions scattered in many places in the application are called crosscutting concerns (cross cutting concern). Generally speaking, these crosscutting concerns are conceptually separated from the business logic of the application, such as logging, declarative things, security and caching. These things are not the core functions of our usual code writing, but they are used in many places. If we print the log according to the object-oriented design method, we must print it in the The contents of the log are added to the class of the log. It is because object-oriented design makes it impossible to connect between classes, and can not unify these repeated codes. Separating these crosscutting concerns from the business is the problem to be solved by aspect oriented programming (AOP). In short, it is to separate and encapsulate the functions that are used in many places but not the core business, and specify to cut into the specified method through configuration.

2. Knowledge analysis

At runtime, the code is dynamically cut into the specified method and location of the class, and the logic code is separated from the code dealing with trivial transactions, so that the complexity can be separated. This programming idea is aspect oriented programming. Crosscutting concerns can be modularized into special classes, which are called aspects. This has two advantages: each concern is concentrated in one place rather than divided into multiple codes. Service modules are more concise because they only contain the code of the primary concern (or core function), while the code of the secondary concern is transferred to the aspect.

Join point

The connection point is a point that can be inserted into the section during application execution. This can be when a method is called, when an exception is thrown, or even when a field is modified. Slice code can use these points to insert into the normal process of the application and add behavior.

Pointcut

If the notification defines "what" and "when" of the aspect, the pointcut defines "where". For example, I want to introduce logs into a specific method, which is the so-called tangent point.

Aspect

Facet is a combination of notification and pointcut. Notification and pointcut jointly define the whole content of the aspect, what it is, when and where to complete its function.

Introduction

The introduction allows us to add new methods and properties to existing classes (spring provides a function of method injection)

Weaving

The process of applying facets to the target object to create a new proxy object

Advice

In AOP, faceted work is called notification. The notification defines the "what" and "when" aspects are used. In addition to describing the work to be done, the notification also solves the problem of when to perform the work The spring aspect can apply five types of notifications

Pre notification (Before): calling the notification function before the target method is invoked.

Post notification (After): calling notification after completion of the target method will not care what the output of the method is.

Return notification (After-returning): call notification after successful execution of the target method.

Exception notification (After-throwing): calling notification after the target method throws an exception.

Surround notification: the notification wraps the notified method and executes custom behavior before and after the notified method is called

3. Frequently asked questions

Annotation based approach

XML based configuration

4. Solutions

5. Coding practice

6. Expand thinking

ProceedingJoinPoint

Around uses the parameter proceedingjoinpoint. The proceed () method is to give control to the notified method. If you forget to call this method, your notification will actually block the call to the notified method.

Spring MVC loading problem

If the scanning package is configured in spring MVC, all beans will be loaded first, and the AOP is configured in the context of spring. When spring MVC loads serivce, the AOP has not been configured for loading, and the aspect class will become invalid

7. References

reference material:

————Spring MVC dry goods series: building spring MVC + mybatis from scratch (IV): AOP learning of the two cores of spring

8. More discussion

What is the difference between spring AOP and AspectJ AOP?

Spring AOP is implemented based on dynamic proxy; AspectJ is implemented based on static proxy.

Spring AOP only supports pointcut at the method level; Full AOP support is provided, and it also supports attribute level pointcut.

What are the implementation methods of AOP?

The technologies for realizing AOP are mainly divided into two categories:

Static proxy - refers to compiling with the commands provided by the AOP framework, so that AOP proxy classes can be generated at the compilation stage. Therefore, it is also called compile time enhancement;

Dynamic proxy - generates AOP dynamic proxy classes "temporarily" in memory at runtime, so it is also called runtime enhancement.

What are proxies in spring

The object created after advice is applied to the target object is called a proxy. In the case of a client object, the target object and the proxy object are the same.

9. Acknowledgment

Thank you for watching. If there is any error, please correct it

10. Conclusion:

That's all for today's sharing. You are welcome to like, forward, leave messages and make bricks~

Ppt link video link

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>