Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow registering bean definitions prior to startup #10800

Merged
merged 1 commit into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions context/src/main/java/io/micronaut/runtime/Micronaut.java
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Loading