|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2023 the original author or authors. |
| 2 | + * Copyright 2002-2025 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
22 | 22 |
|
23 | 23 | import org.jspecify.annotations.Nullable;
|
24 | 24 |
|
| 25 | +import org.springframework.beans.factory.BeanRegistrar; |
25 | 26 | import org.springframework.beans.factory.support.BeanNameGenerator;
|
| 27 | +import org.springframework.beans.factory.support.BeanRegistryAdapter; |
26 | 28 | import org.springframework.beans.factory.support.DefaultListableBeanFactory;
|
27 | 29 | import org.springframework.context.annotation.AnnotatedBeanDefinitionReader;
|
28 | 30 | import org.springframework.context.annotation.AnnotationConfigRegistry;
|
@@ -104,6 +106,8 @@ public class AnnotationConfigWebApplicationContext extends AbstractRefreshableWe
|
104 | 106 |
|
105 | 107 | private @Nullable ScopeMetadataResolver scopeMetadataResolver;
|
106 | 108 |
|
| 109 | + private final Set<BeanRegistrar> beanRegistrars = new LinkedHashSet<>(); |
| 110 | + |
107 | 111 | private final Set<Class<?>> componentClasses = new LinkedHashSet<>();
|
108 | 112 |
|
109 | 113 | private final Set<String> basePackages = new LinkedHashSet<>();
|
@@ -148,6 +152,20 @@ public void setScopeMetadataResolver(@Nullable ScopeMetadataResolver scopeMetada
|
148 | 152 | }
|
149 | 153 |
|
150 | 154 |
|
| 155 | + /** |
| 156 | + * Invoke the given registrars for registering their beans with this |
| 157 | + * application context. |
| 158 | + * <p>Note that {@link #refresh()} must be called in order for the context |
| 159 | + * to fully process the new classes. |
| 160 | + * @param registrars one or more {@link BeanRegistrar} instances |
| 161 | + * @since 7.0 |
| 162 | + */ |
| 163 | + @Override |
| 164 | + public void register(BeanRegistrar... registrars) { |
| 165 | + Assert.notEmpty(registrars, "At least one BeanRegistrar must be specified"); |
| 166 | + Collections.addAll(this.beanRegistrars, registrars); |
| 167 | + } |
| 168 | + |
151 | 169 | /**
|
152 | 170 | * Register one or more component classes to be processed.
|
153 | 171 | * <p>Note that {@link #refresh()} must be called in order for the context
|
@@ -222,6 +240,16 @@ protected void loadBeanDefinitions(DefaultListableBeanFactory beanFactory) {
|
222 | 240 | scanner.setScopeMetadataResolver(scopeMetadataResolver);
|
223 | 241 | }
|
224 | 242 |
|
| 243 | + if (!this.beanRegistrars.isEmpty()) { |
| 244 | + if (logger.isDebugEnabled()) { |
| 245 | + logger.debug("Applying bean registrars: [" + |
| 246 | + StringUtils.collectionToCommaDelimitedString(this.beanRegistrars) + "]"); |
| 247 | + } |
| 248 | + for (BeanRegistrar registrar : this.beanRegistrars) { |
| 249 | + new BeanRegistryAdapter(beanFactory, getEnvironment(), registrar.getClass()).register(registrar); |
| 250 | + } |
| 251 | + } |
| 252 | + |
225 | 253 | if (!this.componentClasses.isEmpty()) {
|
226 | 254 | if (logger.isDebugEnabled()) {
|
227 | 255 | logger.debug("Registering component classes: [" +
|
|
0 commit comments