From 0872eb0dd92129266c3bf396829c3f91e0ec740c Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Wed, 13 Mar 2019 13:52:16 +0000 Subject: [PATCH] Remove use of ReflectionUtils.doWithMethods from ConfigurationBeanFactoryMetadata Closes gh-16220 --- .../ConfigurationBeanFactoryMetadata.java | 81 +++++-------------- 1 file changed, 22 insertions(+), 59 deletions(-) diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/ConfigurationBeanFactoryMetadata.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/ConfigurationBeanFactoryMetadata.java index 687dfbab62..d7774d45b4 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/ConfigurationBeanFactoryMetadata.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/ConfigurationBeanFactoryMetadata.java @@ -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"); * 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.util.HashMap; import java.util.Map; -import java.util.concurrent.atomic.AtomicReference; import org.springframework.beans.BeansException; 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.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.util.ClassUtils; -import org.springframework.util.ReflectionUtils; /** * Utility class to memorize {@code @Bean} definition meta data during initialization of @@ -37,7 +37,7 @@ import org.springframework.util.ReflectionUtils; * @author Dave Syer * @since 1.1.0 */ -public class ConfigurationBeanFactoryMetadata implements BeanFactoryPostProcessor { +public class ConfigurationBeanFactoryMetadata implements ApplicationContextAware { /** * 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 .getName(); - private ConfigurableListableBeanFactory beanFactory; - - private final Map 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)); - } - } - } + private ConfigurableApplicationContext applicationContext; public Map getBeansWithFactoryAnnotation( Class type) { Map result = new HashMap<>(); - for (String name : this.beansFactoryMetadata.keySet()) { + for (String name : this.applicationContext.getBeanFactory() + .getBeanDefinitionNames()) { if (findFactoryAnnotation(name, type) != null) { - result.put(name, this.beanFactory.getBean(name)); + result.put(name, this.applicationContext.getBean(name)); } } return result; @@ -81,43 +66,21 @@ public class ConfigurationBeanFactoryMetadata implements BeanFactoryPostProcesso } public Method findFactoryMethod(String beanName) { - if (!this.beansFactoryMetadata.containsKey(beanName)) { - return null; - } - AtomicReference found = new AtomicReference<>(null); - FactoryMetadata metadata = this.beansFactoryMetadata.get(beanName); - Class factoryType = this.beanFactory.getType(metadata.getBean()); - String factoryMethod = metadata.getMethod(); - if (ClassUtils.isCglibProxyClass(factoryType)) { - factoryType = factoryType.getSuperclass(); - } - ReflectionUtils.doWithMethods(factoryType, (method) -> { - if (method.getName().equals(factoryMethod)) { - found.compareAndSet(null, method); + ConfigurableListableBeanFactory beanFactory = this.applicationContext + .getBeanFactory(); + if (beanFactory.containsBeanDefinition(beanName)) { + BeanDefinition beanDefinition = beanFactory.getMergedBeanDefinition(beanName); + if (beanDefinition instanceof RootBeanDefinition) { + return ((RootBeanDefinition) beanDefinition).getResolvedFactoryMethod(); } - }); - 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; } }