added SourceExtractor support
This commit is contained in:
parent
df8219d56e
commit
cce6e2f4cd
|
|
@ -29,6 +29,7 @@ import org.apache.commons.logging.LogFactory;
|
|||
import org.springframework.beans.factory.annotation.Autowire;
|
||||
import org.springframework.beans.factory.config.BeanDefinition;
|
||||
import org.springframework.beans.factory.config.BeanDefinitionHolder;
|
||||
import org.springframework.beans.factory.parsing.SourceExtractor;
|
||||
import org.springframework.beans.factory.support.BeanDefinitionReader;
|
||||
import org.springframework.beans.factory.support.BeanDefinitionReaderUtils;
|
||||
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
|
||||
|
|
@ -36,7 +37,6 @@ import org.springframework.beans.factory.support.GenericBeanDefinition;
|
|||
import org.springframework.beans.factory.support.RootBeanDefinition;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.core.type.MethodMetadata;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
|
|
@ -56,10 +56,16 @@ class ConfigurationClassBeanDefinitionReader {
|
|||
|
||||
private final BeanDefinitionRegistry registry;
|
||||
|
||||
private final SourceExtractor sourceExtractor;
|
||||
|
||||
public ConfigurationClassBeanDefinitionReader(BeanDefinitionRegistry registry) {
|
||||
Assert.notNull(registry, "BeanDefinitionRegistry must not be null");
|
||||
|
||||
/**
|
||||
* Create a new {@link ConfigurationClassBeanDefinitionReader} instance that will be used
|
||||
* to populate the given {@link BeanDefinitionRegistry}.
|
||||
*/
|
||||
public ConfigurationClassBeanDefinitionReader(BeanDefinitionRegistry registry, SourceExtractor sourceExtractor) {
|
||||
this.registry = registry;
|
||||
this.sourceExtractor = sourceExtractor;
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -108,6 +114,7 @@ class ConfigurationClassBeanDefinitionReader {
|
|||
|
||||
RootBeanDefinition beanDef = new ConfigurationClassBeanDefinition();
|
||||
ConfigurationClass configClass = method.getDeclaringClass();
|
||||
beanDef.setSource(this.sourceExtractor.extractSource(metadata, configClass.getResource()));
|
||||
beanDef.setFactoryBeanName(configClass.getBeanName());
|
||||
beanDef.setUniqueFactoryMethodName(metadata.getMethodName());
|
||||
beanDef.setAutowireMode(RootBeanDefinition.AUTOWIRE_CONSTRUCTOR);
|
||||
|
|
|
|||
|
|
@ -32,7 +32,9 @@ import org.springframework.beans.factory.config.BeanDefinitionHolder;
|
|||
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
|
||||
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
|
||||
import org.springframework.beans.factory.parsing.FailFastProblemReporter;
|
||||
import org.springframework.beans.factory.parsing.PassThroughSourceExtractor;
|
||||
import org.springframework.beans.factory.parsing.ProblemReporter;
|
||||
import org.springframework.beans.factory.parsing.SourceExtractor;
|
||||
import org.springframework.beans.factory.support.AbstractBeanDefinition;
|
||||
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
|
||||
import org.springframework.core.Conventions;
|
||||
|
|
@ -43,6 +45,7 @@ import org.springframework.core.type.classreading.CachingMetadataReaderFactory;
|
|||
import org.springframework.core.type.classreading.MetadataReader;
|
||||
import org.springframework.core.type.classreading.MetadataReaderFactory;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.ClassUtils;
|
||||
|
||||
/**
|
||||
|
|
@ -79,6 +82,8 @@ public class ConfigurationClassPostProcessor implements BeanFactoryPostProcessor
|
|||
|
||||
private final Log logger = LogFactory.getLog(getClass());
|
||||
|
||||
private SourceExtractor sourceExtractor = new PassThroughSourceExtractor();
|
||||
|
||||
private ProblemReporter problemReporter = new FailFastProblemReporter();
|
||||
|
||||
private ClassLoader beanClassLoader = ClassUtils.getDefaultClassLoader();
|
||||
|
|
@ -88,14 +93,22 @@ public class ConfigurationClassPostProcessor implements BeanFactoryPostProcessor
|
|||
private boolean setMetadataReaderFactoryCalled = false;
|
||||
|
||||
|
||||
/**
|
||||
* Set the {@link SourceExtractor} to use for generated bean definitions
|
||||
* that correspond to {@link Bean} factory methods.
|
||||
*/
|
||||
public void setSourceExtractor(SourceExtractor sourceExtractor) {
|
||||
this.sourceExtractor = (sourceExtractor != null ? sourceExtractor : new PassThroughSourceExtractor());
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the {@link ProblemReporter} to use.
|
||||
* <p>Used to register any problems detected with {@link Configuration} or {@link Bean}
|
||||
* declarations. For instance, a Bean method marked as {@literal final} is illegal
|
||||
* declarations. For instance, an @Bean method marked as {@literal final} is illegal
|
||||
* and would be reported as a problem. Defaults to {@link FailFastProblemReporter}.
|
||||
*/
|
||||
public void setProblemReporter(ProblemReporter problemReporter) {
|
||||
this.problemReporter = problemReporter;
|
||||
this.problemReporter = (problemReporter != null ? problemReporter : new FailFastProblemReporter());
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -104,6 +117,7 @@ public class ConfigurationClassPostProcessor implements BeanFactoryPostProcessor
|
|||
* {@link #setBeanClassLoader bean class loader}.
|
||||
*/
|
||||
public void setMetadataReaderFactory(MetadataReaderFactory metadataReaderFactory) {
|
||||
Assert.notNull(metadataReaderFactory, "MetadataReaderFactory must not be null");
|
||||
this.metadataReaderFactory = metadataReaderFactory;
|
||||
this.setMetadataReaderFactoryCalled = true;
|
||||
}
|
||||
|
|
@ -170,7 +184,7 @@ public class ConfigurationClassPostProcessor implements BeanFactoryPostProcessor
|
|||
parser.validate();
|
||||
|
||||
// Read the model and create bean definitions based on its content
|
||||
new ConfigurationClassBeanDefinitionReader(registry).loadBeanDefinitions(parser.getModel());
|
||||
new ConfigurationClassBeanDefinitionReader(registry, this.sourceExtractor).loadBeanDefinitions(parser.getModel());
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
Loading…
Reference in New Issue