|
16 | 16 |
|
17 | 17 | package org.springframework.context.annotation;
|
18 | 18 |
|
19 |
| -import java.io.IOException; |
20 | 19 | import java.lang.reflect.Method;
|
21 | 20 | import java.util.ArrayList;
|
22 | 21 | import java.util.Arrays;
|
|
29 | 28 | import org.apache.commons.logging.LogFactory;
|
30 | 29 |
|
31 | 30 | import org.springframework.beans.factory.annotation.AnnotatedBeanDefinition;
|
| 31 | +import org.springframework.beans.factory.annotation.AnnotatedGenericBeanDefinition; |
32 | 32 | import org.springframework.beans.factory.annotation.Autowire;
|
33 | 33 | import org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor;
|
34 | 34 | import org.springframework.beans.factory.config.BeanDefinition;
|
|
37 | 37 | import org.springframework.beans.factory.parsing.Problem;
|
38 | 38 | import org.springframework.beans.factory.parsing.ProblemReporter;
|
39 | 39 | import org.springframework.beans.factory.parsing.SourceExtractor;
|
40 |
| -import org.springframework.beans.factory.support.AbstractBeanDefinition; |
41 | 40 | import org.springframework.beans.factory.support.AbstractBeanDefinitionReader;
|
42 | 41 | import org.springframework.beans.factory.support.BeanDefinitionReader;
|
43 |
| -import org.springframework.beans.factory.support.BeanDefinitionReaderUtils; |
44 | 42 | import org.springframework.beans.factory.support.BeanDefinitionRegistry;
|
45 |
| -import org.springframework.beans.factory.support.GenericBeanDefinition; |
| 43 | +import org.springframework.beans.factory.support.BeanNameGenerator; |
46 | 44 | import org.springframework.beans.factory.support.RootBeanDefinition;
|
47 | 45 | import org.springframework.core.annotation.AnnotationAttributes;
|
48 | 46 | import org.springframework.core.env.Environment;
|
49 | 47 | import org.springframework.core.io.Resource;
|
50 | 48 | import org.springframework.core.io.ResourceLoader;
|
51 | 49 | import org.springframework.core.type.AnnotationMetadata;
|
52 | 50 | import org.springframework.core.type.MethodMetadata;
|
53 |
| -import org.springframework.core.type.classreading.MetadataReader; |
54 | 51 | import org.springframework.core.type.classreading.MetadataReaderFactory;
|
55 | 52 | import org.springframework.util.StringUtils;
|
56 | 53 |
|
@@ -85,6 +82,8 @@ class ConfigurationClassBeanDefinitionReader {
|
85 | 82 |
|
86 | 83 | private final Environment environment;
|
87 | 84 |
|
| 85 | + private BeanNameGenerator beanNameGenerator = new AnnotationBeanNameGenerator(); |
| 86 | + |
88 | 87 |
|
89 | 88 | /**
|
90 | 89 | * Create a new {@link ConfigurationClassBeanDefinitionReader} instance that will be used
|
@@ -135,39 +134,21 @@ private void doLoadBeanDefinitionForConfigurationClassIfNecessary(ConfigurationC
|
135 | 134 | return;
|
136 | 135 | }
|
137 | 136 |
|
138 |
| - BeanDefinition configBeanDef = new GenericBeanDefinition(); |
139 |
| - String className = configClass.getMetadata().getClassName(); |
| 137 | + AnnotationMetadata metadata = configClass.getMetadata(); |
| 138 | + BeanDefinition configBeanDef = new AnnotatedGenericBeanDefinition(metadata); |
| 139 | + String className = metadata.getClassName(); |
140 | 140 | configBeanDef.setBeanClassName(className);
|
141 |
| - MetadataReader reader; |
142 |
| - try { |
143 |
| - reader = this.metadataReaderFactory.getMetadataReader(className); |
144 |
| - } |
145 |
| - catch (IOException ex) { |
146 |
| - throw new IllegalStateException("Could not create MetadataReader for class " + className); |
147 |
| - } |
148 | 141 | if (ConfigurationClassUtils.checkConfigurationClassCandidate(configBeanDef, this.metadataReaderFactory)) {
|
149 |
| - Map<String, Object> configAttributes = |
150 |
| - reader.getAnnotationMetadata().getAnnotationAttributes(Configuration.class.getName()); |
151 |
| - |
152 |
| - // has the 'value' attribute of @Configuration been set? |
153 |
| - String configBeanName = (String) configAttributes.get("value"); |
154 |
| - if (StringUtils.hasText(configBeanName)) { |
155 |
| - // yes -> register the configuration class bean with this name |
156 |
| - this.registry.registerBeanDefinition(configBeanName, configBeanDef); |
157 |
| - } |
158 |
| - else { |
159 |
| - // no -> register the configuration class bean with a generated name |
160 |
| - configBeanName = BeanDefinitionReaderUtils.registerWithGeneratedName((AbstractBeanDefinition)configBeanDef, this.registry); |
161 |
| - } |
| 142 | + String configBeanName = this.beanNameGenerator.generateBeanName(configBeanDef, this.registry); |
| 143 | + this.registry.registerBeanDefinition(configBeanName, configBeanDef); |
162 | 144 | configClass.setBeanName(configBeanName);
|
163 | 145 | if (logger.isDebugEnabled()) {
|
164 | 146 | logger.debug(String.format("Registered bean definition for imported @Configuration class %s", configBeanName));
|
165 | 147 | }
|
166 | 148 | }
|
167 | 149 | else {
|
168 |
| - AnnotationMetadata metadata = reader.getAnnotationMetadata(); |
169 | 150 | this.problemReporter.error(
|
170 |
| - new InvalidConfigurationImportProblem(className, reader.getResource(), metadata)); |
| 151 | + new InvalidConfigurationImportProblem(className, configClass.getResource(), metadata)); |
171 | 152 | }
|
172 | 153 | }
|
173 | 154 |
|
|
0 commit comments