From afb67887da5586353f71fe4d79d23431d69bc3f2 Mon Sep 17 00:00:00 2001 From: Scott Frederick Date: Fri, 31 Jan 2020 17:13:09 -0600 Subject: [PATCH] Restore deprecated class required by Spring Cloud See gh-19860 --- .../ConfigurationBeanFactoryMetadata.java | 93 +++++++++++++++++++ ...nableConfigurationPropertiesRegistrar.java | 1 + 2 files changed, 94 insertions(+) create mode 100644 spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/ConfigurationBeanFactoryMetadata.java 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 new file mode 100644 index 00000000000..6499b9af3e4 --- /dev/null +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/ConfigurationBeanFactoryMetadata.java @@ -0,0 +1,93 @@ +/* + * Copyright 2012-2020 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. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.context.properties; + +import java.lang.annotation.Annotation; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Map; + +import org.springframework.beans.BeansException; +import org.springframework.beans.factory.config.BeanDefinition; +import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; +import org.springframework.beans.factory.support.BeanDefinitionRegistry; +import org.springframework.beans.factory.support.GenericBeanDefinition; +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; + +/** + * Utility class to memorize {@code @Bean} definition metadata during initialization of + * the bean factory. + * + * @author Dave Syer + * @since 1.1.0 + * @deprecated since 2.2.0 in favor of {@link ConfigurationPropertiesBean} + */ +@Deprecated +public class ConfigurationBeanFactoryMetadata implements ApplicationContextAware { + + /** + * The bean name that this class is registered with. + */ + public static final String BEAN_NAME = ConfigurationBeanFactoryMetadata.class.getName(); + + private ConfigurableApplicationContext applicationContext; + + public Map getBeansWithFactoryAnnotation(Class type) { + Map result = new HashMap<>(); + for (String name : this.applicationContext.getBeanFactory().getBeanDefinitionNames()) { + if (findFactoryAnnotation(name, type) != null) { + result.put(name, this.applicationContext.getBean(name)); + } + } + return result; + } + + public A findFactoryAnnotation(String beanName, Class type) { + Method method = findFactoryMethod(beanName); + return (method != null) ? AnnotationUtils.findAnnotation(method, type) : null; + } + + public Method findFactoryMethod(String beanName) { + ConfigurableListableBeanFactory beanFactory = this.applicationContext.getBeanFactory(); + if (beanFactory.containsBeanDefinition(beanName)) { + BeanDefinition beanDefinition = beanFactory.getMergedBeanDefinition(beanName); + if (beanDefinition instanceof RootBeanDefinition) { + return ((RootBeanDefinition) beanDefinition).getResolvedFactoryMethod(); + } + } + return null; + } + + @Override + public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { + this.applicationContext = (ConfigurableApplicationContext) applicationContext; + } + + static void register(BeanDefinitionRegistry registry) { + if (!registry.containsBeanDefinition(BEAN_NAME)) { + GenericBeanDefinition definition = new GenericBeanDefinition(); + definition.setBeanClass(ConfigurationBeanFactoryMetadata.class); + definition.setRole(BeanDefinition.ROLE_INFRASTRUCTURE); + registry.registerBeanDefinition(ConfigurationBeanFactoryMetadata.BEAN_NAME, definition); + } + } + +} diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/EnableConfigurationPropertiesRegistrar.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/EnableConfigurationPropertiesRegistrar.java index 7dadda9bd9c..22016865421 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/EnableConfigurationPropertiesRegistrar.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/EnableConfigurationPropertiesRegistrar.java @@ -51,6 +51,7 @@ class EnableConfigurationPropertiesRegistrar implements ImportBeanDefinitionRegi ConfigurationPropertiesBindingPostProcessor.register(registry); BoundConfigurationProperties.register(registry); ConfigurationPropertiesBeanDefinitionValidator.register(registry); + ConfigurationBeanFactoryMetadata.register(registry); } }