From abbe61b9f87a97b9541bcd4e8a764457d8283d47 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Wed, 3 Apr 2019 14:33:57 +0200 Subject: [PATCH 1/2] Consistent internal use of getMergedLocalBeanDefinition --- .../support/DefaultListableBeanFactory.java | 36 +++++-------------- 1 file changed, 9 insertions(+), 27 deletions(-) diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/support/DefaultListableBeanFactory.java b/spring-beans/src/main/java/org/springframework/beans/factory/support/DefaultListableBeanFactory.java index 18ba37ac66f..f2bcbe94e3d 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/support/DefaultListableBeanFactory.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/support/DefaultListableBeanFactory.java @@ -656,12 +656,6 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto return result; } - /** - * Find a {@link Annotation} of {@code annotationType} on the specified - * bean, traversing its interfaces and super classes if no annotation can be - * found on the given class itself, as well as checking its raw bean class - * if not found on the exposed bean reference (e.g. in case of a proxy). - */ @Override @Nullable public A findAnnotationOnBean(String beanName, Class annotationType) @@ -673,14 +667,12 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto ann = AnnotationUtils.findAnnotation(beanType, annotationType); } if (ann == null && containsBeanDefinition(beanName)) { - BeanDefinition bd = getMergedBeanDefinition(beanName); - if (bd instanceof AbstractBeanDefinition) { - AbstractBeanDefinition abd = (AbstractBeanDefinition) bd; - if (abd.hasBeanClass()) { - Class beanClass = abd.getBeanClass(); - if (beanClass != beanType) { - ann = AnnotationUtils.findAnnotation(beanClass, annotationType); - } + // Check raw bean class, e.g. in case of a proxy. + RootBeanDefinition bd = getMergedLocalBeanDefinition(beanName); + if (bd.hasBeanClass()) { + Class beanClass = bd.getBeanClass(); + if (beanClass != beanType) { + ann = AnnotationUtils.findAnnotation(beanClass, annotationType); } } } @@ -1975,10 +1967,11 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto @Override @Nullable public Object getOrderSource(Object obj) { - RootBeanDefinition beanDefinition = getRootBeanDefinition(this.instancesToBeanNames.get(obj)); - if (beanDefinition == null) { + String beanName = this.instancesToBeanNames.get(obj); + if (beanName == null || !containsBeanDefinition(beanName)) { return null; } + RootBeanDefinition beanDefinition = getMergedLocalBeanDefinition(beanName); List sources = new ArrayList<>(2); Method factoryMethod = beanDefinition.getResolvedFactoryMethod(); if (factoryMethod != null) { @@ -1990,17 +1983,6 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto } return sources.toArray(); } - - @Nullable - private RootBeanDefinition getRootBeanDefinition(@Nullable String beanName) { - if (beanName != null && containsBeanDefinition(beanName)) { - BeanDefinition bd = getMergedBeanDefinition(beanName); - if (bd instanceof RootBeanDefinition) { - return (RootBeanDefinition) bd; - } - } - return null; - } } } From 9ea02c6dfa66a1c4dd576fb7b92e4ea1ea0bc041 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Wed, 3 Apr 2019 14:34:11 +0200 Subject: [PATCH 2/2] Revised javadoc --- .../beans/factory/ListableBeanFactory.java | 18 +++++++++++------- .../context/annotation/Configuration.java | 4 ++-- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/ListableBeanFactory.java b/spring-beans/src/main/java/org/springframework/beans/factory/ListableBeanFactory.java index 62c6e2b10d6..b0586b8be92 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/ListableBeanFactory.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/ListableBeanFactory.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -243,18 +243,19 @@ public interface ListableBeanFactory extends BeanFactory { throws BeansException; /** - * Find all names of beans whose {@code Class} has the supplied {@link Annotation} + * Find all names of beans which are annotated with the supplied {@link Annotation} * type, without creating corresponding bean instances yet. *

Note that this method considers objects created by FactoryBeans, which means * that FactoryBeans will get initialized in order to determine their object type. * @param annotationType the type of annotation to look for * @return the names of all matching beans * @since 4.0 + * @see #findAnnotationOnBean */ String[] getBeanNamesForAnnotation(Class annotationType); /** - * Find all beans whose {@code Class} has the supplied {@link Annotation} type, + * Find all beans which are annotated with the supplied {@link Annotation} type, * returning a Map of bean names with corresponding bean instances. *

Note that this method considers objects created by FactoryBeans, which means * that FactoryBeans will get initialized in order to determine their object type. @@ -263,18 +264,21 @@ public interface ListableBeanFactory extends BeanFactory { * keys and the corresponding bean instances as values * @throws BeansException if a bean could not be created * @since 3.0 + * @see #findAnnotationOnBean */ Map getBeansWithAnnotation(Class annotationType) throws BeansException; /** - * Find an {@link Annotation} of {@code annotationType} on the specified - * bean, traversing its interfaces and super classes if no annotation can be - * found on the given class itself. + * Find an {@link Annotation} of {@code annotationType} on the specified bean, + * traversing its interfaces and super classes if no annotation can be found on + * the given class itself. * @param beanName the name of the bean to look for annotations on - * @param annotationType the annotation class to look for + * @param annotationType the type of annotation to look for * @return the annotation of the given type if found, or {@code null} otherwise * @throws NoSuchBeanDefinitionException if there is no bean with the given name * @since 3.0 + * @see #getBeanNamesForAnnotation + * @see #getBeansWithAnnotation */ @Nullable A findAnnotationOnBean(String beanName, Class annotationType) diff --git a/spring-context/src/main/java/org/springframework/context/annotation/Configuration.java b/spring-context/src/main/java/org/springframework/context/annotation/Configuration.java index 6508ee02b34..077c0ec3a68 100644 --- a/spring-context/src/main/java/org/springframework/context/annotation/Configuration.java +++ b/spring-context/src/main/java/org/springframework/context/annotation/Configuration.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -430,7 +430,7 @@ public @interface Configuration { * is registered as a traditional XML bean definition, the name/id of the bean * element will take precedence. * @return the explicit component name, if any (or empty String otherwise) - * @see org.springframework.beans.factory.support.DefaultBeanNameGenerator + * @see AnnotationBeanNameGenerator */ @AliasFor(annotation = Component.class) String value() default "";