### Question:
SpringExtensionFactory implements ExtensionFactory { @Override @SuppressWarnings("unchecked") public <T> T getExtension(Class<T> type, String name) { 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; }  static void addApplicationContext(ApplicationContext context); static void removeApplicationContext(ApplicationContext context); static Set<ApplicationContext> getContexts(); static void clearContexts(); @Override @SuppressWarnings("unchecked") T getExtension(Class<T> type, String name);  }### Answer:
@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); }