Ignore Collection types from GuiceAutowireCandidateResolver
This commit is contained in:
committed by
Taylor Wicksell
parent
697d2ede55
commit
0bc99b690a
2
pom.xml
2
pom.xml
@@ -57,7 +57,7 @@
|
||||
<java.version>1.8</java.version>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
|
||||
<spring.version>5.0.3.RELEASE</spring.version>
|
||||
<spring.version>5.1.4.RELEASE</spring.version>
|
||||
</properties>
|
||||
|
||||
<build>
|
||||
|
||||
@@ -15,8 +15,7 @@
|
||||
*/
|
||||
package org.springframework.guice.module;
|
||||
|
||||
import javax.inject.Provider;
|
||||
|
||||
import com.google.inject.Injector;
|
||||
import com.google.inject.Key;
|
||||
import org.springframework.aop.TargetSource;
|
||||
import org.springframework.aop.framework.ProxyFactory;
|
||||
@@ -26,7 +25,11 @@ import org.springframework.beans.factory.support.DefaultListableBeanFactory;
|
||||
import org.springframework.context.annotation.ContextAnnotationAutowireCandidateResolver;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
import com.google.inject.Injector;
|
||||
|
||||
import javax.inject.Provider;
|
||||
import java.util.Collection;
|
||||
import java.util.Map;
|
||||
|
||||
|
||||
/**
|
||||
* @author Dave Syer
|
||||
@@ -35,7 +38,7 @@ import com.google.inject.Injector;
|
||||
*
|
||||
*/
|
||||
class GuiceAutowireCandidateResolver extends ContextAnnotationAutowireCandidateResolver {
|
||||
|
||||
|
||||
private Provider<Injector> injectorProvider;
|
||||
|
||||
public GuiceAutowireCandidateResolver(Provider<Injector> injectorProvider) {
|
||||
@@ -51,6 +54,11 @@ class GuiceAutowireCandidateResolver extends ContextAnnotationAutowireCandidateR
|
||||
Assert.state(getBeanFactory() instanceof DefaultListableBeanFactory,
|
||||
"BeanFactory needs to be a DefaultListableBeanFactory");
|
||||
final DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) getBeanFactory();
|
||||
|
||||
if (isCollectionType(descriptor.getDependencyType())) {
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
beanFactory.doResolveDependency(descriptor, beanName, null, null);
|
||||
} catch (NoSuchBeanDefinitionException e) {
|
||||
@@ -82,6 +90,7 @@ class GuiceAutowireCandidateResolver extends ContextAnnotationAutowireCandidateR
|
||||
} catch (NoSuchBeanDefinitionException e) {
|
||||
target = injectorProvider.get().getInstance(Key.get(descriptor.getResolvableType().getType()));
|
||||
}
|
||||
|
||||
if (target == null) {
|
||||
throw new NoSuchBeanDefinitionException(descriptor.getDependencyType(),
|
||||
"Optional dependency not present for lazy injection point");
|
||||
@@ -102,4 +111,7 @@ class GuiceAutowireCandidateResolver extends ContextAnnotationAutowireCandidateR
|
||||
return pf.getProxy(beanFactory.getBeanClassLoader());
|
||||
}
|
||||
|
||||
private boolean isCollectionType(Class<?> type) {
|
||||
return Collection.class.isAssignableFrom(type) || Map.class == type;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,75 @@
|
||||
package org.springframework.guice;
|
||||
|
||||
import com.google.inject.AbstractModule;
|
||||
import com.google.inject.Injector;
|
||||
import org.junit.Test;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.guice.annotation.EnableGuiceModules;
|
||||
import org.springframework.guice.injector.SpringInjector;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class SpringAutowiredCollectionTests {
|
||||
|
||||
@Test
|
||||
public void getAutowiredCollection() {
|
||||
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
|
||||
context.register(TestConfig.class);
|
||||
context.refresh();
|
||||
Injector injector = new SpringInjector(context);
|
||||
|
||||
ServicesHolder servicesHolder = injector.getInstance(ServicesHolder.class);
|
||||
|
||||
assertEquals(2, servicesHolder.existingServices.size());
|
||||
assertEquals(0, servicesHolder.nonExistingServices.size());
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@EnableGuiceModules
|
||||
static class TestConfig {
|
||||
@Bean
|
||||
public ServicesHolder serviceHolder(Map<String, Service> existingServices,
|
||||
Map<String, NonExistingService> nonExistingServices) {
|
||||
return new ServicesHolder(existingServices, nonExistingServices);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Service service() {
|
||||
return new Service();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public GuiceModule guiceServiceModule() {
|
||||
return new GuiceModule();
|
||||
}
|
||||
}
|
||||
|
||||
static class Service {
|
||||
}
|
||||
|
||||
static class NonExistingService {
|
||||
}
|
||||
|
||||
static class GuiceModule extends AbstractModule {
|
||||
|
||||
@Override
|
||||
protected void configure() {
|
||||
bind(Service.class).asEagerSingleton();
|
||||
}
|
||||
}
|
||||
|
||||
static class ServicesHolder {
|
||||
final Map<String, Service> existingServices;
|
||||
final Map<String, NonExistingService> nonExistingServices;
|
||||
|
||||
public ServicesHolder(Map<String, Service> existingServices,
|
||||
Map<String, NonExistingService> nonExistingServices) {
|
||||
this.existingServices = existingServices;
|
||||
this.nonExistingServices = nonExistingServices;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user