-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve ability to introspect bean context prior to start (#10805)
The existing `InitializableBeanContext` is problematic because it still creates context scoped beans and runs event listeners. We need a way to configure the context and allow it to be introspected prior to start so this deprecates `InitializableBeanContext` and replaces it with `ConfigurableBeanContext` and `ConfigurableApplicationContext` interfaces. These feature a `configure()` method that loads the bean definitions and state needed to start the context but doesn't actually start it. The PR also adds logic to assert the context is running when resolving beans which was missing before and could lead to odd behaviour.
- Loading branch information
1 parent
3ad933b
commit 1a8fc61
Showing
13 changed files
with
348 additions
and
78 deletions.
There are no files selected for viewing
16 changes: 16 additions & 0 deletions
16
...-java/src/test/groovy/io/micronaut/inject/context/ApplicationContextConfigurerSpec.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package io.micronaut.inject.context | ||
|
||
import io.micronaut.context.ApplicationContext | ||
import spock.lang.Specification | ||
|
||
class ApplicationContextConfigurerSpec | ||
extends Specification { | ||
|
||
void 'test context configurer registers bean'() { | ||
given: | ||
def ctx = ApplicationContext.run() | ||
|
||
expect:"bean is registered" | ||
ctx.getBean(Foo) | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
inject-java/src/test/groovy/io/micronaut/inject/context/TestContextConfigurer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package io.micronaut.inject.context; | ||
|
||
import io.micronaut.context.ApplicationContext; | ||
import io.micronaut.context.ApplicationContextConfigurer; | ||
import io.micronaut.context.RuntimeBeanDefinition; | ||
import io.micronaut.context.annotation.ContextConfigurer; | ||
|
||
@ContextConfigurer | ||
public class TestContextConfigurer | ||
implements ApplicationContextConfigurer { | ||
@Override | ||
public void configure(ApplicationContext applicationContext) { | ||
applicationContext.registerBeanDefinition( | ||
RuntimeBeanDefinition.of(new Foo()) | ||
); | ||
} | ||
} | ||
|
||
class Foo {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
inject/src/main/java/io/micronaut/context/ConfigurableApplicationContext.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* | ||
* Copyright 2017-2024 original authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.micronaut.context; | ||
|
||
/** | ||
* Extended version of the {@link ApplicationContext} that | ||
* allows loading the context configuration without starting the context. | ||
* | ||
* @see ConfigurableBeanContext#configure() | ||
* @since 4.5.0 | ||
*/ | ||
public interface ConfigurableApplicationContext | ||
extends ApplicationContext, ConfigurableBeanContext { | ||
} |
38 changes: 38 additions & 0 deletions
38
inject/src/main/java/io/micronaut/context/ConfigurableBeanContext.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* Copyright 2017-2024 original authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.micronaut.context; | ||
|
||
/** | ||
* Extends {@link BeanContext} interface and allows the bean context to be configured but not started. | ||
* | ||
* @since 4.5.0 | ||
*/ | ||
public interface ConfigurableBeanContext | ||
extends BeanContext { | ||
|
||
/** | ||
* Configures the bean context loading all bean definitions | ||
* required to perform successful startup without starting the context itself. | ||
* | ||
* <p>Once called the methods of the {@link BeanDefinitionRegistry} interface will | ||
* return results allowing inspection of the context without needing to run the context.</p> | ||
* | ||
* @see io.micronaut.inject.BeanDefinition | ||
* @see #getBeanDefinition(Class) | ||
* @see #start() | ||
*/ | ||
void configure(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.