Add support for BeanRegistrar registration on GenericApplicationContext
Closes gh-34574
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2023 the original author or authors.
|
||||
* Copyright 2002-2025 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.
|
||||
@@ -22,7 +22,9 @@ import java.util.Set;
|
||||
|
||||
import org.jspecify.annotations.Nullable;
|
||||
|
||||
import org.springframework.beans.factory.BeanRegistrar;
|
||||
import org.springframework.beans.factory.support.BeanNameGenerator;
|
||||
import org.springframework.beans.factory.support.BeanRegistryAdapter;
|
||||
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
|
||||
import org.springframework.context.annotation.AnnotatedBeanDefinitionReader;
|
||||
import org.springframework.context.annotation.AnnotationConfigRegistry;
|
||||
@@ -104,6 +106,8 @@ public class AnnotationConfigWebApplicationContext extends AbstractRefreshableWe
|
||||
|
||||
private @Nullable ScopeMetadataResolver scopeMetadataResolver;
|
||||
|
||||
private final Set<BeanRegistrar> beanRegistrars = new LinkedHashSet<>();
|
||||
|
||||
private final Set<Class<?>> componentClasses = new LinkedHashSet<>();
|
||||
|
||||
private final Set<String> basePackages = new LinkedHashSet<>();
|
||||
@@ -148,6 +152,20 @@ public class AnnotationConfigWebApplicationContext extends AbstractRefreshableWe
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Invoke the given registrars for registering their beans with this
|
||||
* application context.
|
||||
* <p>Note that {@link #refresh()} must be called in order for the context
|
||||
* to fully process the new classes.
|
||||
* @param registrars one or more {@link BeanRegistrar} instances
|
||||
* @since 7.0
|
||||
*/
|
||||
@Override
|
||||
public void register(BeanRegistrar... registrars) {
|
||||
Assert.notEmpty(registrars, "At least one BeanRegistrar must be specified");
|
||||
Collections.addAll(this.beanRegistrars, registrars);
|
||||
}
|
||||
|
||||
/**
|
||||
* Register one or more component classes to be processed.
|
||||
* <p>Note that {@link #refresh()} must be called in order for the context
|
||||
@@ -222,6 +240,16 @@ public class AnnotationConfigWebApplicationContext extends AbstractRefreshableWe
|
||||
scanner.setScopeMetadataResolver(scopeMetadataResolver);
|
||||
}
|
||||
|
||||
if (!this.beanRegistrars.isEmpty()) {
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("Applying bean registrars: [" +
|
||||
StringUtils.collectionToCommaDelimitedString(this.beanRegistrars) + "]");
|
||||
}
|
||||
for (BeanRegistrar registrar : this.beanRegistrars) {
|
||||
new BeanRegistryAdapter(beanFactory, getEnvironment(), registrar.getClass()).register(registrar);
|
||||
}
|
||||
}
|
||||
|
||||
if (!this.componentClasses.isEmpty()) {
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("Registering component classes: [" +
|
||||
|
||||
Reference in New Issue
Block a user