SpringServiceManager implements ServiceManagerSystem { @SuppressWarnings("unchecked") @Override public <T> T getServiceByName(String name, Class<T> type) { T bean = null; if (ApplicationContext.class.getName().equals(name) && ApplicationContext.class.isAssignableFrom(type)) { bean = (T) getApplicationContext(); } else { if (name != null) { try { bean = (T) applicationContext.getBean(name, type); } catch (BeansException e) { log.info("Unable to locate bean by name or id=" + name + ". Will try to look up bean by type next."); bean = null; } } else { try { bean = (T) applicationContext.getBean(type.getName(), type); } catch (BeansException e) { log.info("Unable to locate bean by name or id=" + type.getName() + ". Will try to look up bean by type next."); bean = null; } } if (name == null && bean == null) { try { Map<String, T> map = applicationContext.getBeansOfType(type); if (map.size() == 1) { bean = (T) map.values().iterator().next(); } else { log.error("Multiple beans of type " + type.getName() + " found. Only one was expected!"); } } catch (BeansException e) { log.error(e.getMessage(), e); bean = null; } } } return bean; } SpringServiceManager(ServiceManagerSystem parent, DSpaceConfigurationService configurationService, boolean testMode, boolean developmentMode, String... configPaths); ClassPathXmlApplicationContext getApplicationContext(); ListableBeanFactory getBeanFactory(); @SuppressWarnings("unchecked") @Override T getServiceByName(String name, Class<T> type); @SuppressWarnings("unchecked") @Override List<T> getServicesByType(Class<T> type); @Override void shutdown(); @Override void startup(); @SuppressWarnings("unchecked") @Override T registerServiceClass(String name, Class<T> type); @Override void registerService(String name, Object service); @Override void registerServiceNoAutowire(String name, Object service); @Override void unregisterService(String name); @Override List<String> getServicesNames(); @Override boolean isServiceExists(String name); @Override Map<String, Object> getServices(); @Override void pushConfig(Map<String, Object> settings); static final String configPath; static final String coreResourcePath; static final String addonResourcePath; }
@Test public void testGetServiceByName() { configurationService.clear(); ssm.startup(); ConcreteExample concrete = ssm.getServiceByName(ConcreteExample.class.getName(), ConcreteExample.class); assertNotNull(concrete); assertEquals("azeckoski", concrete.getName()); concrete = null; SampleAnnotationBean sab = ssm.getServiceByName(SampleAnnotationBean.class.getName(), SampleAnnotationBean.class); assertNotNull(sab); assertEquals(null, sab.getSampleValue()); sab = null; }