Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the principle and process of Spring AOP

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "what is the principle and process of Spring AOP". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let the editor take you to learn "what is the principle and process of Spring AOP"?

# Spring AOP principle

Briefly talk about the design of AOP:

Each Bean is represented by JDK or Cglib. Depends on whether there is an interface.

Each Bean will have multiple method interceptors. Note: the interceptor is divided into two layers, the outer layer is controlled by the Spring kernel, and the inner interceptor is set by the user, that is, AOP.

When the proxy method is called, it goes through the outer interceptor first, and the outer interceptor determines which "inner interceptor" the method should execute according to the various information of the method. The design of the inner interceptor is the design of the responsibility company.

It's not easy to steal. It's even easier after reading it.

AOP can be divided into two parts to pull, oh, no, to analyze.

First: the creation of the agent; second: the invocation of the agent.

Note: we try to post less code, try to use text description, because the interview, is also a text description, it is impossible for you to translate the code. So, here you need to keep some simplicity, want to know the details, look at the interface 21 source code, want to know more details, look at Spring Framework's latest master branch code.

Code location: under the com.interface21.aop package.

Start the analysis (pull):

Creation of the agent (step by step):

First, you need to create an agent factory, which requires three important pieces of information: the interceptor array, the target object interface array, and the target object.

When you create a proxy factory, another default interceptor is added at the end of the interceptor array-- for the final call to the target method.

When the getProxy method is called, a proxy object (JDK or Cglib) is returned according to the condition of a large number of interfaces.

Note: when you create a proxy object, you also create an outer interceptor, which is the interceptor of the Spring kernel. The process used to control the entire AOP.

The call of the agent

When a call is made to the proxy object, the outer interceptor is triggered.

The outer interceptor creates an inner interceptor chain based on the agent configuration information. During creation, the expression is used to determine whether the current interception matches the interceptor. And this interceptor chain design pattern is the responsibility chain pattern.

When the entire chain is executed at the end, the default interceptor at the tail when the proxy is created is triggered, thus calling the target method. And finally return.

Let's take a non-standard UML diagram:

For the calling process, take a flowchart:

Code location: com.interface21.web.servlet.DispatcherServlet#doService

Yes, it is the code of Spring 1.0. after 15 years of development, Spring has become too bloated. From a learning point of view, interface 21 is the best code and does not accept rebuttal.

The code is as follows:

1. Set the property / / 1. Set properties / / Make web application context availablerequest.setAttribute (WEB_APPLICATION_CONTEXT_ATTRIBUTE, getWebApplicationContext ()); / / Make locale resolver availablerequest.setAttribute (LOCALE_RESOLVER_ATTRIBUTE, this.localeResolver); / / Make theme resolver availablerequest.setAttribute (THEME_RESOLVER_ATTRIBUTE, this.themeResolver); 2. The corresponding handler execution chain is obtained according to the URL requested by Request, which is actually the interceptor and Controller proxy object. / / 2. Find handler and return the execution chain HandlerExecutionChain mappedHandler = getHandler (request); 3. Get the adapter / / This will throw an exception if no adapter is found// 3 for handler. The adapter that returns handler HandlerAdapter ha = getHandlerAdapter (mappedHandler.getHandler ())

What exactly is the function of this adapter? The HandlerAdapter comment reads: This interface is not intended for application developers. It is available to handlers who want to develop their own web workflow.

This interface is not applicable to application developers. It is suitable for handlers that want to develop their own Web workflow.

In other words, if you want to do something before dealing with handler, you may need this, that is, to adapt the handler. For example, the Spring test program does this:

Public ModelAndView handle (HttpServletRequest request, HttpServletResponse response, Object delegate) throws IOException, ServletException {/ / you may need doSomething. ((MyHandler) delegate) .doSomething (request); return null;} 4. Loop execution of handler's pre interceptor / / 4. Pre interceptor for (int I = 0; I) looping through handler

< mappedHandler.getInterceptors().length; i++) { HandlerInterceptor interceptor = mappedHandler.getInterceptors()[i]; // pre 拦截器 if (!interceptor.preHandle(request, response, mappedHandler.getHandler())) { return; }} 这个没什么好讲的吧?2021Java面试宝典 5. 执行真正的 handler,并返回 ModelAndView(Handler 是个代理对象,可能会执行 AOP )// 5. 执行真正的 handler,并返回 ModelAndView(Handler 是个代理对象,可能会执行 AOP )ModelAndView mv = ha.handle(request, response, mappedHandler.getHandler()); 6. 循环执行 handler 的 post 拦截器 // 6. 循环执行 handler 的 post 拦截器for (int i = mappedHandler.getInterceptors().length - 1; i >

= 0; iMel -) {HandlerInterceptor interceptor = mappedHandler.getInterceptors () [I]; / post interceptor interceptor.postHandle (request, response, mappedHandler.getHandler ());} 7. According to the ModelAndView information, get the View instance View view = null;if (mv.isReference ()) {/ / We need to resolve this view name / / 7. According to the ModelAndView information, get the View instance view = this.viewResolver.resolveViewName (mv.getViewName (), locale);} 8. Rendering View returns / / 8. Rendering View returns view.render (mv.getModel (), request, response). At this point, I believe you have a better understanding of "the principle and process of Spring AOP". You might as well do it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report