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/02 Report--
What is the use of SpringExtensionFactory in dubbo? I believe many inexperienced people don't know what to do about it. Therefore, this article summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.
ExtensionFactory
Dubbo-2.7.3/dubbo-common/src/main/java/org/apache/dubbo/common/extension/ExtensionFactory.java
@ SPIpublic interface ExtensionFactory {/ * * Get extension. * * @ param type object type. * @ param name object name. * @ return object instance. * / T getExtension (Class type, String name);}
The ExtensionFactory interface defines the getExtension method
SpringExtensionFactory
Dubbo-2.7.3/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/extension/SpringExtensionFactory.java
Public class SpringExtensionFactory implements ExtensionFactory {private static final Logger logger = LoggerFactory.getLogger (SpringExtensionFactory.class); private static final Set CONTEXTS = new ConcurrentHashSet (); private static final ApplicationListener SHUTDOWN_HOOK_LISTENER = new ShutdownHookListener (); public static void addApplicationContext (ApplicationContext context) {CONTEXTS.add (context); if (context instanceof ConfigurableApplicationContext) {((ConfigurableApplicationContext) context). RegisterShutdownHook (); DubboShutdownHook.getDubboShutdownHook (). Unregister () } BeanFactoryUtils.addApplicationListener (context, SHUTDOWN_HOOK_LISTENER);} public static void removeApplicationContext (ApplicationContext context) {CONTEXTS.remove (context);} public static Set getContexts () {return CONTEXTS;} / / currently for test purpose public static void clearContexts () {CONTEXTS.clear () } @ Override @ SuppressWarnings ("unchecked") public T getExtension (Class type, String name) {/ / SPI should be get from SpiExtensionFactory if (type.isInterface () & & type.isAnnotationPresent (SPI.class)) {return null;} for (ApplicationContext context: CONTEXTS) {if (context.containsBean (name)) {Object bean = context.getBean (name) If (type.isInstance (bean)) {return (T) bean;} logger.warn ("No spring extension (bean) named:" + name + ", try to find an extension (bean) of type" + type.getName ()); if (Object.class = = type) {return null } for (ApplicationContext context: CONTEXTS) {try {return context.getBean (type);} catch (NoUniqueBeanDefinitionException multiBeanExe) {logger.warn ("Find more than 1 spring extensions (beans) of type" + type.getName () + ", will stop auto injection. Please make sure you have specified the concrete parameter type and there's only one extension of that type. ");} catch (NoSuchBeanDefinitionException noBeanExe) {if (logger.isDebugEnabled ()) {logger.debug (" Error when get spring extension (bean) for type: "+ type.getName (), noBeanExe) } logger.warn ("No spring extension (bean) named:" + name + ", type:" + type.getName () + "found, stop get bean."); return null;} private static class ShutdownHookListener implements ApplicationListener {@ Override public void onApplicationEvent (ApplicationEvent event) {if (event instanceof ContextClosedEvent) {DubboShutdownHook shutdownHook = DubboShutdownHook.getDubboShutdownHook () ShutdownHook.doDestroy ();}
SpringExtensionFactory implements the ExtensionFactory method, which provides an addApplicationContext static method to add ApplicationContext, and registers SHUTDOWN_HOOK_LISTENER;. Its getExtension method first obtains the bean from the ApplicationContext according to the name. If not, the bean is obtained in the ApplicationContext according to the type. If not, the null is returned.
SpringExtensionFactoryTest
Dubbo-2.7.3/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/extension/SpringExtensionFactoryTest.java
@ Configurationpublic class SpringExtensionFactoryTest {private SpringExtensionFactory springExtensionFactory = new SpringExtensionFactory (); private AnnotationConfigApplicationContext context1; private AnnotationConfigApplicationContext context2; @ BeforeEach public void init () {context1 = new AnnotationConfigApplicationContext (); context1.register (getClass ()); context1.refresh (); context2 = new AnnotationConfigApplicationContext (); context2.register (BeanForContext2.class); context2.refresh (); SpringExtensionFactory.addApplicationContext (context1); SpringExtensionFactory.addApplicationContext (context2) } @ Test public void testGetExtensionBySPI () {Protocol protocol = springExtensionFactory.getExtension (Protocol.class, "protocol"); Assertions.assertNull (protocol);} @ Test public void testGetExtensionByName () {DemoService bean = springExtensionFactory.getExtension (DemoService.class, "bean1"); Assertions.assertNotNull (bean) } @ Test public void testGetExtensionByTypeMultiple () {try {springExtensionFactory.getExtension (DemoService.class, "beanname-not-exist");} catch (Exception e) {e.printStackTrace (); Assertions.assertTrue (e instanceof NoUniqueBeanDefinitionException);}} @ Test public void testGetExtensionByType () {HelloService bean = springExtensionFactory.getExtension (HelloService.class, "beanname-not-exist") Assertions.assertNotNull (bean);} @ AfterEach public void destroy () {SpringExtensionFactory.clearContexts (); context1.close (); context2.close ();} @ Bean ("bean1") public DemoService bean1 () {return new DemoServiceImpl ();} @ Bean ("bean2") public DemoService bean2 () {return new DemoServiceImpl () @ Bean ("hello") public HelloService helloService () {return new HelloServiceImpl ();}}
After adding context1 and context2; to springExtensionFactory, SpringExtensionFactoryTest's init method verifies that extension is obtained according to SPI, name and type.
Summary
SpringExtensionFactory implements the ExtensionFactory method, which provides an addApplicationContext static method to add ApplicationContext, and registers SHUTDOWN_HOOK_LISTENER;. Its getExtension method first obtains the bean from the ApplicationContext according to the name. If not, the bean is obtained in the ApplicationContext according to the type. If not, the null is returned.
After reading the above, have you mastered the useful methods of SpringExtensionFactory in dubbo? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!
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.