Skip to content

Commit

Permalink
allow registering bean definitions prior to startup (#10800)
Browse files Browse the repository at this point in the history
  • Loading branch information
graemerocher committed May 7, 2024
1 parent 905467b commit 79e12c7
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 0 deletions.
6 changes: 6 additions & 0 deletions context/src/main/java/io/micronaut/runtime/Micronaut.java
Expand Up @@ -18,6 +18,7 @@
import io.micronaut.context.ApplicationContext;
import io.micronaut.context.ApplicationContextBuilder;
import io.micronaut.context.DefaultApplicationContextBuilder;
import io.micronaut.context.RuntimeBeanDefinition;
import io.micronaut.context.banner.Banner;
import io.micronaut.context.banner.MicronautBanner;
import io.micronaut.context.banner.ResourceBanner;
Expand Down Expand Up @@ -220,6 +221,11 @@ private static long elapsedMillis(long startNanos) {
return (Micronaut) super.singletons(beans);
}

@Override
public Micronaut beanDefinitions(@NonNull RuntimeBeanDefinition<?>... definitions) {
return (Micronaut) super.beanDefinitions(definitions);
}

@Override
public @NonNull Micronaut propertySources(@Nullable PropertySource... propertySources) {
return (Micronaut) super.propertySources(propertySources);
Expand Down
Expand Up @@ -17,6 +17,19 @@ class RuntimeBeanDefinitionSpec extends AbstractTypeElementSpec {
@Shared
BeanContext sharedContext = BeanContext.build()

void "test runtime bean definition registered with builder"() {
given:
def foo = new Foo()
def context = ApplicationContext.builder()
.beanDefinitions(RuntimeBeanDefinition.of(foo))
.build()
.start()

expect:
context.getBeanDefinition(Foo)
context.getBean(Foo).is(foo)
}

void 'test simple runtime bean definition'() {
given:

Expand Down
Expand Up @@ -96,6 +96,16 @@ public interface ApplicationContextBuilder {
*/
@NonNull ApplicationContextBuilder singletons(@Nullable Object... beans);

/**
* Register additional runtime bean definitions prior to startup.
* @param definitions The definitions.
* @return The context builder
* @since 4.5.0
*/
default @NonNull ApplicationContextBuilder beanDefinitions(@NonNull RuntimeBeanDefinition<?>... definitions) {
return this;
};

/**
* If set to {@code true} (the default is {@code true}) Micronaut will attempt to automatically deduce the environment
* it is running in using environment variables and/or stack trace inspection.
Expand Down
Expand Up @@ -49,6 +49,7 @@
*/
public class DefaultApplicationContextBuilder implements ApplicationContextBuilder, ApplicationContextConfiguration {
private final List<Object> singletons = new ArrayList<>();
private final List<RuntimeBeanDefinition<?>> beanDefinitions = new ArrayList<>();
private final List<String> environments = new ArrayList<>();
private final List<String> defaultEnvironments = new ArrayList<>();
private final List<String> packages = new ArrayList<>();
Expand Down Expand Up @@ -153,6 +154,14 @@ public Set<Class<? extends Annotation>> getEagerInitAnnotated() {
return this;
}

@Override
public ApplicationContextBuilder beanDefinitions(@NonNull RuntimeBeanDefinition<?>... definitions) {
if (definitions != null) {
beanDefinitions.addAll(Arrays.asList(definitions));
}
return this;
}

@Override
public @NonNull ClassPathResourceLoader getResourceLoader() {
if (classPathResourceLoader == null) {
Expand Down Expand Up @@ -346,6 +355,12 @@ public boolean isEnvironmentPropertySource() {
}
}

if (!beanDefinitions.isEmpty()) {
for (RuntimeBeanDefinition<?> beanDefinition : beanDefinitions) {
applicationContext.registerBeanDefinition(beanDefinition);
}
}

if (!configurationIncludes.isEmpty()) {
environment.addConfigurationIncludes(configurationIncludes.toArray(StringUtils.EMPTY_STRING_ARRAY));
}
Expand Down

0 comments on commit 79e12c7

Please sign in to comment.