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 use of ReflectiveMethodInvocation in SpringFramework

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

What is the use of ReflectiveMethodInvocation in SpringFramework, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.

The Spring version is 5.0.4.release.

ReflectiveMethodInvocation is an important class in AOP, which is used in the invoke method of JdkDynamicAopProxy, such as List-1

List-1

Public Object invoke (Object proxy, Method method, Object [] args) throws Throwable {. / / We need to create a method invocation... Invocation = new ReflectiveMethodInvocation (proxy, target, method, args, targetClass, chain); / / Proceed to the joinpoint through the interceptor chain. RetVal = invocation.proceed ();...}

As shown in figure 1 below

Figure 1

ReflectiveMethodInvocation implements the MethodInvocation of aop alliance, and indirectly implements Invocation and Joinpoint. As shown in List-2 below, target is the target class, targetClass is the target class, class,method is the target method, arguments is the corresponding method parameter, and interceptorsAndDynamicMethodMatchers is the corresponding interceptor.

List-2

Protected ReflectiveMethodInvocation (Object proxy, @ Nullable Object target, Method method, @ Nullable Object [] arguments, @ Nullable Class targetClass, List interceptorsAndDynamicMethodMatchers) {this.proxy = proxy; this.target = target; this.targetClass = targetClass; this.method = BridgeMethodResolver.findBridgedMethod (method); this.arguments = AopProxyUtils.adaptArgumentsIfNecessary (method, arguments); this.interceptorsAndDynamicMethodMatchers = interceptorsAndDynamicMethodMatchers;}

One of the important methods of ReflectiveMethodInvocation is proceed (). The following List-3,currentInterceptorIndex indicates that the interceptor is accessed, and if it is the last, call invokeJoinpoint (), as shown in List-4, to call the target method using reflection.

List-3

Public Object proceed () throws Throwable {/ / We start with an index of-1 and increment early. If (this.currentInterceptorIndex = = this.interceptorsAndDynamicMethodMatchers.size ()-1) {return invokeJoinpoint ();} Object interceptorOrInterceptionAdvice = this.interceptorsAndDynamicMethodMatchers.get (+ + this.currentInterceptorIndex); if (interceptorOrInterceptionAdvice instanceof InterceptorAndDynamicMethodMatcher) {/ / Evaluate dynamic method matcher here: static part will already have / / been evaluated and found to match. InterceptorAndDynamicMethodMatcher dm = (InterceptorAndDynamicMethodMatcher) interceptorOrInterceptionAdvice; if (dm.methodMatcher.matches (this.method, this.targetClass, this.arguments)) {return dm.interceptor.invoke (this);} else {/ / Dynamic matching failed. / / Skip this interceptor and invoke the next in the chain. Return proceed ();}} else {/ / It's an interceptor, so we just invoke it: The pointcut will have / / been evaluated statically before this object was constructed. Return ((MethodInterceptor) interceptorOrInterceptionAdvice) .invoke (this);}}

In List-3, if the interceptor is not finished, the next interceptor is called recursively.

If it is InterceptorAndDynamicMethodMatcher, get its MethodMatcher to determine whether it is match, and if match, call its MethodInterceptor.

If not, the invoke method of MethodInterceptor is called directly, and this is passed into the invoke method, and the next one is called recursively, which is similar to FilterProxy in Spring security. You can look at the implementation of a MethodInterceptor implementation class AspectJAfterAdvice, as shown in List-5.

List-4

Protected Object invokeJoinpoint () throws Throwable {return AopUtils.invokeJoinpointUsingReflection (this.target, this.method, this.arguments);} public static Object invokeJoinpointUsingReflection (@ Nullable Object target, Method method, Object [] args) throws Throwable {. ReflectionUtils.makeAccessible (method); return method.invoke (target, args);...}

In the following List-5, proceed () is called first, and then the invokeAdviceMethod method is executed.

List-5

Public class AspectJAfterAdvice extends AbstractAspectJAdvice implements MethodInterceptor, AfterAdvice, Serializable {... @ Override public Object invoke (MethodInvocation mi) throws Throwable {try {return mi.proceed ();} finally {invokeAdviceMethod (getJoinPointMatch (), null, null) }}...

In general, Spring aop constructs an interceptor chain that is called when a dynamic proxy is called, and executes before and after the target method, according to our defined aop.

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

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

Internet Technology

Wechat

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

12
Report