@Test public void testHierarchicalNamesForAnnotationWithNoMatch() throws Exception { List<String> names = Arrays.asList( BeanFactoryUtils.beanNamesForAnnotationIncludingAncestors(this.listableBeanFactory, Override.class)); assertEquals(0, names.size()); }
/** * Get all bean names whose {@code Class} has the supplied {@link Annotation} * type, including those defined in ancestor factories, without creating any bean * instances yet. Will return unique names in case of overridden bean definitions. * @param lbf the bean factory * @param annotationType the type of annotation to look for * @return the array of matching bean names, or an empty array if none * @since 5.0 * @see ListableBeanFactory#getBeanNamesForAnnotation(Class) */ public static String[] beanNamesForAnnotationIncludingAncestors( ListableBeanFactory lbf, Class<? extends Annotation> annotationType) { Assert.notNull(lbf, "ListableBeanFactory must not be null"); String[] result = lbf.getBeanNamesForAnnotation(annotationType); if (lbf instanceof HierarchicalBeanFactory) { HierarchicalBeanFactory hbf = (HierarchicalBeanFactory) lbf; if (hbf.getParentBeanFactory() instanceof ListableBeanFactory) { String[] parentResult = beanNamesForAnnotationIncludingAncestors( (ListableBeanFactory) hbf.getParentBeanFactory(), annotationType); result = mergeNamesWithParent(result, parentResult, hbf); } } return result; }
@Test public void testHierarchicalNamesForAnnotationWithMatchOnlyInRoot() throws Exception { List<String> names = Arrays.asList( BeanFactoryUtils.beanNamesForAnnotationIncludingAncestors(this.listableBeanFactory, TestAnnotation.class)); assertEquals(1, names.size()); assertTrue(names.contains("annotatedBean")); // Distinguish from default ListableBeanFactory behavior assertTrue(listableBeanFactory.getBeanNamesForAnnotation(TestAnnotation.class).length == 0); }
@Test public void testGetBeanNamesForAnnotationWithOverride() throws Exception { AnnotatedBean annotatedBean = new AnnotatedBean(); this.listableBeanFactory.registerSingleton("anotherAnnotatedBean", annotatedBean); List<String> names = Arrays.asList( BeanFactoryUtils.beanNamesForAnnotationIncludingAncestors(this.listableBeanFactory, TestAnnotation.class)); assertEquals(2, names.size()); assertTrue(names.contains("annotatedBean")); assertTrue(names.contains("anotherAnnotatedBean")); }
/** * Get all bean names whose {@code Class} has the supplied {@link Annotation} * type, including those defined in ancestor factories, without creating any bean * instances yet. Will return unique names in case of overridden bean definitions. * @param lbf the bean factory * @param annotationType the type of annotation to look for * @return the array of matching bean names, or an empty array if none * @since 5.0 * @see ListableBeanFactory#getBeanNamesForAnnotation(Class) */ public static String[] beanNamesForAnnotationIncludingAncestors( ListableBeanFactory lbf, Class<? extends Annotation> annotationType) { Assert.notNull(lbf, "ListableBeanFactory must not be null"); String[] result = lbf.getBeanNamesForAnnotation(annotationType); if (lbf instanceof HierarchicalBeanFactory) { HierarchicalBeanFactory hbf = (HierarchicalBeanFactory) lbf; if (hbf.getParentBeanFactory() instanceof ListableBeanFactory) { String[] parentResult = beanNamesForAnnotationIncludingAncestors( (ListableBeanFactory) hbf.getParentBeanFactory(), annotationType); result = mergeNamesWithParent(result, parentResult, hbf); } } return result; }
private void addExtensionBeans(Collection<EndpointBean> endpointBeans) { Map<EndpointId, EndpointBean> byId = endpointBeans.stream() .collect(Collectors.toMap(EndpointBean::getId, Function.identity())); String[] beanNames = BeanFactoryUtils.beanNamesForAnnotationIncludingAncestors( this.applicationContext, EndpointExtension.class); for (String beanName : beanNames) { ExtensionBean extensionBean = createExtensionBean(beanName); EndpointBean endpointBean = byId.get(extensionBean.getEndpointId()); Assert.state(endpointBean != null, () -> ("Invalid extension '" + extensionBean.getBeanName() + "': no endpoint found with id '" + extensionBean.getEndpointId() + "'")); addExtensionBean(endpointBean, extensionBean); } }
private Collection<EndpointBean> createEndpointBeans() { Map<EndpointId, EndpointBean> byId = new LinkedHashMap<>(); String[] beanNames = BeanFactoryUtils.beanNamesForAnnotationIncludingAncestors( this.applicationContext, Endpoint.class); for (String beanName : beanNames) { if (!ScopedProxyUtils.isScopedTarget(beanName)) { EndpointBean endpointBean = createEndpointBean(beanName); EndpointBean previous = byId.putIfAbsent(endpointBean.getId(), endpointBean); Assert.state(previous == null, () -> "Found two endpoints with the id '" + endpointBean.getId() + "': '" + endpointBean.getBeanName() + "' and '" + previous.getBeanName() + "'"); } } return byId.values(); }