Remove use of ReflectionUtils.doWithMethods from ConfigurationBeanFactoryMetadata

Closes gh-16220
pull/16225/head
Andy Wilkinson 6 years ago
parent 210c51e31c
commit 0872eb0dd9

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2018 the original author or authors. * Copyright 2012-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -20,15 +20,15 @@ import java.lang.annotation.Annotation;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;
import org.springframework.beans.BeansException; import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.annotation.AnnotationUtils; import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.util.ClassUtils;
import org.springframework.util.ReflectionUtils;
/** /**
* Utility class to memorize {@code @Bean} definition meta data during initialization of * Utility class to memorize {@code @Bean} definition meta data during initialization of
@ -37,7 +37,7 @@ import org.springframework.util.ReflectionUtils;
* @author Dave Syer * @author Dave Syer
* @since 1.1.0 * @since 1.1.0
*/ */
public class ConfigurationBeanFactoryMetadata implements BeanFactoryPostProcessor { public class ConfigurationBeanFactoryMetadata implements ApplicationContextAware {
/** /**
* The bean name that this class is registered with. * The bean name that this class is registered with.
@ -45,30 +45,15 @@ public class ConfigurationBeanFactoryMetadata implements BeanFactoryPostProcesso
public static final String BEAN_NAME = ConfigurationBeanFactoryMetadata.class public static final String BEAN_NAME = ConfigurationBeanFactoryMetadata.class
.getName(); .getName();
private ConfigurableListableBeanFactory beanFactory; private ConfigurableApplicationContext applicationContext;
private final Map<String, FactoryMetadata> beansFactoryMetadata = new HashMap<>();
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory)
throws BeansException {
this.beanFactory = beanFactory;
for (String name : beanFactory.getBeanDefinitionNames()) {
BeanDefinition definition = beanFactory.getBeanDefinition(name);
String method = definition.getFactoryMethodName();
String bean = definition.getFactoryBeanName();
if (method != null && bean != null) {
this.beansFactoryMetadata.put(name, new FactoryMetadata(bean, method));
}
}
}
public <A extends Annotation> Map<String, Object> getBeansWithFactoryAnnotation( public <A extends Annotation> Map<String, Object> getBeansWithFactoryAnnotation(
Class<A> type) { Class<A> type) {
Map<String, Object> result = new HashMap<>(); Map<String, Object> result = new HashMap<>();
for (String name : this.beansFactoryMetadata.keySet()) { for (String name : this.applicationContext.getBeanFactory()
.getBeanDefinitionNames()) {
if (findFactoryAnnotation(name, type) != null) { if (findFactoryAnnotation(name, type) != null) {
result.put(name, this.beanFactory.getBean(name)); result.put(name, this.applicationContext.getBean(name));
} }
} }
return result; return result;
@ -81,43 +66,21 @@ public class ConfigurationBeanFactoryMetadata implements BeanFactoryPostProcesso
} }
public Method findFactoryMethod(String beanName) { public Method findFactoryMethod(String beanName) {
if (!this.beansFactoryMetadata.containsKey(beanName)) { ConfigurableListableBeanFactory beanFactory = this.applicationContext
return null; .getBeanFactory();
} if (beanFactory.containsBeanDefinition(beanName)) {
AtomicReference<Method> found = new AtomicReference<>(null); BeanDefinition beanDefinition = beanFactory.getMergedBeanDefinition(beanName);
FactoryMetadata metadata = this.beansFactoryMetadata.get(beanName); if (beanDefinition instanceof RootBeanDefinition) {
Class<?> factoryType = this.beanFactory.getType(metadata.getBean()); return ((RootBeanDefinition) beanDefinition).getResolvedFactoryMethod();
String factoryMethod = metadata.getMethod();
if (ClassUtils.isCglibProxyClass(factoryType)) {
factoryType = factoryType.getSuperclass();
}
ReflectionUtils.doWithMethods(factoryType, (method) -> {
if (method.getName().equals(factoryMethod)) {
found.compareAndSet(null, method);
} }
});
return found.get();
}
private static class FactoryMetadata {
private final String bean;
private final String method;
FactoryMetadata(String bean, String method) {
this.bean = bean;
this.method = method;
}
public String getBean() {
return this.bean;
}
public String getMethod() {
return this.method;
} }
return null;
}
@Override
public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException {
this.applicationContext = (ConfigurableApplicationContext) applicationContext;
} }
} }

Loading…
Cancel
Save