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

Introduction of the SPI mechanism of Dubbo and the method of implementing AOP by Dubbo through Wrapper

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article mainly introduces "the introduction of Dubbo's SPI mechanism and the method of Dubbo realizing AOP through Wrapper". In daily operation, I believe many people have doubts about the introduction of Dubbo's SPI mechanism and the method of Dubbo realizing AOP through Wrapper. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "the introduction of Dubbo's SPI mechanism and Dubbo's method of realizing AOP through Wrapper". Next, please follow the editor to study!

1. Dubbo's example of implementing AOP through Wrapper

Reference: Dubbo's SPI mechanism analysis 2-Adaptive detailed explanation, Dubbo's SPI mechanism analysis 3-Dubbo 's IOC dependency injection

/ / add a Wrapper implementation class public class AdaptiveExtWrapper implements AdaptiveExt {private AdaptiveExt adaptiveExt; / / to the original interface and implementation class. The key to implementing a Wrapper class is to provide a constructor with similar parameters. The later code will analyze public AdaptiveExtWrapper (AdaptiveExt adaptiveExt) {this.adaptiveExt = adaptiveExt. } @ Override public String echo (String msg, URL url) {/ / do something, implementing AOP System.out.println ("before"); adaptiveExt.echo (msg, url); System.out.println ("after"); / / do something return "wrapper";}} public class DubboAdaptiveExt implements AdaptiveExt {private AdaptiveExt adaptiveExt / / here the DubboAdaptiveExt dependency injects an instance of type AdaptiveExt, where the test case injects ThriftAdaptiveExt public void setAdaptiveExt (AdaptiveExt adaptiveExt) {this.adaptiveExt = adaptiveExt;} @ Override public String echo (String msg, URL url) {System.out.println (this.adaptiveExt.echo (msg, url)); return "dubbo";} @ Testpublic void test1 () {ExtensionLoader loader = ExtensionLoader.getExtensionLoader (AdaptiveExt.class)

Also remember to add a new line to the configuration file of dubbo, otherwise the dubbo framework cannot scan the Wrapper

Dubbo=com.alibaba.dubbo.demo.provider.adaptive.impl.DubboAdaptiveExtcloud=com.alibaba.dubbo.demo.provider.adaptive.impl.SpringCloudAdaptiveExtthrift=com.alibaba.dubbo.demo.provider.adaptive.impl.ThriftAdaptiveExtcom.alibaba.dubbo.demo.provider.adaptive.impl.AdaptiveExtWrapper running code, output: beforethriftafter2, Dubbo through Wrapper to achieve AOP source code analysis

You can find that the above code output before, after, intermediate output thrift, which can actually show that adaptiveExtension = loader.getExtension ("dubbo") returns an instance of the AdaptiveExtWrapper type, a bit like a static proxy, the following analysis of the source code.

/ / Core code, delete some unimportant code private T createExtension (String name) {/ / here we also scan the files in the default directory of dubbo through dubbo's SPI mechanism to load the implementation classes that implement AdaptiveExt. Here we will / / load 4, including AdaptiveExtWrapper, which is the only element in the following wrapperClasses. Let's first analyze the loading process Class clazz = getExtensionClasses (). Get (name) Try {T instance = (T) EXTENSION_INSTANCES.get (clazz); if (instance = = null) {EXTENSION_INSTANCES.putIfAbsent (clazz, clazz.newInstance ()); instance = (T) EXTENSION_INSTANCES.get (clazz);} / / dependency injection, where ThriftAdaptiveExt instance object injectExtension (instance) is injected / / so cachedWrapperClasses contains the AdaptiveExtWrapper class Set > wrappers = cachedWrapperClasses; if (wrappers = = null) {cachedWrapperClasses = new ConcurrentHashSet

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