|
|
|
@ -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<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));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
private ConfigurableApplicationContext applicationContext;
|
|
|
|
|
|
|
|
|
|
public <A extends Annotation> Map<String, Object> getBeansWithFactoryAnnotation(
|
|
|
|
|
Class<A> type) {
|
|
|
|
|
Map<String, Object> 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<Method> 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();
|
|
|
|
|
ConfigurableListableBeanFactory beanFactory = this.applicationContext
|
|
|
|
|
.getBeanFactory();
|
|
|
|
|
if (beanFactory.containsBeanDefinition(beanName)) {
|
|
|
|
|
BeanDefinition beanDefinition = beanFactory.getMergedBeanDefinition(beanName);
|
|
|
|
|
if (beanDefinition instanceof RootBeanDefinition) {
|
|
|
|
|
return ((RootBeanDefinition) beanDefinition).getResolvedFactoryMethod();
|
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|