Add limitations
This commit is contained in:
@@ -13,7 +13,12 @@
|
||||
|
||||
package org.springframework.guice;
|
||||
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import com.google.inject.AbstractModule;
|
||||
import com.google.inject.Injector;
|
||||
@@ -24,19 +29,38 @@ import com.google.inject.Injector;
|
||||
*/
|
||||
public class ModuleBeanWiringTests extends AbstractCompleteWiringTests {
|
||||
|
||||
private AnnotationConfigApplicationContext context;
|
||||
|
||||
@Override
|
||||
protected Injector createInjector() {
|
||||
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
|
||||
context.register(TestConfig.class, ModuleRegistryConfiguration.class);
|
||||
context = new AnnotationConfigApplicationContext();
|
||||
context.register(TestConfig.class);
|
||||
context.refresh();
|
||||
return new SpringInjector(context);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void bindToSpringBeanFromGuiceModule() throws Exception {
|
||||
assertNotNull(context.getBean(Spam.class));
|
||||
}
|
||||
|
||||
@EnableGuiceModules
|
||||
@Configuration
|
||||
public static class TestConfig extends AbstractModule {
|
||||
@Override
|
||||
protected void configure() {
|
||||
bind(Service.class).to(MyService.class);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Spam spam(Service service) {
|
||||
return new Spam(service);
|
||||
}
|
||||
}
|
||||
|
||||
protected static class Spam {
|
||||
public Spam(Service service) {
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,69 +0,0 @@
|
||||
/*
|
||||
* Copyright 2013-2014 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;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.beans.factory.ObjectFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import com.google.inject.Binding;
|
||||
import com.google.inject.Guice;
|
||||
import com.google.inject.Injector;
|
||||
import com.google.inject.Key;
|
||||
import com.google.inject.Module;
|
||||
import com.google.inject.Provider;
|
||||
|
||||
@Configuration
|
||||
public class ModuleRegistryConfiguration {
|
||||
|
||||
@Autowired
|
||||
private ConfigurableListableBeanFactory beanFactory;
|
||||
|
||||
@Autowired(required=false)
|
||||
private List<Module> modules = Collections.emptyList();
|
||||
|
||||
private Injector injector;
|
||||
|
||||
@Bean
|
||||
public Injector injector() {
|
||||
return injector;
|
||||
}
|
||||
|
||||
@PostConstruct
|
||||
public void init() {
|
||||
injector = Guice.createInjector(modules);
|
||||
for (Entry<Key<?>, Binding<?>> entry : injector.getBindings().entrySet()) {
|
||||
if (entry.getKey().getTypeLiteral().getRawType().equals(Injector.class)) {
|
||||
continue;
|
||||
}
|
||||
final Provider<?> provider = entry.getValue().getProvider();
|
||||
beanFactory.registerResolvableDependency(entry.getKey().getTypeLiteral().getRawType(), new ObjectFactory<Object>() {
|
||||
@Override
|
||||
public Object getObject() throws BeansException {
|
||||
return provider.get();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user