List of usage examples for org.springframework.beans.factory.support DefaultListableBeanFactory getBeansOfType
@Override public <T> Map<String, T> getBeansOfType(@Nullable Class<T> type) throws BeansException
From source file:org.spring.guice.module.SpringModule.java
public SpringModule(DefaultListableBeanFactory beanFactory) { this.beanFactory = beanFactory; if (beanFactory.getBeanNamesForType(GuiceModuleMetadata.class).length > 0) { this.matcher = new CompositeTypeMatcher(beanFactory.getBeansOfType(GuiceModuleMetadata.class).values()); }/* w w w . j a v a 2 s.com*/ }
From source file:com.helpinput.spring.refresher.mvc.MvcContextRefresher.java
@Override public void refresh(ApplicationContext context, Map<Class<?>, ScanedType> scanedClasses) { boolean needUpdateHandlerMapping = false; boolean needUpdateInterceptor = false; for (Entry<Class<?>, ScanedType> entry : scanedClasses.entrySet()) { if ((entry.getValue().getValue() > ScanedType.SAME.getValue()) && entry.getKey().getAnnotation(Controller.class) != null) { needUpdateHandlerMapping = true; break; }/*from w w w .j a v a 2 s . c o m*/ } for (Entry<Class<?>, ScanedType> entry : scanedClasses.entrySet()) { if ((entry.getValue().getValue() > ScanedType.SAME.getValue()) && HandlerInterceptor.class.isAssignableFrom(entry.getKey())) { needUpdateInterceptor = true; break; } } if (needUpdateInterceptor) { DefaultListableBeanFactory dlbf = (DefaultListableBeanFactory) ((AbstractApplicationContext) context) .getBeanFactory(); Map<String, AbstractHandlerMapping> mappings = dlbf.getBeansOfType(AbstractHandlerMapping.class); if (Utils.hasLength(mappings)) { Field interceptorsField = Utils.findField(AbstractHandlerMapping.class, "interceptors"); Field mappedInterceptorsFeild = Utils.findField(AbstractHandlerMapping.class, "mappedInterceptors"); Method initApplicationContext = Utils.findMethod(AbstractHandlerMapping.class, "initApplicationContext"); if (interceptorsField != null && mappedInterceptorsFeild != null && initApplicationContext != null) { for (AbstractHandlerMapping mapping : mappings.values()) { synchronized (mapping) { final List<Object> interceptors = Utils.getFieldValue(mapping, interceptorsField); if (Utils.hasLength(interceptors)) interceptors.clear(); final List<MappedInterceptor> mappedInterceptors = Utils.getFieldValue(mapping, mappedInterceptorsFeild); if (Utils.hasLength(mappedInterceptors)) mappedInterceptors.clear(); Utils.InvokedMethod(mapping, initApplicationContext); } } } } } if (needUpdateHandlerMapping) { final String mapName = "org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping#0"; Object mappingHandler = context.getBean(mapName); if (mappingHandler != null) { Method method = Utils.findMethod(mappingHandler, "initHandlerMethods"); Map<?, ?> handlerMethods = Utils.getFieldValue(mappingHandler, "handlerMethods"); Map<?, ?> urlMap = Utils.getFieldValue(mappingHandler, "urlMap"); if (method != null && handlerMethods != null && urlMap != null) { synchronized (mappingHandler) { handlerMethods.clear(); urlMap.clear(); Utils.InvokedMethod(mappingHandler, method); } } } } }
From source file:org.springframework.beans.factory.DefaultListableBeanFactoryTests.java
@Test public void testCircularReferenceThroughFactoryBeanTypeCheck() { DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); RootBeanDefinition bd = new RootBeanDefinition(ConstructorDependencyFactoryBean.class); bd.setAutowireMode(RootBeanDefinition.AUTOWIRE_CONSTRUCTOR); lbf.registerBeanDefinition("test", bd); try {/*from w w w . jav a2s . c o m*/ lbf.getBeansOfType(String.class); fail("Should have thrown UnsatisfiedDependencyException"); } catch (UnsatisfiedDependencyException expected) { } }
From source file:org.springframework.beans.factory.xml.XmlBeanFactoryTests.java
@Test public void testAbstractParentBeans() { DefaultListableBeanFactory parent = new DefaultListableBeanFactory(); new XmlBeanDefinitionReader(parent).loadBeanDefinitions(PARENT_CONTEXT); parent.preInstantiateSingletons();/*from w w w .j a v a2 s.c o m*/ assertTrue(parent.isSingleton("inheritedTestBeanWithoutClass")); // abstract beans should not match Map<?, ?> tbs = parent.getBeansOfType(TestBean.class); assertEquals(2, tbs.size()); assertTrue(tbs.containsKey("inheritedTestBeanPrototype")); assertTrue(tbs.containsKey("inheritedTestBeanSingleton")); // abstract bean should throw exception on creation attempt try { parent.getBean("inheritedTestBeanWithoutClass"); fail("Should have thrown BeanIsAbstractException"); } catch (BeanIsAbstractException ex) { // expected } // non-abstract bean should work, even if it serves as parent assertTrue(parent.getBean("inheritedTestBeanPrototype") instanceof TestBean); }