Refine search
/** * 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); }
/** * 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); } }
protected ScriptTemplateConfig autodetectViewConfig() throws BeansException { try { return BeanFactoryUtils.beanOfTypeIncludingAncestors( obtainApplicationContext(), ScriptTemplateConfig.class, true, false); } catch (NoSuchBeanDefinitionException ex) { throw new ApplicationContextException("Expected a single ScriptTemplateConfig bean in the current " + "web application context or the parent root context: ScriptTemplateConfigurer is " + "the usual implementation. This bean may have any name.", ex); } }
/** * 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); }
/** * 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); }
@Override public Class<?> getType(String name) throws NoSuchBeanDefinitionException { String beanName = BeanFactoryUtils.transformedBeanName(name); Object bean = this.beans.get(beanName); if (bean == null) { throw new NoSuchBeanDefinitionException(beanName, "Defined beans are [" + StringUtils.collectionToCommaDelimitedString(this.beans.keySet()) + "]"); } if (bean instanceof FactoryBean && !BeanFactoryUtils.isFactoryDereference(name)) { // If it's a FactoryBean, we want to look at what it creates, not the factory class. return ((FactoryBean<?>) bean).getObjectType(); } return bean.getClass(); }
result.putAll(lbf.getBeansOfType(type)); if (lbf instanceof HierarchicalBeanFactory) { HierarchicalBeanFactory hbf = (HierarchicalBeanFactory) lbf; if (hbf.getParentBeanFactory() instanceof ListableBeanFactory) { Map<String, T> parentResult = beansOfTypeIncludingAncestors( (ListableBeanFactory) hbf.getParentBeanFactory(), type); parentResult.forEach((beanName, beanInstance) -> {
/** * 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; }
/** * 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); }
String[] candidateBeans = BeanFactoryUtils.beanNamesForTypeIncludingAncestors(bf, beanType); String matchingBean = null; for (String beanName : candidateBeans) { return bf.getBean(matchingBean, beanType); else if (bf.containsBean(qualifier)) { return bf.getBean(qualifier, beanType);
@Bean public NacosConfigProperties nacosConfigProperties(ApplicationContext context) { if (context.getParent() != null && BeanFactoryUtils.beanNamesForTypeIncludingAncestors( context.getParent(), NacosConfigProperties.class).length > 0) { return BeanFactoryUtils.beanOfTypeIncludingAncestors(context.getParent(), NacosConfigProperties.class); } NacosConfigProperties nacosConfigProperties = new NacosConfigProperties(); return nacosConfigProperties; }
/** * Return all bean names for bean definitions that will result in an {@link EntityManagerFactory} eventually. We're * checking for {@link EntityManagerFactory} and the well-known factory beans here to avoid eager initialization of * the factory beans. The double lookup is necessary especially for JavaConfig scenarios as people might declare an * {@link EntityManagerFactory} directly. * * @param beanFactory * @return */ public static Iterable<String> getEntityManagerFactoryBeanNames(ListableBeanFactory beanFactory) { String[] beanNames = beanNamesForTypeIncludingAncestors(beanFactory, EntityManagerFactory.class, true, false); Set<String> names = new HashSet<>(asList(beanNames)); for (String factoryBeanName : beanNamesForTypeIncludingAncestors(beanFactory, AbstractEntityManagerFactoryBean.class, true, false)) { names.add(transformedBeanName(factoryBeanName)); } return names; }
List<Advisor> advisors = new ArrayList<>(); aspectNames = new ArrayList<>(); String[] beanNames = BeanFactoryUtils.beanNamesForTypeIncludingAncestors( this.beanFactory, Object.class, true, false); for (String beanName : beanNames) { Class<?> beanType = this.beanFactory.getType(beanName); if (beanType == null) { continue; new BeanFactoryAspectInstanceFactory(this.beanFactory, beanName); List<Advisor> classAdvisors = this.advisorFactory.getAdvisors(factory); if (this.beanFactory.isSingleton(beanName)) { this.advisorsCache.put(beanName, classAdvisors); if (this.beanFactory.isSingleton(beanName)) { throw new IllegalArgumentException("Bean with name '" + beanName + "' is a singleton, but aspect instantiation model is not singleton");
@SuppressWarnings("unchecked") private Object invokeServiceLocatorMethod(Method method, Object[] args) throws Exception { Class serviceLocatorMethodReturnType = getServiceLocatorMethodReturnType(method); try { String beanName = tryGetBeanName(args); if (StringUtils.hasLength(beanName)) { // Service locator for a specific bean name. return beanFactory.getBean(beanName, serviceLocatorMethodReturnType); } else { // Service locator for a bean type. return BeanFactoryUtils.beanOfTypeIncludingAncestors(beanFactory, serviceLocatorMethodReturnType); } } catch (BeansException ex) { if (serviceLocatorExceptionConstructor != null) { throw createServiceLocatorException(serviceLocatorExceptionConstructor, ex); } throw ex; } }
/** * Return the bean name, stripping out the factory dereference prefix if necessary, * and resolving aliases to canonical names. * @param name the user-specified name * @return the transformed bean name */ protected String transformedBeanName(String name) { return canonicalName(BeanFactoryUtils.transformedBeanName(name)); }
/** * Count all beans in any hierarchy in which this factory participates. * Includes counts of ancestor bean factories. * <p>Beans that are "overridden" (specified in a descendant factory * with the same name) are only counted once. * @param lbf the bean factory * @return count of beans including those defined in ancestor factories * @see #beanNamesIncludingAncestors */ public static int countBeansIncludingAncestors(ListableBeanFactory lbf) { return beanNamesIncludingAncestors(lbf).length; }
result.putAll(lbf.getBeansOfType(type, includeNonSingletons, allowEagerInit)); if (lbf instanceof HierarchicalBeanFactory) { HierarchicalBeanFactory hbf = (HierarchicalBeanFactory) lbf; if (hbf.getParentBeanFactory() instanceof ListableBeanFactory) { Map<String, T> parentResult = beansOfTypeIncludingAncestors( (ListableBeanFactory) hbf.getParentBeanFactory(), type, includeNonSingletons, allowEagerInit); parentResult.forEach((beanName, beanInstance) -> {
/** * 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 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; }
private String[] getBeanNamesForTypedStream(ResolvableType requiredType) { return BeanFactoryUtils.beanNamesForTypeIncludingAncestors(this, requiredType); }