Use static factory methods and proxyBeanMethods = false

It is better to be explicit about beans of type `Module` and make
sure they don't interfere with Spring trying to enhance
`@Configuration` classes. The tests are now clear of the pesky
warning logs, and there is a new section in the README.
This commit is contained in:
Dave Syer
2022-03-20 16:27:23 +00:00
committed by GitHub
parent c22fb6a976
commit e71d6aca56
21 changed files with 54 additions and 25 deletions

View File

@@ -97,7 +97,7 @@ bindings. Example:
public static class TestConfig {
@Bean
public MyModule myModule() {
public static MyModule myModule() {
return new MyModule();
}
@@ -112,6 +112,35 @@ public static class TestConfig {
The `Service` was defined in the Guice module `MyModule`, and then it
was be bound to the autowired `spam()` method when Spring started.
## Configuration Class Enhancements
Note that the `Module` bean definition in the example above is
declared in a `static` method. This is intentional and can be used to
avoid accidentally preventing Spring from being able to enhance the
parent `@Configuration` class. The default behaviour for Spring is to
create a proxy for `@Configuration` classes so the `@Bean` methods
can call each other and Spring will preserve the singleton nature of
the bean factory - one bean of each type per bean id.
If you see logs like this when the context starts:
```
Mar 21, 2022 8:30:35 AM org.springframework.context.annotation.ConfigurationClassPostProcessor enhanceConfigurationClasses
INFO: Cannot enhance @Configuration bean definition 'TestConfig' since its singleton instance has been created too early. The typical cause is a non-static @Bean method with a BeanDefinitionRegistryPostProcessor return type: Consider declaring such methods as 'static'.
```
that is a sign that you might want to use static methods to define
any beans of type `Module`. It is logged at INFO because Spring
doesn't know if it was intentional or not. Most likely it was
unintentional, and it is better to avoid nasty surprises later
if we can.
Another way to avoid the warning is to declare the parent class as
`@Configuration(proxyBeanMethods = false)` so that Spring knows that
you don't even want to enhance the class. It's not a bad idea to use
that flag wherever you can because it saves some time on start up
if the proxy doesn't need to be created.
## Using Guice as an API for accessing a Spring ApplicationContext
In this case the main feature is an `Injector` implementation that

View File

@@ -76,7 +76,7 @@ import org.springframework.guice.module.SpringModule;
* @author Howard Yuan
*
*/
@Configuration
@Configuration(proxyBeanMethods = false)
@Order(Ordered.HIGHEST_PRECEDENCE)
class ModuleRegistryConfiguration implements BeanDefinitionRegistryPostProcessor, ApplicationContextAware {

View File

@@ -97,7 +97,7 @@ public class BeanPostProcessorTests {
static class BeanPostProcessorTestConfig {
@Bean
PostProcessorRegistrar postProcessorRegistrar() {
static PostProcessorRegistrar postProcessorRegistrar() {
return new PostProcessorRegistrar();
}
@@ -112,7 +112,7 @@ public class BeanPostProcessorTests {
}
@Bean
Module someGuiceModule() {
static Module someGuiceModule() {
return new AbstractModule() {
@Override

View File

@@ -222,7 +222,7 @@ public class BindingAnnotationTests {
}
@Bean
AbstractModule module() {
static AbstractModule module() {
return new AbstractModule() {
@Override
protected void configure() {

View File

@@ -83,7 +83,7 @@ public class BindingDeduplicationTests {
}
@Bean
Module module() {
static Module module() {
return new AbstractModule() {
@Override
protected void configure() {

View File

@@ -93,7 +93,7 @@ public class DuplicateNamesDifferentTypesTests {
static class DuplicateNamesDifferentTypesTestsConfig {
@Bean
Module module() {
static Module module() {
return new AbstractModule() {
@Override
protected void configure() {

View File

@@ -105,7 +105,7 @@ public class ElementVisitorTests {
}
@Bean
Module module() {
static Module module() {
return new AbstractModule() {
@Override
protected void configure() {

View File

@@ -71,7 +71,7 @@ public class LazyInitializationTests {
static class GuiceConfig {
@Bean
GuiceModule guiceModule() {
static GuiceModule guiceModule() {
return new GuiceModule();
}

View File

@@ -49,7 +49,7 @@ public class MapWiringTests {
static class ModulesConfig {
@Bean
TestConfig testConfig() {
static TestConfig testConfig() {
return new TestConfig();
}

View File

@@ -93,7 +93,7 @@ public class ModuleFilteringTests {
}
@Bean
Module module() {
static Module module() {
return new AbstractModule() {
@Override

View File

@@ -121,7 +121,7 @@ public class PrivateModuleTests {
}
@Bean
Module module() {
static Module module() {
return new AbstractModule() {
@Override
protected void configure() {

View File

@@ -49,7 +49,7 @@ public class PrototypeScopedBeanTests {
static class ModulesConfig {
@Bean
Module guiceModule() {
static Module guiceModule() {
return new AbstractModule() {
@Override
protected void configure() {

View File

@@ -71,7 +71,7 @@ public class ProvidesSupplierWiringTests {
static class ModulesConfig {
@Bean
TestConfig testConfig() {
static TestConfig testConfig() {
return new TestConfig();
}

View File

@@ -91,7 +91,7 @@ public class ScopingTests {
static class ScopingTestsConfig {
@Bean
Module module() {
static Module module() {
return new AbstractModule() {
@Override
protected void configure() {

View File

@@ -61,7 +61,7 @@ public class SpringAutowiredCollectionTests {
}
@Bean
GuiceModule guiceServiceModule() {
static GuiceModule guiceServiceModule() {
return new GuiceModule();
}

View File

@@ -361,7 +361,7 @@ public class SuperClassTests {
static class DisableJITConfig {
@Bean
AbstractModule disableJITModule() {
static AbstractModule disableJITModule() {
return new AbstractModule() {
@Override
protected void configure() {

View File

@@ -119,7 +119,7 @@ public class EnableGuiceModulesTests {
}
@Configuration
@Configuration(proxyBeanMethods = false)
@EnableGuiceModules
protected static class ModuleConfig extends AbstractModule {
@@ -135,12 +135,12 @@ public class EnableGuiceModulesTests {
}
@Configuration
@Configuration(proxyBeanMethods = false)
@EnableGuiceModules
protected static class ModuleBeanConfig {
@Bean
public MyModule module() {
public static MyModule module() {
return new MyModule();
}
@@ -204,7 +204,7 @@ public class EnableGuiceModulesTests {
}
@Bean
public MyGuiceModule bazModule() {
public static MyGuiceModule bazModule() {
return new MyGuiceModule();
}

View File

@@ -56,7 +56,7 @@ public class ModuleBeanWiringTests extends AbstractCompleteWiringTests {
}
@EnableGuiceModules
@Configuration
@Configuration(proxyBeanMethods = false)
public static class TestConfig extends AbstractModule {
@Autowired

View File

@@ -56,7 +56,7 @@ public class ModuleNamedBeanWiringTests extends AbstractCompleteWiringTests {
}
@EnableGuiceModules
@Configuration
@Configuration(proxyBeanMethods = false)
public static class TestConfig extends AbstractModule {
@Autowired

View File

@@ -65,7 +65,7 @@ public class DevelepmentStageInjectorTest {
static class ModulesConfig {
@Bean
TestGuiceModule testGuiceModule() {
static TestGuiceModule testGuiceModule() {
return new TestGuiceModule();
}

View File

@@ -79,7 +79,7 @@ public class SpringModuleWrappedTests {
protected static class ModuleProviderConfig {
@Bean
public ProviderModule module() {
public static ProviderModule module() {
return new ProviderModule();
}