Ensure singleton with no interface works
This commit is contained in:
@@ -32,14 +32,16 @@ public class SpringModule implements Module {
|
||||
private DefaultListableBeanFactory beanFactory;
|
||||
|
||||
public SpringModule(GenericApplicationContext context) {
|
||||
this.beanFactory = (DefaultListableBeanFactory) context.getAutowireCapableBeanFactory();
|
||||
this.beanFactory = (DefaultListableBeanFactory) context
|
||||
.getAutowireCapableBeanFactory();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void configure(Binder binder) {
|
||||
for (String name : beanFactory.getBeanDefinitionNames()) {
|
||||
BeanDefinition definition = beanFactory.getBeanDefinition(name);
|
||||
if (definition.isAutowireCandidate() && definition.getRole() == AbstractBeanDefinition.ROLE_APPLICATION) {
|
||||
if (definition.isAutowireCandidate()
|
||||
&& definition.getRole() == AbstractBeanDefinition.ROLE_APPLICATION) {
|
||||
Class<?> type = beanFactory.getType(name);
|
||||
@SuppressWarnings("unchecked")
|
||||
final Class<Object> cls = (Class<Object>) type;
|
||||
@@ -49,6 +51,9 @@ public class SpringModule implements Module {
|
||||
return beanFactory.getBean(cls);
|
||||
}
|
||||
};
|
||||
if (!cls.isInterface()) {
|
||||
binder.bind(cls).toProvider(provider);
|
||||
}
|
||||
for (Class<?> iface : ClassUtils.getAllInterfacesForClass(cls)) {
|
||||
@SuppressWarnings("unchecked")
|
||||
Class<Object> unchecked = (Class<Object>) iface;
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package org.springframework.guice;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
import javax.inject.Inject;
|
||||
@@ -44,6 +45,13 @@ public abstract class AbstractCompleteWiringTests {
|
||||
assertNotNull(injector.getInstance(Service.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getInstanceBoundWithNoInterface() {
|
||||
Baz instance = injector.getInstance(Baz.class);
|
||||
assertNotNull(instance);
|
||||
assertEquals(instance, injector.getInstance(Baz.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getProviderUnbound() {
|
||||
assertNotNull(injector.getProvider(Foo.class).get());
|
||||
@@ -79,4 +87,12 @@ public abstract class AbstractCompleteWiringTests {
|
||||
|
||||
}
|
||||
|
||||
public static class Baz {
|
||||
|
||||
@Inject
|
||||
public Baz(Service service) {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -13,6 +13,8 @@
|
||||
|
||||
package org.springframework.guice;
|
||||
|
||||
import javax.inject.Singleton;
|
||||
|
||||
import com.google.inject.AbstractModule;
|
||||
import com.google.inject.Guice;
|
||||
import com.google.inject.Injector;
|
||||
@@ -33,6 +35,7 @@ public class GuiceWiringTests extends AbstractCompleteWiringTests {
|
||||
@Override
|
||||
protected void configure() {
|
||||
bind(Service.class).to(MyService.class);
|
||||
bind(Baz.class).in(Singleton.class);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -40,6 +40,10 @@ public class SpringModuleWiringTests extends AbstractCompleteWiringTests {
|
||||
public Service service() {
|
||||
return new MyService();
|
||||
}
|
||||
@Bean
|
||||
public Baz baz() {
|
||||
return new Baz(service());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user