Add support for BeanRegistrar registration on GenericApplicationContext

Closes gh-34574
This commit is contained in:
Juergen Hoeller
2025-03-11 21:18:20 +01:00
parent beb3a91847
commit 86b21d9b5c
5 changed files with 80 additions and 7 deletions

View File

@@ -60,9 +60,10 @@ import org.springframework.core.env.Environment;
public interface BeanRegistrar {
/**
* Register beans in a programmatic way.
* @param registry the bean registry
* Register beans on the given {@link BeanRegistry} in a programmatic way.
* @param registry the bean registry to operate on
* @param env the environment that can be used to get the active profile or some properties
*/
void register(BeanRegistry registry, Environment env);
}

View File

@@ -56,6 +56,12 @@ public class BeanRegistryAdapter implements BeanRegistry {
private final @Nullable MultiValueMap<String, BeanDefinitionCustomizer> customizers;
public BeanRegistryAdapter(DefaultListableBeanFactory beanFactory, Environment environment,
Class<? extends BeanRegistrar> beanRegistrarClass) {
this(beanFactory, beanFactory, environment, beanRegistrarClass, null);
}
public BeanRegistryAdapter(BeanDefinitionRegistry beanRegistry, ListableBeanFactory beanFactory,
Environment environment, Class<? extends BeanRegistrar> beanRegistrarClass) {
@@ -73,6 +79,7 @@ public class BeanRegistryAdapter implements BeanRegistry {
this.customizers = customizers;
}
@Override
public <T> String registerBean(Class<T> beanClass) {
String beanName = BeanDefinitionReaderUtils.uniqueBeanName(beanClass.getName(), this.beanRegistry);
@@ -154,7 +161,8 @@ public class BeanRegistryAdapter implements BeanRegistry {
}
}
static class BeanSpecAdapter<T> implements Spec<T> {
private static class BeanSpecAdapter<T> implements Spec<T> {
private final RootBeanDefinition beanDefinition;
@@ -239,7 +247,8 @@ public class BeanRegistryAdapter implements BeanRegistry {
}
}
static class SupplierContextAdapter implements SupplierContext {
private static class SupplierContextAdapter implements SupplierContext {
private final ListableBeanFactory beanFactory;