Ensure *Aware contracts can be satisfied in ModuleRegistryConfiguration

If the BeanFactoryProvider is used there is a partially refreshed
ApplicationContext floating around. It works better if it is able to
inject Environment etc. via the Aware interfaces into early beans
like post processors.
This commit is contained in:
Dave Syer
2019-01-10 09:21:51 +00:00
parent af2497c6d8
commit fd835487d3
3 changed files with 152 additions and 44 deletions

View File

@@ -0,0 +1,95 @@
/*
* Copyright 2016-2017 the original author or 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
*
* http://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 org.springframework.guice.module;
import javax.inject.Inject;
import javax.inject.Named;
import com.google.inject.AbstractModule;
import com.google.inject.Guice;
import com.google.inject.Injector;
import org.junit.Test;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.guice.annotation.EnableGuiceModules;
import static org.junit.Assert.assertNotNull;
public class SpringModuleWrappedTests {
@Test
public void testDependenciesFromWrappedModule() {
Injector injector = Guice.createInjector(new SpringModule(
BeanFactoryProvider.from(TestConfig.class, ModuleProviderConfig.class)));
assertNotNull(injector.getInstance(Baz.class));
}
@Configuration
public static class TestConfig {
@Bean
public Baz baz(Service service) {
return new Baz(service);
}
}
interface Service {
}
protected static class MyService implements Service {
}
public static class Foo {
@Inject
public Foo(@Named("service") Service service) {
service.toString();
}
}
public static class Baz {
@Inject
public Baz(Service service) {
}
}
@Configuration
@EnableGuiceModules
protected static class ModuleProviderConfig {
@Bean
public ProviderModule module() {
return new ProviderModule();
}
@Bean
public Foo service(Service service) {
return new Foo(service);
}
}
protected static class ProviderModule extends AbstractModule {
@Override
protected void configure() {
bind(Service.class).toProvider(() -> new MyService());
}
}
}