• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

TAKETODAY / today-infrastructure / 18072046494

28 Sep 2025 09:00AM UTC coverage: 81.887% (-0.003%) from 81.89%
18072046494

push

github

TAKETODAY
:sparkles: Future zip API

59764 of 77946 branches covered (76.67%)

Branch coverage included in aggregate %.

141251 of 167532 relevant lines covered (84.31%)

3.6 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

94.3
today-context/src/main/java/infra/context/annotation/ConfigurationClassBeanDefinitionReader.java
1
/*
2
 * Copyright 2017 - 2025 the original author or authors.
3
 *
4
 * This program is free software: you can redistribute it and/or modify
5
 * it under the terms of the GNU General Public License as published by
6
 * the Free Software Foundation, either version 3 of the License, or
7
 * (at your option) any later version.
8
 *
9
 * This program is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 * GNU General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU General Public License
15
 * along with this program. If not, see [https://www.gnu.org/licenses/]
16
 */
17

18
package infra.context.annotation;
19

20
import java.io.Serial;
21
import java.lang.reflect.Method;
22
import java.util.HashMap;
23
import java.util.Map;
24
import java.util.Set;
25

26
import infra.beans.factory.BeanDefinitionStoreException;
27
import infra.beans.factory.BeanRegistrar;
28
import infra.beans.factory.annotation.AnnotatedBeanDefinition;
29
import infra.beans.factory.annotation.AnnotatedGenericBeanDefinition;
30
import infra.beans.factory.annotation.DisableAllDependencyInjection;
31
import infra.beans.factory.annotation.DisableDependencyInjection;
32
import infra.beans.factory.annotation.EnableDependencyInjection;
33
import infra.beans.factory.config.BeanDefinition;
34
import infra.beans.factory.config.BeanDefinitionHolder;
35
import infra.beans.factory.support.AbstractBeanDefinition;
36
import infra.beans.factory.support.AbstractBeanDefinitionReader;
37
import infra.beans.factory.support.BeanDefinitionOverrideException;
38
import infra.beans.factory.support.BeanDefinitionReader;
39
import infra.beans.factory.support.BeanDefinitionRegistry;
40
import infra.beans.factory.support.BeanNameGenerator;
41
import infra.beans.factory.support.BeanRegistryAdapter;
42
import infra.beans.factory.support.RootBeanDefinition;
43
import infra.beans.factory.xml.XmlBeanDefinitionReader;
44
import infra.context.BootstrapContext;
45
import infra.core.annotation.MergedAnnotation;
46
import infra.core.annotation.MergedAnnotations;
47
import infra.core.type.AnnotationMetadata;
48
import infra.core.type.MethodMetadata;
49
import infra.core.type.StandardAnnotationMetadata;
50
import infra.core.type.StandardMethodMetadata;
51
import infra.lang.Assert;
52
import infra.lang.NonNull;
53
import infra.logging.Logger;
54
import infra.logging.LoggerFactory;
55
import infra.stereotype.Component;
56
import infra.util.ObjectUtils;
57
import infra.util.ReflectionUtils;
58
import infra.util.StringUtils;
59

60
/**
61
 * Reads a given fully-populated set of ConfigurationClass instances, registering bean
62
 * definitions with the given {@link BeanDefinitionRegistry} based on its contents.
63
 *
64
 * @author Chris Beams
65
 * @author Juergen Hoeller
66
 * @author Phillip Webb
67
 * @author Sam Brannen
68
 * @author Sebastien Deleuze
69
 * @author <a href="https://github.com/TAKETODAY">Harry Yang</a>
70
 * @see ConfigurationClassParser
71
 * @since 4.0
72
 */
