In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces you how to understand the principle of Spring AOP and SpringMVC process, the content is very detailed, interested friends can refer to, hope to be helpful to you.
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. In fact, the landlord has written a simple example before, address: http://thinkinjava.cn/2018/10/ uses-Cglib- to implement multiple proxies /
It's even easier after reading it.
AOP can be divided into two parts to pull, oh, no, to analyze. * *: creation of proxy; second, invocation of proxy.
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, you need to keep some simplicity here. If you want to know the details, look at the interface 21 source code, for more details, look at the master branch code of Spring Framework.
Code location: under the com.interface21.aop package.
Start the analysis (pull):
1. 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.
2. 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 executes to *, the default interceptor at the tail when the proxy is created is triggered, thus calling the target method. * * return.
Beside the point: Spring's transaction is just an interceptor.
Let's take a non-standard UML diagram:
For the calling process, take a flowchart:
This is probably the way it is. For more details, please see the source code. If you do not understand it, please consult me. I am not sure whether this picture is very easy to understand-at least it can be called simple and easy to understand if it is cute and new.
Spring MVC process
Let's start with a picture:
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 * * code and does not accept rebuttal.)
The code is as follows:
1. Set properti
/ / 1. Set properties / / Make web application context available request.setAttribute (WEB_APPLICATION_CONTEXT_ATTRIBUTE, getWebApplicationContext ()); / / Make locale resolver available request.setAttribute (LOCALE_RESOLVER_ATTRIBUTE, this.localeResolver); / / Make theme resolver available request.setAttribute (THEME_RESOLVER_ATTRIBUTE, this.themeResolver)
two。 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 for handler
/ / This will throw an exception if no adapter is found / / 3. 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. Pre interceptor for loop execution of handler
/ / 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; } } 这个没什么好讲的吧? 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. Get View instance based on ModelAndView information
View view = null; if (mv.isReference ()) {/ / We need to resolve this view name / / 7. According to ModelAndView information, get View instance view = this.viewResolver.resolveViewName (mv.getViewName (), locale);}
8. Render View returns
/ / 8. Rendering View returns view.render (mv.getModel (), request, response). This is the end of how to understand the principle of Spring AOP and the SpringMVC process. I hope the above content can be helpful to you and you can learn more. If you think the article is good, you can share it for more people to see.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.