Introduced getBeanNamesForAnnotation method on ListableBeanFactory
Issue: SPR-11069
This commit is contained in:
@@ -212,23 +212,36 @@ public interface ListableBeanFactory extends BeanFactory {
|
||||
throws BeansException;
|
||||
|
||||
/**
|
||||
* Find all beans whose {@code Class} has the supplied {@link java.lang.annotation.Annotation} type.
|
||||
* Find all beans whose {@code Class} has the supplied {@link Annotation} type,
|
||||
* without creating any bean instances yet.
|
||||
* @param annotationType the type of annotation to look for
|
||||
* @return the names of all matching beans
|
||||
* @since 4.0
|
||||
*/
|
||||
String[] getBeanNamesForAnnotation(Class<? extends Annotation> annotationType);
|
||||
|
||||
/**
|
||||
* Find all beans whose {@code Class} has the supplied {@link Annotation} type,
|
||||
* returning a Map of bean names with corresponding bean instances.
|
||||
* @param annotationType the type of annotation to look for
|
||||
* @return a Map with the matching beans, containing the bean names as
|
||||
* keys and the corresponding bean instances as values
|
||||
* @throws BeansException if a bean could not be created
|
||||
* @since 3.0
|
||||
*/
|
||||
Map<String, Object> getBeansWithAnnotation(Class<? extends Annotation> annotationType)
|
||||
throws BeansException;
|
||||
Map<String, Object> getBeansWithAnnotation(Class<? extends Annotation> annotationType) throws BeansException;
|
||||
|
||||
/**
|
||||
* Find a {@link Annotation} of {@code annotationType} on the specified
|
||||
* 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
|
||||
* @return the annotation of the given type found, or {@code null}
|
||||
* @throws NoSuchBeanDefinitionException if there is no bean with the given name
|
||||
* @since 3.0
|
||||
*/
|
||||
<A extends Annotation> A findAnnotationOnBean(String beanName, Class<A> annotationType);
|
||||
<A extends Annotation> A findAnnotationOnBean(String beanName, Class<A> annotationType)
|
||||
throws NoSuchBeanDefinitionException;
|
||||
|
||||
}
|
||||
|
||||
@@ -487,6 +487,23 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String[] getBeanNamesForAnnotation(Class<? extends Annotation> annotationType) {
|
||||
List<String> results = new ArrayList<String>();
|
||||
for (String beanName : getBeanDefinitionNames()) {
|
||||
BeanDefinition beanDefinition = getBeanDefinition(beanName);
|
||||
if (!beanDefinition.isAbstract() && findAnnotationOnBean(beanName, annotationType) != null) {
|
||||
results.add(beanName);
|
||||
}
|
||||
}
|
||||
for (String beanName : getSingletonNames()) {
|
||||
if (!results.contains(beanName) && findAnnotationOnBean(beanName, annotationType) != null) {
|
||||
results.add(beanName);
|
||||
}
|
||||
}
|
||||
return results.toArray(new String[results.size()]);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, Object> getBeansWithAnnotation(Class<? extends Annotation> annotationType) {
|
||||
Map<String, Object> results = new LinkedHashMap<String, Object>();
|
||||
@@ -511,7 +528,9 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto
|
||||
* if not found on the exposed bean reference (e.g. in case of a proxy).
|
||||
*/
|
||||
@Override
|
||||
public <A extends Annotation> A findAnnotationOnBean(String beanName, Class<A> annotationType) {
|
||||
public <A extends Annotation> A findAnnotationOnBean(String beanName, Class<A> annotationType)
|
||||
throws NoSuchBeanDefinitionException{
|
||||
|
||||
A ann = null;
|
||||
Class<?> beanType = getType(beanName);
|
||||
if (beanType != null) {
|
||||
|
||||
@@ -276,6 +276,17 @@ public class StaticListableBeanFactory implements ListableBeanFactory {
|
||||
return matches;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String[] getBeanNamesForAnnotation(Class<? extends Annotation> annotationType) {
|
||||
List<String> results = new ArrayList<String>();
|
||||
for (String beanName : this.beans.keySet()) {
|
||||
if (findAnnotationOnBean(beanName, annotationType) != null) {
|
||||
results.add(beanName);
|
||||
}
|
||||
}
|
||||
return results.toArray(new String[results.size()]);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, Object> getBeansWithAnnotation(Class<? extends Annotation> annotationType)
|
||||
throws BeansException {
|
||||
@@ -290,7 +301,9 @@ public class StaticListableBeanFactory implements ListableBeanFactory {
|
||||
}
|
||||
|
||||
@Override
|
||||
public <A extends Annotation> A findAnnotationOnBean(String beanName, Class<A> annotationType) {
|
||||
public <A extends Annotation> A findAnnotationOnBean(String beanName, Class<A> annotationType)
|
||||
throws NoSuchBeanDefinitionException{
|
||||
|
||||
return AnnotationUtils.findAnnotation(getType(beanName), annotationType);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user