Add more bridges
This commit is contained in:
@@ -20,6 +20,20 @@ public abstract class AbstractCompleteWiringTests {
|
||||
|
||||
protected abstract Injector createInjector();
|
||||
|
||||
@Test
|
||||
public void injectInstance() {
|
||||
Bar bar = new Bar();
|
||||
injector.injectMembers(bar);
|
||||
assertNotNull(bar.service);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void memberInjector() {
|
||||
Bar bar = new Bar();
|
||||
injector.getMembersInjector(Bar.class).injectMembers(bar);
|
||||
assertNotNull(bar.service);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getInstanceUnbound() {
|
||||
assertNotNull(injector.getInstance(Foo.class));
|
||||
@@ -30,6 +44,16 @@ public abstract class AbstractCompleteWiringTests {
|
||||
assertNotNull(injector.getInstance(Service.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getProviderUnbound() {
|
||||
assertNotNull(injector.getProvider(Foo.class).get());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getProviderBound() {
|
||||
assertNotNull(injector.getProvider(Service.class).get());
|
||||
}
|
||||
|
||||
interface Service {
|
||||
}
|
||||
|
||||
@@ -44,4 +68,15 @@ public abstract class AbstractCompleteWiringTests {
|
||||
|
||||
}
|
||||
|
||||
public static class Bar {
|
||||
|
||||
private Service service;
|
||||
|
||||
@Inject
|
||||
public void setService(Service service) {
|
||||
this.service = service;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -22,7 +22,7 @@ import com.google.inject.Injector;
|
||||
* @author Dave Syer
|
||||
*
|
||||
*/
|
||||
public class HybridWiringTests extends AbstractCompleteWiringTests {
|
||||
public class ModuleBeanWiringTests extends AbstractCompleteWiringTests {
|
||||
|
||||
@Override
|
||||
protected Injector createInjector() {
|
||||
@@ -45,17 +45,22 @@ public class SpringInjector implements Injector {
|
||||
|
||||
@Override
|
||||
public void injectMembers(Object instance) {
|
||||
|
||||
context.getAutowireCapableBeanFactory().autowireBean(instance);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> MembersInjector<T> getMembersInjector(TypeLiteral<T> typeLiteral) {
|
||||
return null;
|
||||
return new MembersInjector<T>() {
|
||||
@Override
|
||||
public void injectMembers(T instance) {
|
||||
context.getAutowireCapableBeanFactory().autowireBean(instance);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> MembersInjector<T> getMembersInjector(Class<T> type) {
|
||||
return null;
|
||||
return getMembersInjector(TypeLiteral.get(type));
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -90,17 +95,36 @@ public class SpringInjector implements Injector {
|
||||
|
||||
@Override
|
||||
public <T> Provider<T> getProvider(Key<T> key) {
|
||||
return null;
|
||||
// TODO: support for other metadata in the key
|
||||
@SuppressWarnings("unchecked")
|
||||
Provider<T> provider = (Provider<T>) getProvider(key.getTypeLiteral().getRawType());
|
||||
return provider;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> Provider<T> getProvider(Class<T> type) {
|
||||
return null;
|
||||
if (context.getBeanNamesForType(type).length==0) {
|
||||
if (injector!=null && injector.getExistingBinding(Key.get(type))!=null) {
|
||||
return injector.getProvider(type);
|
||||
}
|
||||
// TODO: use prototype scope?
|
||||
context.getDefaultListableBeanFactory().registerBeanDefinition(type.getSimpleName(), new RootBeanDefinition(type));
|
||||
}
|
||||
final Class<T> cls = type;
|
||||
return new Provider<T>() {
|
||||
@Override
|
||||
public T get() {
|
||||
return context.getBean(cls);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T getInstance(Key<T> key) {
|
||||
return null;
|
||||
// TODO: support for other metadata in the key
|
||||
@SuppressWarnings("unchecked")
|
||||
T provider = (T) getInstance(key.getTypeLiteral().getRawType());
|
||||
return provider;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
60
src/test/java/demo/SpringModule.java
Normal file
60
src/test/java/demo/SpringModule.java
Normal file
@@ -0,0 +1,60 @@
|
||||
/*
|
||||
* 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 demo;
|
||||
|
||||
import org.springframework.beans.factory.config.BeanDefinition;
|
||||
import org.springframework.beans.factory.support.AbstractBeanDefinition;
|
||||
import org.springframework.context.support.GenericApplicationContext;
|
||||
import org.springframework.util.ClassUtils;
|
||||
|
||||
import com.google.inject.Binder;
|
||||
import com.google.inject.Module;
|
||||
import com.google.inject.Provider;
|
||||
|
||||
/**
|
||||
* @author Dave Syer
|
||||
*
|
||||
*/
|
||||
public class SpringModule implements Module {
|
||||
|
||||
private GenericApplicationContext context;
|
||||
|
||||
public SpringModule(GenericApplicationContext context) {
|
||||
this.context = context;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void configure(Binder binder) {
|
||||
for (String name : context.getBeanDefinitionNames()) {
|
||||
BeanDefinition definition = context.getBeanDefinition(name);
|
||||
if (definition.isAutowireCandidate() && definition.getRole() == AbstractBeanDefinition.ROLE_APPLICATION) {
|
||||
Class<?> type = context.getType(name);
|
||||
@SuppressWarnings("unchecked")
|
||||
final Class<Object> cls = (Class<Object>) type;
|
||||
Provider<Object> provider = new Provider<Object>() {
|
||||
@Override
|
||||
public Object get() {
|
||||
return context.getBean(cls);
|
||||
}
|
||||
};
|
||||
for (Class<?> iface : ClassUtils.getAllInterfacesForClass(cls)) {
|
||||
@SuppressWarnings("unchecked")
|
||||
Class<Object> unchecked = (Class<Object>) iface;
|
||||
binder.bind(unchecked).toProvider(provider);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
45
src/test/java/demo/SpringModuleWiringTests.java
Normal file
45
src/test/java/demo/SpringModuleWiringTests.java
Normal file
@@ -0,0 +1,45 @@
|
||||
/*
|
||||
* 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 demo;
|
||||
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import com.google.inject.Guice;
|
||||
import com.google.inject.Injector;
|
||||
|
||||
/**
|
||||
* @author Dave Syer
|
||||
*
|
||||
*/
|
||||
public class SpringModuleWiringTests extends AbstractCompleteWiringTests {
|
||||
|
||||
@Override
|
||||
protected Injector createInjector() {
|
||||
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
|
||||
context.register(TestConfig.class);
|
||||
context.refresh();
|
||||
return Guice.createInjector(new SpringModule(context));
|
||||
}
|
||||
|
||||
@Configuration
|
||||
public static class TestConfig {
|
||||
@Bean
|
||||
public Service service() {
|
||||
return new MyService();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user