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 > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains the "DefaultAdvisorChainFactory class case study in Spring Framework". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "DefaultAdvisorChainFactory class case study in Spring Framework".
The Spring version is 5.0.4.release.
The interceptor chain is used in JdkDynamicAopProxy, where List-1,advised is ProxyFactory and method getInterceptorsAndDynamicInterceptionAdvice is in its parent class AdvisedSupport.
List-1
Public Object invoke (Object proxy, Method method, Object [] args) throws Throwable {. / / Get the interception chain for this method. List chain = this.advised.getInterceptorsAndDynamicInterceptionAdvice (method, targetClass);...}
The following List-2,advisorChainFactory is DefaultAdvisorChainFactory
List-2
Public List getInterceptorsAndDynamicInterceptionAdvice (Method method, @ Nullable Class targetClass) {MethodCacheKey cacheKey = new MethodCacheKey (method); List cached = this.methodCache.get (cacheKey); if (cached = = null) {cached = this.advisorChainFactory.getInterceptorsAndDynamicInterceptionAdvice (this, method, targetClass); this.methodCache.put (cacheKey, cached);} return cached;}
The method implementation of getInterceptorsAndDynamicInterceptionAdvice is shown in List-3 below, which converts Advisor to registry in Interceptor,List-3, which is DefaultAdvisorAdapterRegistry.
List-3
Public class DefaultAdvisorChainFactory implements AdvisorChainFactory, Serializable {@ Override public List getInterceptorsAndDynamicInterceptionAdvice (Advised config, Method method, @ Nullable Class targetClass) {. Interceptor [] interceptors = registry.getInterceptors (advisor);... Return interceptorList;}...
As shown in the following List-4, the Adapter is registered in the constructor, and then the template pattern is used in the getInterceptors method to convert the advice in advisor to MethodInterceptor.
List-4
Public class DefaultAdvisorAdapterRegistry implements AdvisorAdapterRegistry, Serializable {private final List adapters = new ArrayList (3); public DefaultAdvisorAdapterRegistry () {registerAdvisorAdapter (new MethodBeforeAdviceAdapter ()); registerAdvisorAdapter (new AfterReturningAdviceAdapter ()); registerAdvisorAdapter (new ThrowsAdviceAdapter ());} @ Override public MethodInterceptor [] getInterceptors (Advisor advisor) throws UnknownAdviceTypeException {List interceptors = new ArrayList (3) Advice advice = advisor.getAdvice (); if (advice instanceof MethodInterceptor) {interceptors.add ((MethodInterceptor) advice) } for (AdvisorAdapter adapter: this.adapters) {if (adapter.supportsAdvice (advice)) {interceptors.add (adapter.getInterceptor (advisor)) } if (interceptors.isEmpty ()) {throw new UnknownAdviceTypeException (advisor.getAdvice ());} return interceptors.toArray (new MethodInterceptor [0]);}.
For an example, as shown in List-5 below, the supportsAdvice method of MethodBeforeAdviceAdapter determines whether the advice is encapsulated in MethodBeforeAdviceInterceptor in the MethodBeforeAdvice;getInterceptor method.
List-5
Class MethodBeforeAdviceAdapter implements AdvisorAdapter, Serializable {@ Override public boolean supportsAdvice (Advice advice) {return (advice instanceof MethodBeforeAdvice);} @ Override public MethodInterceptor getInterceptor (Advisor advisor) {MethodBeforeAdvice advice = (MethodBeforeAdvice) advisor.getAdvice (); return new MethodBeforeAdviceInterceptor (advice);}}
The code for MethodBeforeAdviceInterceptor is as follows, which is relatively simple, mainly the invoke method. The before method of MethodBeforeAdvice is called first, and then the invoke method of MethodInvocation is executed.
List-6
Public class MethodBeforeAdviceInterceptor implements MethodInterceptor, BeforeAdvice, Serializable {private final MethodBeforeAdvice advice; public MethodBeforeAdviceInterceptor (MethodBeforeAdvice advice) {Assert.notNull (advice, "Advice must not be null"); this.advice = advice;} @ Override public Object invoke (MethodInvocation mi) throws Throwable {this.advice.before (mi.getMethod (), mi.getArguments (), mi.getThis ()) Return mi.proceed ();}}
On the whole, DefaultAdvisorChainFactory will convert our advice into MethodInerceptor, and different Advice implementations will have different MethodInterceptor implementations, thus realizing the interception of Before and After.
When you get the bean from the Spring container, the interceptor for the bean is constructed and encapsulated with the target class, and then when the method of the target class is called, the interceptor chain is constructed again. Why is the interceptor chain constructed again because some interceptors only act on part of the methods of the target class, but the interceptor based on the method level is cached.
Thank you for reading, the above is the content of "DefaultAdvisorChainFactory case study in Spring Framework". After the study of this article, I believe you have a deeper understanding of the problem of DefaultAdvisorChainFactory case analysis in Spring Framework, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.