/** * Return all bean names in the factory, including ancestor factories. * @param lbf the bean factory * @return the array of matching bean names, or an empty array if none * @see #beanNamesForTypeIncludingAncestors */ public static String[] beanNamesIncludingAncestors(ListableBeanFactory lbf) { return beanNamesForTypeIncludingAncestors(lbf, Object.class); }
private String[] getBeanNamesForTypedStream(ResolvableType requiredType) { return BeanFactoryUtils.beanNamesForTypeIncludingAncestors(this, requiredType); }
private boolean checkBeanOfType(Class<?> beanType) { return (this.applicationContext == null || !ObjectUtils.isEmpty(BeanFactoryUtils.beanNamesForTypeIncludingAncestors( this.applicationContext, beanType, false, false))); }
private boolean checkBeanOfType(Class<?> beanType) { return (this.applicationContext == null || !ObjectUtils.isEmpty(BeanFactoryUtils.beanNamesForTypeIncludingAncestors( this.applicationContext, beanType, false, false))); }
/** * Find the names of beans annotated with * {@linkplain ControllerAdvice @ControllerAdvice} in the given * ApplicationContext and wrap them as {@code ControllerAdviceBean} instances. */ public static List<ControllerAdviceBean> findAnnotatedBeans(ApplicationContext context) { return Arrays.stream(BeanFactoryUtils.beanNamesForTypeIncludingAncestors(context, Object.class)) .filter(name -> context.findAnnotationOnBean(name, ControllerAdvice.class) != null) .map(name -> new ControllerAdviceBean(name, context)) .collect(Collectors.toList()); }
/** * Determine the names of candidate beans in the application context. * @since 5.1 * @see #setDetectHandlerMethodsInAncestorContexts * @see BeanFactoryUtils#beanNamesForTypeIncludingAncestors */ protected String[] getCandidateBeanNames() { return (this.detectHandlerMethodsInAncestorContexts ? BeanFactoryUtils.beanNamesForTypeIncludingAncestors(obtainApplicationContext(), Object.class) : obtainApplicationContext().getBeanNamesForType(Object.class)); }
/** * Gets name-matched Beans from {@link ListableBeanFactory BeanFactory} * * @param beanFactory {@link ListableBeanFactory BeanFactory} * @param beanNames the names of Bean * @param beanType the {@link Class type} of Bean * @param <T> the {@link Class type} of Bean * @return the read-only and non-null {@link List} of Bean names * @since 2.6.6 */ public static <T> List<T> getBeans(ListableBeanFactory beanFactory, String[] beanNames, Class<T> beanType) { String[] allBeanNames = beanNamesForTypeIncludingAncestors(beanFactory, beanType); List<T> beans = new ArrayList<T>(beanNames.length); for (String beanName : beanNames) { if (containsElement(allBeanNames, beanName)) { beans.add(beanFactory.getBean(beanName, beanType)); } } return Collections.unmodifiableList(beans); } }
/** * 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); }
/** * Gets name-matched Beans from {@link ListableBeanFactory BeanFactory} * * @param beanFactory {@link ListableBeanFactory BeanFactory} * @param beanNames the names of Bean * @param beanType the {@link Class type} of Bean * @param <T> the {@link Class type} of Bean * @return the read-only and non-null {@link List} of Bean names * @since 2.6.6 */ public static <T> List<T> getBeans(ListableBeanFactory beanFactory, String[] beanNames, Class<T> beanType) { String[] allBeanNames = beanNamesForTypeIncludingAncestors(beanFactory, beanType); List<T> beans = new ArrayList<T>(beanNames.length); for (String beanName : beanNames) { if (containsElement(allBeanNames, beanName)) { beans.add(beanFactory.getBean(beanName, beanType)); } } return Collections.unmodifiableList(beans); } }
/** * Add all global interceptors and pointcuts. */ private void addGlobalAdvisor(ListableBeanFactory beanFactory, String prefix) { String[] globalAdvisorNames = BeanFactoryUtils.beanNamesForTypeIncludingAncestors(beanFactory, Advisor.class); String[] globalInterceptorNames = BeanFactoryUtils.beanNamesForTypeIncludingAncestors(beanFactory, Interceptor.class); List<Object> beans = new ArrayList<>(globalAdvisorNames.length + globalInterceptorNames.length); Map<Object, String> names = new HashMap<>(beans.size()); for (String name : globalAdvisorNames) { Object bean = beanFactory.getBean(name); beans.add(bean); names.put(bean, name); } for (String name : globalInterceptorNames) { Object bean = beanFactory.getBean(name); beans.add(bean); names.put(bean, name); } AnnotationAwareOrderComparator.sort(beans); for (Object bean : beans) { String name = names.get(bean); if (name.startsWith(prefix)) { addAdvisorOnChainCreation(bean, name); } } }
/** * Retrieve all bean of type {@code T} from the given {@code BeanFactory} declaring a * qualifier (e.g. via {@code <qualifier>} or {@code @Qualifier}) matching the given * qualifier, or having a bean name matching the given qualifier. * @param beanFactory the factory to get the target beans from (also searching ancestors) * @param beanType the type of beans to retrieve * @param qualifier the qualifier for selecting among all type matches * @return the matching beans of type {@code T} * @throws BeansException if any of the matching beans could not be created * @since 5.1.1 * @see BeanFactoryUtils#beansOfTypeIncludingAncestors(ListableBeanFactory, Class) */ public static <T> Map<String, T> qualifiedBeansOfType( ListableBeanFactory beanFactory, Class<T> beanType, String qualifier) throws BeansException { String[] candidateBeans = BeanFactoryUtils.beanNamesForTypeIncludingAncestors(beanFactory, beanType); Map<String, T> result = new LinkedHashMap<>(4); for (String beanName : candidateBeans) { if (isQualifierMatch(qualifier::equals, beanName, beanFactory)) { result.put(beanName, beanFactory.getBean(beanName, beanType)); } } return result; }
/** * Register all handlers found in the current ApplicationContext. * <p>The actual URL determination for a handler is up to the concrete * {@link #determineUrlsForHandler(String)} implementation. A bean for * which no such URLs could be determined is simply not considered a handler. * @throws org.springframework.beans.BeansException if the handler couldn't be registered * @see #determineUrlsForHandler(String) */ protected void detectHandlers() throws BeansException { ApplicationContext applicationContext = obtainApplicationContext(); String[] beanNames = (this.detectHandlersInAncestorContexts ? BeanFactoryUtils.beanNamesForTypeIncludingAncestors(applicationContext, Object.class) : applicationContext.getBeanNamesForType(Object.class)); // Take any bean name that we can determine URLs for. for (String beanName : beanNames) { String[] urls = determineUrlsForHandler(beanName); if (!ObjectUtils.isEmpty(urls)) { // URL paths found: Let's consider it a handler. registerHandler(urls, beanName); } } if ((logger.isDebugEnabled() && !getHandlerMap().isEmpty()) || logger.isTraceEnabled()) { logger.debug("Detected " + getHandlerMap().size() + " mappings in " + formatMappingName()); } }
@Test public void testHierarchicalNamesWithNoMatch() throws Exception { List<String> names = Arrays.asList( BeanFactoryUtils.beanNamesForTypeIncludingAncestors(this.listableBeanFactory, NoOp.class)); assertEquals(0, names.size()); }
/** * Get all bean names for the given type, including those defined in ancestor * factories. Will return unique names in case of overridden bean definitions. * <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 beanNamesForTypeIncludingAncestors} automatically * includes prototypes and FactoryBeans. * @param lbf the bean factory * @param type the type that beans must match (as a {@code Class}) * @return the array of matching bean names, or an empty array if none * @see ListableBeanFactory#getBeanNamesForType(Class) */ public static String[] beanNamesForTypeIncludingAncestors(ListableBeanFactory lbf, Class<?> type) { Assert.notNull(lbf, "ListableBeanFactory must not be null"); String[] result = lbf.getBeanNamesForType(type); if (lbf instanceof HierarchicalBeanFactory) { HierarchicalBeanFactory hbf = (HierarchicalBeanFactory) lbf; if (hbf.getParentBeanFactory() instanceof ListableBeanFactory) { String[] parentResult = beanNamesForTypeIncludingAncestors( (ListableBeanFactory) hbf.getParentBeanFactory(), type); result = mergeNamesWithParent(result, parentResult, hbf); } } return result; }
/** * Get all bean names for the given type, including those defined in ancestor * factories. Will return unique names in case of overridden bean definitions. * <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 beanNamesForTypeIncludingAncestors} automatically * includes prototypes and FactoryBeans. * @param lbf the bean factory * @param type the type that beans must match (as a {@code ResolvableType}) * @return the array of matching bean names, or an empty array if none * @since 4.2 * @see ListableBeanFactory#getBeanNamesForType(ResolvableType) */ public static String[] beanNamesForTypeIncludingAncestors(ListableBeanFactory lbf, ResolvableType type) { Assert.notNull(lbf, "ListableBeanFactory must not be null"); String[] result = lbf.getBeanNamesForType(type); if (lbf instanceof HierarchicalBeanFactory) { HierarchicalBeanFactory hbf = (HierarchicalBeanFactory) lbf; if (hbf.getParentBeanFactory() instanceof ListableBeanFactory) { String[] parentResult = beanNamesForTypeIncludingAncestors( (ListableBeanFactory) hbf.getParentBeanFactory(), type); result = mergeNamesWithParent(result, parentResult, hbf); } } return result; }
@Test public void testGetBeanNamesForTypeWithOverride() throws Exception { List<String> names = Arrays.asList( BeanFactoryUtils.beanNamesForTypeIncludingAncestors(this.listableBeanFactory, ITestBean.class)); // includes 2 TestBeans from FactoryBeans (DummyFactory definitions) assertEquals(4, names.size()); assertTrue(names.contains("test")); assertTrue(names.contains("test3")); assertTrue(names.contains("testFactory1")); assertTrue(names.contains("testFactory2")); }
@Test public void testHierarchicalNamesWithMatchOnlyInRoot() throws Exception { List<String> names = Arrays.asList( BeanFactoryUtils.beanNamesForTypeIncludingAncestors(this.listableBeanFactory, IndexedTestBean.class)); assertEquals(1, names.size()); assertTrue(names.contains("indexedBean")); // Distinguish from default ListableBeanFactory behavior assertTrue(listableBeanFactory.getBeanNamesForType(IndexedTestBean.class).length == 0); }
String[] candidateBeans = BeanFactoryUtils.beanNamesForTypeIncludingAncestors(bf, beanType); String matchingBean = null; for (String beanName : candidateBeans) {
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()); }