/** * Obtain all beans of the given type as autowire candidates. * @param type the type of the bean * @return the target beans, or an empty Collection if no bean of this type is found * @throws BeansException if bean retrieval failed */ protected <T> Map<String, T> findAutowireCandidates(Class<T> type) throws BeansException { if (this.beanFactory == null) { throw new IllegalStateException("No BeanFactory configured - " + "override the getBeanOfType method or specify the 'beanFactory' property"); } return BeanFactoryUtils.beansOfTypeIncludingAncestors(this.beanFactory, type); }
/** * Detect beans of type {@link MappedInterceptor} and add them to the list of mapped interceptors. * <p>This is called in addition to any {@link MappedInterceptor MappedInterceptors} that may have been provided * via {@link #setInterceptors}, by default adding all beans of type {@link MappedInterceptor} * from the current context and its ancestors. Subclasses can override and refine this policy. * @param mappedInterceptors an empty list to add {@link MappedInterceptor} instances to */ protected void detectMappedInterceptors(List<HandlerInterceptor> mappedInterceptors) { mappedInterceptors.addAll( BeanFactoryUtils.beansOfTypeIncludingAncestors( obtainApplicationContext(), MappedInterceptor.class, true, false).values()); }
/** * Obtain the delegate PhaseListener beans from the Spring root WebApplicationContext. * @param facesContext the current JSF context * @return a Collection of PhaseListener objects * @see #getBeanFactory * @see org.springframework.beans.factory.ListableBeanFactory#getBeansOfType(Class) */ protected Collection<PhaseListener> getDelegates(FacesContext facesContext) { ListableBeanFactory bf = getBeanFactory(facesContext); return BeanFactoryUtils.beansOfTypeIncludingAncestors(bf, PhaseListener.class, true, false).values(); }
protected void initStrategies(ApplicationContext context) { Map<String, HandlerMapping> mappingBeans = BeanFactoryUtils.beansOfTypeIncludingAncestors( context, HandlerMapping.class, true, false); ArrayList<HandlerMapping> mappings = new ArrayList<>(mappingBeans.values()); AnnotationAwareOrderComparator.sort(mappings); this.handlerMappings = Collections.unmodifiableList(mappings); Map<String, HandlerAdapter> adapterBeans = BeanFactoryUtils.beansOfTypeIncludingAncestors( context, HandlerAdapter.class, true, false); this.handlerAdapters = new ArrayList<>(adapterBeans.values()); AnnotationAwareOrderComparator.sort(this.handlerAdapters); Map<String, HandlerResultHandler> beans = BeanFactoryUtils.beansOfTypeIncludingAncestors( context, HandlerResultHandler.class, true, false); this.resultHandlers = new ArrayList<>(beans.values()); AnnotationAwareOrderComparator.sort(this.resultHandlers); }
private static List<HandlerMapping> initHandlerMappings(ApplicationContext applicationContext) { Map<String, HandlerMapping> beans = BeanFactoryUtils.beansOfTypeIncludingAncestors( applicationContext, HandlerMapping.class, true, false); if (!beans.isEmpty()) { List<HandlerMapping> mappings = new ArrayList<>(beans.values()); AnnotationAwareOrderComparator.sort(mappings); return Collections.unmodifiableList(mappings); } return Collections.unmodifiableList(initFallback(applicationContext)); }
/** * Return a single bean of the given type or subtypes, also picking up beans * defined in ancestor bean factories if the current bean factory is a * HierarchicalBeanFactory. Useful convenience method when we expect a * single bean and don't care about the bean name. * <p>Does consider objects created by FactoryBeans, which means that FactoryBeans * will get initialized. If the object created by the FactoryBean doesn't match, * the raw FactoryBean itself will be matched against the type. * <p>This version of {@code beanOfTypeIncludingAncestors} automatically includes * prototypes and FactoryBeans. * <p><b>Note: Beans of the same name will take precedence at the 'lowest' factory level, * i.e. such beans will be returned from the lowest factory that they are being found in, * hiding corresponding beans in ancestor factories.</b> This feature allows for * 'replacing' beans by explicitly choosing the same bean name in a child factory; * the bean in the ancestor factory won't be visible then, not even for by-type lookups. * @param lbf the bean factory * @param type type of bean to match * @return the matching bean instance * @throws NoSuchBeanDefinitionException if no bean of the given type was found * @throws NoUniqueBeanDefinitionException if more than one bean of the given type was found * @throws BeansException if the bean could not be created * @see #beansOfTypeIncludingAncestors(ListableBeanFactory, Class) */ public static <T> T beanOfTypeIncludingAncestors(ListableBeanFactory lbf, Class<T> type) throws BeansException { Map<String, T> beansOfType = beansOfTypeIncludingAncestors(lbf, type); return uniqueBean(type, beansOfType); }
/** * Obtain the delegate PhaseListener beans from the Spring root WebApplicationContext. * @param facesContext the current JSF context * @return a Collection of PhaseListener objects * @see #getBeanFactory * @see org.springframework.beans.factory.ListableBeanFactory#getBeansOfType(Class) */ protected Collection<PhaseListener> getDelegates(FacesContext facesContext) { ListableBeanFactory bf = getBeanFactory(facesContext); return BeanFactoryUtils.beansOfTypeIncludingAncestors(bf, PhaseListener.class, true, false).values(); }
/** * Get optional Bean * * @param beanFactory {@link ListableBeanFactory} * @param beanName the name of Bean * @param beanType the {@link Class type} of Bean * @param <T> the {@link Class type} of Bean * @return A bean if present , or <code>null</code> * @since 2.6.6 */ public static <T> T getOptionalBean(ListableBeanFactory beanFactory, String beanName, Class<T> beanType) { String[] allBeanNames = beanNamesForTypeIncludingAncestors(beanFactory, beanType); if (!containsElement(allBeanNames, beanName)) { return null; } Map<String, T> beansOfType = beansOfTypeIncludingAncestors(beanFactory, beanType); return beansOfType.get(beanName); }
/** * Get optional Bean * * @param beanFactory {@link ListableBeanFactory} * @param beanName the name of Bean * @param beanType the {@link Class type} of Bean * @param <T> the {@link Class type} of Bean * @return A bean if present , or <code>null</code> * @since 2.6.6 */ public static <T> T getOptionalBean(ListableBeanFactory beanFactory, String beanName, Class<T> beanType) { String[] allBeanNames = beanNamesForTypeIncludingAncestors(beanFactory, beanType); if (!containsElement(allBeanNames, beanName)) { return null; } Map<String, T> beansOfType = beansOfTypeIncludingAncestors(beanFactory, beanType); return beansOfType.get(beanName); }
/** * Detect all PersistenceExceptionTranslators in the given BeanFactory. * @param beanFactory the ListableBeanFactory to obtaining all * PersistenceExceptionTranslators from * @return a chained PersistenceExceptionTranslator, combining all * PersistenceExceptionTranslators found in the factory * @see ChainedPersistenceExceptionTranslator */ protected PersistenceExceptionTranslator detectPersistenceExceptionTranslators(ListableBeanFactory beanFactory) { // Find all translators, being careful not to activate FactoryBeans. Map<String, PersistenceExceptionTranslator> pets = BeanFactoryUtils.beansOfTypeIncludingAncestors( beanFactory, PersistenceExceptionTranslator.class, false, false); ChainedPersistenceExceptionTranslator cpet = new ChainedPersistenceExceptionTranslator(); for (PersistenceExceptionTranslator pet : pets.values()) { cpet.addDelegate(pet); } return cpet; }
private static <B extends HttpSecurityBuilder<B>> OAuth2AuthorizedClientRepository getAuthorizedClientRepositoryBean(B builder) { Map<String, OAuth2AuthorizedClientRepository> authorizedClientRepositoryMap = BeanFactoryUtils.beansOfTypeIncludingAncestors( builder.getSharedObject(ApplicationContext.class), OAuth2AuthorizedClientRepository.class); if (authorizedClientRepositoryMap.size() > 1) { throw new NoUniqueBeanDefinitionException(OAuth2AuthorizedClientRepository.class, authorizedClientRepositoryMap.size(), "Expected single matching bean of type '" + OAuth2AuthorizedClientRepository.class.getName() + "' but found " + authorizedClientRepositoryMap.size() + ": " + StringUtils.collectionToCommaDelimitedString(authorizedClientRepositoryMap.keySet())); } return (!authorizedClientRepositoryMap.isEmpty() ? authorizedClientRepositoryMap.values().iterator().next() : null); }
@Override protected void initServletContext(ServletContext servletContext) { Collection<ViewResolver> matchingBeans = BeanFactoryUtils.beansOfTypeIncludingAncestors(obtainApplicationContext(), ViewResolver.class).values(); if (this.viewResolvers == null) { this.viewResolvers = new ArrayList<>(matchingBeans.size()); for (ViewResolver viewResolver : matchingBeans) { if (this != viewResolver) { this.viewResolvers.add(viewResolver); } } } else { for (int i = 0; i < this.viewResolvers.size(); i++) { ViewResolver vr = this.viewResolvers.get(i); if (matchingBeans.contains(vr)) { continue; } String name = vr.getClass().getName() + i; obtainApplicationContext().getAutowireCapableBeanFactory().initializeBean(vr, name); } } AnnotationAwareOrderComparator.sort(this.viewResolvers); this.cnmFactoryBean.setServletContext(servletContext); }
BeanFactoryUtils.beansOfTypeIncludingAncestors(context, HandlerAdapter.class, true, false); if (!matchingBeans.isEmpty()) { this.handlerAdapters = new ArrayList<>(matchingBeans.values());
@Override public void afterPropertiesSet() throws Exception { if (getApplication() == null) { Map<String, ApplicationConfig> applicationConfigMap = applicationContext == null ? null : BeanFactoryUtils.beansOfTypeIncludingAncestors(applicationContext, ApplicationConfig.class, false, false); if (applicationConfigMap != null && applicationConfigMap.size() > 0) { ApplicationConfig applicationConfig = null; for (ApplicationConfig config : applicationConfigMap.values()) { if (config.isDefault() == null || config.isDefault()) { if (applicationConfig != null) { throw new IllegalStateException("Duplicate application configs: " + applicationConfig + " and " + config); } applicationConfig = config; } } if (applicationConfig != null) { setApplication(applicationConfig); } } } }
@Override public void afterPropertiesSet() throws Exception { if (getApplication() == null) { Map<String, ApplicationConfig> applicationConfigMap = applicationContext == null ? null : BeanFactoryUtils.beansOfTypeIncludingAncestors(applicationContext, ApplicationConfig.class, false, false); if (applicationConfigMap != null && applicationConfigMap.size() > 0) { ApplicationConfig applicationConfig = null; for (ApplicationConfig config : applicationConfigMap.values()) { if (config.isDefault() == null || config.isDefault()) { if (applicationConfig != null) { throw new IllegalStateException("Duplicate application configs: " + applicationConfig + " and " + config); } applicationConfig = config; } } if (applicationConfig != null) { setApplication(applicationConfig); } } } }
@Override protected ProtectedResourceDetailsService createInstance() throws Exception { Map<String, ProtectedResourceDetails> detailsMap = BeanFactoryUtils.beansOfTypeIncludingAncestors((ListableBeanFactory) getBeanFactory(), ProtectedResourceDetails.class); InMemoryProtectedResourceDetailsService service = new InMemoryProtectedResourceDetailsService(); service.setResourceDetailsStore(detailsMap); return service; } }
@Test public void testNoBeansOfType() { StaticListableBeanFactory lbf = new StaticListableBeanFactory(); lbf.addBean("foo", new Object()); Map<String, ?> beans = BeanFactoryUtils.beansOfTypeIncludingAncestors(lbf, ITestBean.class, true, false); assertTrue(beans.isEmpty()); }
BeanFactoryUtils.beansOfTypeIncludingAncestors(this.listableBeanFactory, ITestBean.class, true, false); assertEquals(2, beans.size()); assertEquals(test3, beans.get("test3")); assertEquals(test, beans.get("test")); beans = BeanFactoryUtils.beansOfTypeIncludingAncestors(this.listableBeanFactory, ITestBean.class, false, false); assertEquals(1, beans.size()); assertEquals(test, beans.get("test")); beans = BeanFactoryUtils.beansOfTypeIncludingAncestors(this.listableBeanFactory, ITestBean.class, false, true); Object testFactory1 = this.listableBeanFactory.getBean("testFactory1"); assertEquals(2, beans.size()); assertEquals(testFactory1, beans.get("testFactory1")); beans = BeanFactoryUtils.beansOfTypeIncludingAncestors(this.listableBeanFactory, ITestBean.class, true, true); assertEquals(4, beans.size()); assertEquals(test3, beans.get("test3")); beans = BeanFactoryUtils.beansOfTypeIncludingAncestors(this.listableBeanFactory, DummyFactory.class, true, true); assertEquals(2, beans.size()); assertEquals(this.listableBeanFactory.getBean("&testFactory1"), beans.get("&testFactory1")); assertEquals(this.listableBeanFactory.getBean("&testFactory2"), beans.get("&testFactory2")); beans = BeanFactoryUtils.beansOfTypeIncludingAncestors(this.listableBeanFactory, FactoryBean.class, true, true); assertEquals(2, beans.size()); assertEquals(this.listableBeanFactory.getBean("&testFactory1"), beans.get("&testFactory1"));
private void assertOneMessageSourceOnly(ClassPathXmlApplicationContext ctx, Object myMessageSource) { String[] beanNamesForType = ctx.getBeanNamesForType(StaticMessageSource.class); assertEquals(1, beanNamesForType.length); assertEquals("myMessageSource", beanNamesForType[0]); beanNamesForType = ctx.getBeanNamesForType(StaticMessageSource.class, true, true); assertEquals(1, beanNamesForType.length); assertEquals("myMessageSource", beanNamesForType[0]); beanNamesForType = BeanFactoryUtils.beanNamesForTypeIncludingAncestors(ctx, StaticMessageSource.class); assertEquals(1, beanNamesForType.length); assertEquals("myMessageSource", beanNamesForType[0]); beanNamesForType = BeanFactoryUtils.beanNamesForTypeIncludingAncestors(ctx, StaticMessageSource.class, true, true); assertEquals(1, beanNamesForType.length); assertEquals("myMessageSource", beanNamesForType[0]); Map<?, StaticMessageSource> beansOfType = ctx.getBeansOfType(StaticMessageSource.class); assertEquals(1, beansOfType.size()); assertSame(myMessageSource, beansOfType.values().iterator().next()); beansOfType = ctx.getBeansOfType(StaticMessageSource.class, true, true); assertEquals(1, beansOfType.size()); assertSame(myMessageSource, beansOfType.values().iterator().next()); beansOfType = BeanFactoryUtils.beansOfTypeIncludingAncestors(ctx, StaticMessageSource.class); assertEquals(1, beansOfType.size()); assertSame(myMessageSource, beansOfType.values().iterator().next()); beansOfType = BeanFactoryUtils.beansOfTypeIncludingAncestors(ctx, StaticMessageSource.class, true, true); assertEquals(1, beansOfType.size()); assertSame(myMessageSource, beansOfType.values().iterator().next()); }
@Test public void testFindsBeansOfTypeWithStaticFactory() { StaticListableBeanFactory lbf = new StaticListableBeanFactory(); TestBean t1 = new TestBean(); TestBean t2 = new TestBean(); DummyFactory t3 = new DummyFactory(); DummyFactory t4 = new DummyFactory(); t4.setSingleton(false); lbf.addBean("t1", t1); lbf.addBean("t2", t2); lbf.addBean("t3", t3); lbf.addBean("t4", t4); Map<String, ?> beans = BeanFactoryUtils.beansOfTypeIncludingAncestors(lbf, ITestBean.class, true, true); assertEquals(4, beans.size()); assertEquals(t1, beans.get("t1")); assertEquals(t2, beans.get("t2")); assertEquals(t3.getObject(), beans.get("t3")); assertTrue(beans.get("t4") instanceof TestBean); beans = BeanFactoryUtils.beansOfTypeIncludingAncestors(lbf, DummyFactory.class, true, true); assertEquals(2, beans.size()); assertEquals(t3, beans.get("&t3")); assertEquals(t4, beans.get("&t4")); beans = BeanFactoryUtils.beansOfTypeIncludingAncestors(lbf, FactoryBean.class, true, true); assertEquals(2, beans.size()); assertEquals(t3, beans.get("&t3")); assertEquals(t4, beans.get("&t4")); }