Switch to supplier for one bean definition

This commit is contained in:
Dave Syer
2023-02-03 07:45:30 +00:00
parent 279c6674e2
commit 1726fa780a
2 changed files with 9 additions and 5 deletions

View File

@@ -38,7 +38,6 @@ class GuiceFactoryBean<T> implements FactoryBean<T> {
private final boolean isSingleton;
@Autowired
private Injector injector;
GuiceFactoryBean(Class<T> beanType, Key<T> key, boolean isSingleton) {
@@ -47,6 +46,11 @@ class GuiceFactoryBean<T> implements FactoryBean<T> {
this.isSingleton = isSingleton;
}
@Autowired
void setInjector(Injector injector) {
this.injector = injector;
}
@Override
public T getObject() throws Exception {
return (T) this.injector.getInstance(this.key);

View File

@@ -160,10 +160,10 @@ class ModuleRegistryConfiguration implements BeanDefinitionRegistryPostProcessor
// Register the injector initializer
RootBeanDefinition beanDefinition = new RootBeanDefinition(GuiceInjectorInitializer.class);
ConstructorArgumentValues args = new ConstructorArgumentValues();
args.addIndexedArgumentValue(0, modules);
args.addIndexedArgumentValue(1, this.applicationContext);
beanDefinition.setConstructorArgumentValues(args);
final List<Module> finalModules = new ArrayList<>(modules);
beanDefinition.setInstanceSupplier(() -> new GuiceInjectorInitializer(finalModules,
(ConfigurableApplicationContext) this.applicationContext));
beanDefinition.setAttribute(SpringModule.SPRING_GUICE_SOURCE, true);
registry.registerBeanDefinition("guiceInjectorInitializer", beanDefinition);
}