73
class ConfigurationClassBeanDefinitionReader {
74

75
  private static final Logger logger = LoggerFactory.getLogger(ConfigurationClassBeanDefinitionReader.class);
3✔
76

77
  private static final ScopeMetadataResolver scopeMetadataResolver = new AnnotationScopeMetadataResolver();
5✔
78

79
  private final ImportRegistry importRegistry;
80

81
  private final BootstrapContext bootstrapContext;
82

83
  private final BeanNameGenerator importBeanNameGenerator;
84

85
  /**
86
   * Create a new {@link ConfigurationClassBeanDefinitionReader} instance
87
   * that will be used to populate the given {@link BeanDefinitionRegistry}.
88
   */
89
  ConfigurationClassBeanDefinitionReader(BootstrapContext bootstrapContext,
90
          BeanNameGenerator importBeanNameGenerator, ImportRegistry importRegistry) {
2✔
91

92
    this.bootstrapContext = bootstrapContext;
3✔
93
    this.importRegistry = importRegistry;
3✔
94
    this.importBeanNameGenerator = importBeanNameGenerator;
3✔
95
  }
1✔
96

97
  /**
98
   * Read {@code configurationModel}, registering bean definitions
99
   * with the registry based on its contents.
100
   */
101
  public void loadBeanDefinitions(Set<ConfigurationClass> configurationModel) {
102
    TrackedConditionEvaluator trackedConditionEvaluator = new TrackedConditionEvaluator();
5✔
103
    for (ConfigurationClass configClass : configurationModel) {
10✔
104
      loadBeanDefinitionsForConfigurationClass(configClass, trackedConditionEvaluator);
4✔
105
    }
1✔
106
  }
1✔
107

108
  /**
109
   * Read a particular {@link ConfigurationClass}, registering bean definitions
110
   * for the class itself and all of its {@link Component} methods.
111
   */
112
  private void loadBeanDefinitionsForConfigurationClass(
113
          ConfigurationClass configClass, TrackedConditionEvaluator trackedConditionEvaluator) {
114

115
    if (trackedConditionEvaluator.shouldSkip(configClass)) {
4✔
116
      String beanName = configClass.beanName;
3✔
117
      // TODO annotated with both @Component and @ConditionalOnMissingBean,condition matching error
118
      if (StringUtils.isNotEmpty(beanName) && bootstrapContext.containsBeanDefinition(beanName)) {
8!
119
        bootstrapContext.removeBeanDefinition(beanName);
4✔
120
      }
121
      importRegistry.removeImportingClass(configClass.metadata.getClassName());
6✔
122
    }
1✔
123
    else {
124
      boolean disableAllDependencyInjection = isDisableAllDependencyInjection(configClass);
4✔
125
      if (configClass.isImported()) {
3✔
126
        registerBeanDefinitionForImportedConfigurationClass(configClass, disableAllDependencyInjection);
4✔
127
      }
128

129
      for (ComponentMethod componentMethod : configClass.componentMethods) {
11✔
130
        loadBeanDefinitionsForComponentMethod(componentMethod, disableAllDependencyInjection);
4✔
131
      }
1✔
132

133
      loadBeanDefinitionsFromImportedResources(configClass.importedResources);
4✔
134
      loadBeanDefinitionsFromImportBeanDefinitionRegistrars(configClass.importBeanDefinitionRegistrars);
4✔
135
      loadBeanDefinitionsFromBeanRegistrars(configClass.beanRegistrars);
4✔
136
    }
137
  }
1✔
138

139
  /**
140
   * Register the {@link Configuration} class itself as a bean definition.
141
   */
142
  private void registerBeanDefinitionForImportedConfigurationClass(ConfigurationClass configClass, boolean disableAllDependencyInjection) {
143
    var configBeanDef = new AnnotatedGenericBeanDefinition(configClass.metadata);
6✔
144

145
    ScopeMetadata scopeMetadata = scopeMetadataResolver.resolveScopeMetadata(configBeanDef);
4✔
146
    configBeanDef.setScope(scopeMetadata.getScopeName());
4✔
147
    // import bean name
148
    String configBeanName = importBeanNameGenerator.generateBeanName(configBeanDef, bootstrapContext.getRegistry());
8✔
149
    AnnotationConfigUtils.applyAnnotationMetadata(configBeanDef, false);
3✔
150

151
    BeanDefinitionHolder definitionHolder = new BeanDefinitionHolder(configBeanDef, configBeanName);
6✔
152
    definitionHolder = AnnotationConfigUtils.applyScopedProxyMode(scopeMetadata, definitionHolder, bootstrapContext.getRegistry());
7✔
153
    bootstrapContext.registerBeanDefinition(definitionHolder.getBeanName(), definitionHolder.getBeanDefinition());
7✔
154
    configClass.setBeanName(configBeanName);
3✔
155

156
    boolean enableDependencyInjection = isEnableDependencyInjection(configClass.metadata.getAnnotations(), disableAllDependencyInjection);
7✔
157
    configBeanDef.setEnableDependencyInjection(enableDependencyInjection);
3✔
158

159
    if (logger.isTraceEnabled()) {
3!
160
      logger.trace("Registered bean definition for imported class '{}'", configBeanName);
×
161
    }
162
  }
1✔
163

164
  /**
165
   * Read the given {@link ComponentMethod}, registering bean definitions
166
   * with the BeanDefinitionRegistry based on its contents.
167
   */
168
  private void loadBeanDefinitionsForComponentMethod(ComponentMethod componentMethod, boolean disableAllDependencyInjection) {
169
    ConfigurationClass configClass = componentMethod.configurationClass;
3✔
170
    MethodMetadata metadata = componentMethod.metadata;
3✔
171
    String methodName = metadata.getMethodName();
3✔
172

173
    // Do we need to mark the bean as skipped by its condition?
174
    if (bootstrapContext.shouldSkip(metadata, ConfigurationCondition.ConfigurationPhase.REGISTER_BEAN)) {
6✔
175
      configClass.skippedComponentMethods.add(methodName);
5✔
176
      return;
1✔
177
    }
178
    if (configClass.skippedComponentMethods.contains(methodName)) {
5!
179
      return;
×
180
    }
181
    MergedAnnotations annotations = metadata.getAnnotations();
3✔
182
    MergedAnnotation<Component> component = annotations.get(Component.class);
4✔
183
    Assert.state(component.isPresent(), "No @Component annotation attributes");
4✔
184

185
    // Consider name and any aliases.
186
    String[] explicitNames = component.getStringArray("name");
4✔
187
    String beanName;
188
    String localBeanName;
189
    if (explicitNames.length > 0 && StringUtils.hasText(explicitNames[0])) {
8!
190
      beanName = explicitNames[0];
4✔
191
      localBeanName = beanName;
2✔
192
      // Register aliases even when overridden below.
193
      for (int i = 1; i < explicitNames.length; i++) {
9✔
194
        bootstrapContext.registerAlias(beanName, explicitNames[i]);
7✔
195
      }
196
    }
197
    else {
198
      // Default bean name derived from method name.
199
      beanName = this.importBeanNameGenerator instanceof ConfigurationBeanNameGenerator cbng
9✔
200
              ? cbng.deriveBeanName(metadata) : methodName;
6✔
201
      localBeanName = methodName;
2✔
202
    }
203

204
    var beanDef = new ConfigurationClassBeanDefinition(configClass, metadata, localBeanName);
7✔
205
    beanDef.setSource(configClass.resource);
4✔
206
    beanDef.setResource(configClass.resource);
4✔
207

208
    // Has this effectively been overridden before (e.g. via XML)?
209
    if (isOverriddenByExistingDefinition(componentMethod, beanName, beanDef)) {
6✔
210
      if (beanName.equals(componentMethod.configurationClass.beanName)) {
6✔
211
        throw new BeanDefinitionStoreException(componentMethod.configurationClass.resource.toString(),
14✔
212
                beanName, ("Bean name derived from @Component method '%s' clashes with bean name for containing configuration class; " +
213
                "please make those names unique!").formatted(componentMethod.metadata.getMethodName()));
5✔
214
      }
215
      return;
1✔
216
    }
217

218
    boolean enableDI = isEnableDependencyInjection(annotations, disableAllDependencyInjection);
5✔
219
    beanDef.setEnableDependencyInjection(enableDI);
3✔
220

221
    AnnotationMetadata configClassMetadata = configClass.metadata;
3✔
222
    if (metadata.isStatic()) {
3✔
223
      // static @Component method
224
      if (configClassMetadata instanceof StandardAnnotationMetadata) {
3✔
225
        beanDef.setBeanClass(((StandardAnnotationMetadata) configClassMetadata).getIntrospectedClass());
6✔
226
      }
227
      else {
228
        beanDef.setBeanClassName(configClassMetadata.getClassName());
4✔
229
      }
230
      beanDef.setUniqueFactoryMethodName(methodName);
4✔
231
    }
232
    else {
233
      // instance @Component method
234
      beanDef.setFactoryBeanName(configClass.beanName);
4✔
235
      beanDef.setUniqueFactoryMethodName(methodName);
3✔
236
    }
237

238
    if (metadata instanceof StandardMethodMetadata smm && configClass.metadata instanceof StandardAnnotationMetadata sam) {
15!
239
      Method method = ReflectionUtils.getMostSpecificMethod(smm.getIntrospectedMethod(), sam.getIntrospectedClass());
6✔
240
      if (method == smm.getIntrospectedMethod()) {
4✔
241
        beanDef.setResolvedFactoryMethod(method);
3✔
242
      }
243
    }
244

245
    beanDef.setAutowireMode(AbstractBeanDefinition.AUTOWIRE_CONSTRUCTOR);
3✔
246
    AnnotationConfigUtils.applyAnnotationMetadata(beanDef, false);
3✔
247

248
    if (!component.getBoolean("autowireCandidate")) {
4✔
249
      beanDef.setAutowireCandidate(false);
3✔
250
    }
251

252
    if (!component.getBoolean("defaultCandidate")) {
4✔
253
      beanDef.setDefaultCandidate(false);
3✔
254
    }
255

256
    var instantiation = component.getEnum("bootstrap", Component.Bootstrap.class);
6✔
257
    if (instantiation == Component.Bootstrap.BACKGROUND) {
3✔
258
      beanDef.setBackgroundInit(true);
3✔
259
    }
260

261
    String[] initMethodName = component.getStringArray("initMethods");
4✔
262
    if (ObjectUtils.isNotEmpty(initMethodName)) {
3✔
263
      beanDef.setInitMethodNames(initMethodName);
3✔
264
    }
265

266
    String destroyMethodName = component.getString("destroyMethod");
4✔
267
    beanDef.setDestroyMethodName(destroyMethodName);
3✔
268

269
    // Consider scoping
270
    ScopedProxyMode proxyMode = ScopedProxyMode.NO;
2✔
271
    MergedAnnotation<Scope> scope = annotations.get(Scope.class);
4✔
272
    if (scope.isPresent()) {
3✔
273
      beanDef.setScope(scope.getString("value"));
5✔
274
      proxyMode = scope.getEnum("proxyMode", ScopedProxyMode.class);
6✔
275
      if (proxyMode == ScopedProxyMode.DEFAULT) {
3✔
276
        proxyMode = ScopedProxyMode.NO;
2✔
277
      }
278
    }
279

280
    // Replace the original bean definition with the target one, if necessary
281
    BeanDefinition beanDefToRegister;
282
    if (proxyMode != ScopedProxyMode.NO) {
3✔
283
      BeanDefinitionHolder proxyDef = ScopedProxyCreator.createScopedProxy(new BeanDefinitionHolder(beanDef, beanName),
9✔
284
              bootstrapContext.getRegistry(), proxyMode == ScopedProxyMode.TARGET_CLASS);
7✔
285
      beanDefToRegister = new ConfigurationClassBeanDefinition(
3✔
286
              (RootBeanDefinition) proxyDef.getBeanDefinition(), configClass, metadata, localBeanName);
7✔
287
    }
1✔
288
    else {
289
      beanDefToRegister = beanDef;
2✔
290
    }
291

292
    // Replace the original bean definition with the target one, if necessary
293
    if (logger.isTraceEnabled()) {
3!
294
      logger.trace("Registering bean definition for @Component method {}.{}()",
×
295
              configClassMetadata.getClassName(), beanName);
×
296
    }
297

298
    bootstrapContext.registerBeanDefinition(beanName, beanDefToRegister);
5✔
299
  }
1✔
300

301
  /**
302
   * disable all member class DI
303
   *
304
   * @param configClass current config class
305
   */
306
  private boolean isDisableAllDependencyInjection(ConfigurationClass configClass) {
307
    if (!configClass.metadata.isAnnotated(DisableAllDependencyInjection.class)) {
5✔
308
      try {
309
        String enclosingClassName = configClass.metadata.getEnclosingClassName();
4✔
310
        while (enclosingClassName != null) {
2✔
311
          AnnotationMetadata enclosingMetadata = bootstrapContext.getAnnotationMetadata(enclosingClassName);
5✔
312
          if (enclosingMetadata.isAnnotated(DisableAllDependencyInjection.class)) {
4✔
313
            return true;
2✔
314
          }
315
          enclosingClassName = enclosingMetadata.getEnclosingClassName();
3✔
316
        }
1✔
317
      }
318
      catch (Exception e) {
1✔
319
        logger.error("Failed to read class file via ASM for determining DI status order", e);
4✔
320
      }
1✔
321
      return false;
2✔
322
    }
323
    return true;
2✔
324
  }
325

326
  private boolean isEnableDependencyInjection(MergedAnnotations annotations, boolean disableAllDependencyInjection) {
327
    return annotations.isPresent(EnableDependencyInjection.class)
9✔
328
            || !(disableAllDependencyInjection || annotations.isPresent(DisableDependencyInjection.class));
5✔
329
  }
330

331
  private boolean isOverriddenByExistingDefinition(ComponentMethod componentMethod, String beanName, ConfigurationClassBeanDefinition newBeanDef) {
332
    if (!bootstrapContext.containsBeanDefinition(beanName)) {
5✔
333
      return false;
2✔
334
    }
335
    BeanDefinition existingBeanDef = bootstrapContext.getBeanDefinition(beanName);
5✔
336
    ConfigurationClass configClass = componentMethod.configurationClass;
3✔
337

338
    // If the bean method is an overloaded case on the same configuration class,
339
    // preserve the existing bean definition and mark it as overloaded.
340
    if (existingBeanDef instanceof ConfigurationClassBeanDefinition ccbd) {
6✔
341
      if (!ccbd.getMetadata().getClassName().equals(configClass.metadata.getClassName())) {
8✔
342
        return false;
2✔
343
      }
344
      if (ccbd.getFactoryMethodMetadata().getMethodName().equals(componentMethod.metadata.getMethodName())) {
8✔
345
        ccbd.setNonUniqueFactoryMethodName(ccbd.getFactoryMethodMetadata().getMethodName());
5✔
346
        return true;
2✔
347
      }
348
      Map<String, Object> attributes = configClass.metadata.getAnnotationAttributes(Configuration.class);
5✔
349
      if ((attributes != null && (Boolean) attributes.get("enforceUniqueMethods"))
10!
350
              || !bootstrapContext.getRegistry().isBeanDefinitionOverridable(beanName)) {
4✔
351
        throw new BeanDefinitionOverrideException(beanName, newBeanDef, existingBeanDef,
10✔
352
                "@Bean method override with same bean name but different method name: " + existingBeanDef);
353
      }
354
      return true;
2✔
355
    }
356

357
    // A bean definition resulting from a component scan can be silently overridden
358
    // by an @Component method, even when general overriding is disabled
359
    // as long as the bean class is the same.
360
    if (existingBeanDef instanceof ScannedGenericBeanDefinition scannedBeanDef) {
6✔
361
      if (componentMethod.metadata.getReturnTypeName().equals(scannedBeanDef.getBeanClassName())) {
7!
362
        bootstrapContext.removeBeanDefinition(beanName);
4✔
363
      }
364
      return false;
2✔
365
    }
366

367
    // Has the existing bean definition bean marked as a framework-generated bean?
368
    // -> allow the current bean method to override it, since it is application-level
369
    if (existingBeanDef.getRole() > BeanDefinition.ROLE_APPLICATION) {
3✔
370
      return false;
2✔
371
    }
372

373
    // At this point, it's a top-level override (probably XML), just having been parsed
374
    // before configuration class processing kicks in...
375
    if (!bootstrapContext.getRegistry().isBeanDefinitionOverridable(beanName)) {
6✔
376
      throw new BeanDefinitionOverrideException(beanName, newBeanDef,
10✔
377
              existingBeanDef, "@Component definition illegally overridden by existing bean definition: " + existingBeanDef);
378
    }
379
    if (logger.isDebugEnabled()) {
3!
380
      logger.debug("Skipping bean definition for {}: a definition for bean '{}' " +
×
381
              "already exists. This top-level bean definition is considered as an override.", componentMethod, beanName);
382
    }
383
    return true;
2✔
384
  }
385

386
  private void loadBeanDefinitionsFromImportedResources(Map<String, Class<? extends BeanDefinitionReader>> importedResources) {
387
    HashMap<Class<?>, BeanDefinitionReader> readerInstanceCache = new HashMap<>();
4✔
388

389
    for (Map.Entry<String, Class<? extends BeanDefinitionReader>> entry : importedResources.entrySet()) {
11✔
390
      String resource = entry.getKey();
4✔
391
      Class<? extends BeanDefinitionReader> readerClass = entry.getValue();
4✔
392
      // Default reader selection necessary?
393
      if (BeanDefinitionReader.class == readerClass) {
3!
394
        // Primarily ".xml" files but for any other extension as well
395
        readerClass = XmlBeanDefinitionReader.class;
2✔
396
      }
397

398
      BeanDefinitionReader reader = readerInstanceCache.get(readerClass);
5✔
399
      if (reader == null) {
2✔
400
        try {
401
          // Instantiate the specified BeanDefinitionReader
402
          reader = readerClass.getConstructor(BeanDefinitionRegistry.class).newInstance(bootstrapContext.getRegistry());
19✔
403
          // Delegate the current ResourceLoader to it if possible
404
          if (reader instanceof AbstractBeanDefinitionReader abdr) {
6!
405
            abdr.setEnvironment(bootstrapContext.getEnvironment());
5✔
406
            abdr.setResourceLoader(bootstrapContext.getResourceLoader());
5✔
407
          }
408
          readerInstanceCache.put(readerClass, reader);
5✔
409
        }
410
        catch (Throwable ex) {
×
411
          throw new IllegalStateException("Could not instantiate BeanDefinitionReader class [%s]"
×
412
                  .formatted(readerClass.getName()), ex);
×
413
        }
1✔
414
      }
415

416
      reader.loadBeanDefinitions(resource);
4✔
417
    }
1✔
418

419
  }
1✔
420

421
  private void loadBeanDefinitionsFromImportBeanDefinitionRegistrars(Map<ImportBeanDefinitionRegistrar, AnnotationMetadata> registrars) {
422
    for (var entry : registrars.entrySet()) {
11✔
423
      entry.getKey().registerBeanDefinitions(entry.getValue(), bootstrapContext, importBeanNameGenerator);
11✔
424
    }
1✔
425
  }
1✔
426

427
  private void loadBeanDefinitionsFromBeanRegistrars(Map<String, BeanRegistrar> registrars) {
428
    for (BeanRegistrar registrar : registrars.values()) {
11✔
429
      registrar.register(new BeanRegistryAdapter(bootstrapContext.getRegistry(),
9✔
430
              bootstrapContext.getBeanFactory(), bootstrapContext.getEnvironment(), registrar.getClass()), bootstrapContext.getEnvironment());
10✔
431
    }
1✔
432
  }
1✔
433

434
  /**
435
   * Evaluate {@code @Conditional} annotations, tracking results and taking into
436
   * account 'imported by'.
437
   */
438
  private final class TrackedConditionEvaluator {
5✔
439

440
    private final HashMap<ConfigurationClass, Boolean> skipped = new HashMap<>();
6✔
441

442
    public boolean shouldSkip(ConfigurationClass configClass) {
443
      Boolean skip = this.skipped.get(configClass);
6✔
444
      if (skip == null) {
2✔
445
        if (configClass.isImported()) {
3✔
446
          boolean allSkipped = true;
2✔
447
          for (ConfigurationClass importedBy : configClass.importedBy) {
11✔
448
            if (!shouldSkip(importedBy)) {
4✔
449
              allSkipped = false;
2✔
450
              break;
1✔
451
            }
452
          }
1✔
453
          if (allSkipped) {
2✔
454
            // The config classes that imported this one were all skipped, therefore we are skipped...
455
            skip = true;
3✔
456
          }
457
        }
458
        if (skip == null) {
2✔
459
          skip = bootstrapContext.shouldSkip(configClass.metadata, ConfigurationCondition.ConfigurationPhase.REGISTER_BEAN);
9✔
460
        }
461
        this.skipped.put(configClass, skip);
6✔
462
      }
463
      return skip;
3✔
464
    }
465
  }
466

467
  /**
468
   * {@link RootBeanDefinition} marker subclass used to signify that a bean definition
469
   * was created from a configuration class as opposed to any other configuration source.
470
   * Used in bean overriding cases where it's necessary to determine whether the bean
471
   * definition was created externally.
472
   */
473
  private static final class ConfigurationClassBeanDefinition extends RootBeanDefinition
474
          implements AnnotatedBeanDefinition {
475

476
    @Serial
477
    private static final long serialVersionUID = 1L;
478

479
    private final AnnotationMetadata annotationMetadata;
480

481
    private final MethodMetadata factoryMethodMetadata;
482

483
    private final String localBeanName;
484

485
    public ConfigurationClassBeanDefinition(ConfigurationClass configClass,
486
            MethodMetadata beanMethodMetadata, String localBeanName) {
2✔
487

488
      this.annotationMetadata = configClass.metadata;
4✔
489
      this.factoryMethodMetadata = beanMethodMetadata;
3✔
490
      this.localBeanName = localBeanName;
3✔
491
      setResource(configClass.resource);
4✔
492
      setLenientConstructorResolution(false);
3✔
493
    }
1✔
494

495
    public ConfigurationClassBeanDefinition(RootBeanDefinition original,
496
            ConfigurationClass configClass, MethodMetadata beanMethodMetadata, String localBeanName) {
497
      super(original);
3✔
498
      this.annotationMetadata = configClass.metadata;
4✔
499
      this.factoryMethodMetadata = beanMethodMetadata;
3✔
500
      this.localBeanName = localBeanName;
3✔
501
    }
1✔
502

503
    private ConfigurationClassBeanDefinition(ConfigurationClassBeanDefinition original) {
504
      super(original);
3✔
505
      this.annotationMetadata = original.annotationMetadata;
4✔
506
      this.factoryMethodMetadata = original.factoryMethodMetadata;
4✔
507
      this.localBeanName = original.localBeanName;
4✔
508
    }
1✔
509

510
    @Override
511
    public AnnotationMetadata getMetadata() {
512
      return this.annotationMetadata;
3✔
513
    }
514

515
    @Override
516
    @NonNull
517
    public MethodMetadata getFactoryMethodMetadata() {
518
      return this.factoryMethodMetadata;
3✔
519
    }
520

521
    @Override
522
    public boolean isFactoryMethod(Method candidate) {
523
      return super.isFactoryMethod(candidate)
6✔
524
              && BeanAnnotationHelper.isBeanAnnotated(candidate)
3✔
525
              && BeanAnnotationHelper.determineBeanNameFor(candidate).equals(localBeanName);
8✔
526
    }
527

528
    @Override
529
    public ConfigurationClassBeanDefinition cloneBeanDefinition() {
530
      return new ConfigurationClassBeanDefinition(this);
5✔
531
    }
532
  }
533

534
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc