diff --git a/src/main/java/org/springframework/guice/annotation/EnableGuiceModules.java b/src/main/java/org/springframework/guice/annotation/EnableGuiceModules.java index 4977da3..8366719 100644 --- a/src/main/java/org/springframework/guice/annotation/EnableGuiceModules.java +++ b/src/main/java/org/springframework/guice/annotation/EnableGuiceModules.java @@ -21,7 +21,12 @@ import java.lang.annotation.Target; import org.springframework.context.annotation.Import; +import com.google.inject.Module; + /** + * Enable spring beans that are themselves a Guice {@link Module} to contribute + * dependencies via the bindings in Guice. + * * @author Dave Syer * */ diff --git a/src/main/java/org/springframework/guice/annotation/GuiceFactoryBean.java b/src/main/java/org/springframework/guice/annotation/GuiceFactoryBean.java index 4ce5a78..9943192 100644 --- a/src/main/java/org/springframework/guice/annotation/GuiceFactoryBean.java +++ b/src/main/java/org/springframework/guice/annotation/GuiceFactoryBean.java @@ -1,30 +1,47 @@ +/* + * 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.annotation; import javax.inject.Provider; import org.springframework.beans.factory.FactoryBean; -public class GuiceFactoryBean implements FactoryBean { - private final Provider provider; - private final Class beanType; - - public GuiceFactoryBean(Class beanType, Provider provider) { - this.provider = provider; - this.beanType = beanType; - } +/** + * Convenience class used to map a Guice {@link Provider} to a Spring bean. + * + * @author Dave Syer + */ +class GuiceFactoryBean implements FactoryBean { + private final Provider provider; + private final Class beanType; - @Override - public T getObject() throws Exception { - return (T) provider.get(); - } + public GuiceFactoryBean(Class beanType, Provider provider) { + this.provider = provider; + this.beanType = beanType; + } - @Override - public Class getObjectType() { - return beanType; - } + @Override + public T getObject() throws Exception { + return (T) provider.get(); + } - @Override - public boolean isSingleton() { - return true; - } - } \ No newline at end of file + @Override + public Class getObjectType() { + return beanType; + } + + @Override + public boolean isSingleton() { + return true; + } +} \ No newline at end of file diff --git a/src/main/java/org/springframework/guice/annotation/GuiceModuleRegistrar.java b/src/main/java/org/springframework/guice/annotation/GuiceModuleRegistrar.java index 5ae7392..5a1ebd2 100644 --- a/src/main/java/org/springframework/guice/annotation/GuiceModuleRegistrar.java +++ b/src/main/java/org/springframework/guice/annotation/GuiceModuleRegistrar.java @@ -46,10 +46,12 @@ import org.springframework.guice.module.GuiceModuleMetadata; import org.springframework.util.Assert; /** + * Registers bean definitions for Guice modules. + * * @author Dave Syer * */ -public class GuiceModuleRegistrar implements ImportBeanDefinitionRegistrar, +class GuiceModuleRegistrar implements ImportBeanDefinitionRegistrar, ResourceLoaderAware { private ResourceLoader resourceLoader = new DefaultResourceLoader(); @@ -82,7 +84,7 @@ public class GuiceModuleRegistrar implements ImportBeanDefinitionRegistrar, registry.registerBeanDefinition(name, definition); } - public static class GuiceModuleMetadataFactory implements + protected static class GuiceModuleMetadataFactory implements FactoryBean { private Collection includeFilters; diff --git a/src/main/java/org/springframework/guice/injector/InjectorFactory.java b/src/main/java/org/springframework/guice/annotation/InjectorFactory.java similarity index 75% rename from src/main/java/org/springframework/guice/injector/InjectorFactory.java rename to src/main/java/org/springframework/guice/annotation/InjectorFactory.java index 4428e75..a1c4358 100644 --- a/src/main/java/org/springframework/guice/injector/InjectorFactory.java +++ b/src/main/java/org/springframework/guice/annotation/InjectorFactory.java @@ -1,9 +1,7 @@ -package org.springframework.guice.injector; +package org.springframework.guice.annotation; import java.util.List; -import org.springframework.guice.annotation.EnableGuiceModules; - import com.google.inject.Injector; import com.google.inject.Module; diff --git a/src/main/java/org/springframework/guice/annotation/ModuleRegistryConfiguration.java b/src/main/java/org/springframework/guice/annotation/ModuleRegistryConfiguration.java index dd3aa2b..6c216e3 100644 --- a/src/main/java/org/springframework/guice/annotation/ModuleRegistryConfiguration.java +++ b/src/main/java/org/springframework/guice/annotation/ModuleRegistryConfiguration.java @@ -33,7 +33,6 @@ import org.springframework.context.ApplicationContextException; import org.springframework.context.annotation.Configuration; import org.springframework.core.Ordered; import org.springframework.core.annotation.Order; -import org.springframework.guice.injector.InjectorFactory; import org.springframework.guice.module.SpringModule; import com.google.inject.Binding; @@ -45,7 +44,7 @@ import com.google.inject.name.Named; @Configuration @Order(Ordered.HIGHEST_PRECEDENCE + 10) -public class ModuleRegistryConfiguration implements BeanDefinitionRegistryPostProcessor, ApplicationContextAware { +class ModuleRegistryConfiguration implements BeanDefinitionRegistryPostProcessor, ApplicationContextAware { ApplicationContext applicationContext; diff --git a/src/main/java/org/springframework/guice/injector/CompositeAutowireCandidateResolver.java b/src/main/java/org/springframework/guice/injector/CompositeAutowireCandidateResolver.java deleted file mode 100644 index dacf55c..0000000 --- a/src/main/java/org/springframework/guice/injector/CompositeAutowireCandidateResolver.java +++ /dev/null @@ -1,71 +0,0 @@ -/* - * Copyright 2015 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.injector; - -import java.util.List; - -import org.springframework.beans.factory.config.BeanDefinitionHolder; -import org.springframework.beans.factory.config.DependencyDescriptor; -import org.springframework.beans.factory.support.AutowireCandidateResolver; - -/** - * @author Dave Syer - * - */ -public class CompositeAutowireCandidateResolver implements AutowireCandidateResolver { - - private List delegates; - - public CompositeAutowireCandidateResolver(List delegates) { - this.delegates = delegates; - } - - @Override - public boolean isAutowireCandidate(BeanDefinitionHolder bdHolder, - DependencyDescriptor descriptor) { - for (AutowireCandidateResolver delegate : this.delegates) { - if (delegate.isAutowireCandidate(bdHolder, descriptor)) { - return true; - } - } - return false; - } - - @Override - public Object getSuggestedValue(DependencyDescriptor descriptor) { - for (AutowireCandidateResolver delegate : this.delegates) { - Object value = delegate.getSuggestedValue(descriptor); - if (value!=null) { - return value; - } - } - return null; - } - - @Override - public Object getLazyResolutionProxyIfNecessary(DependencyDescriptor descriptor, - String beanName) { - for (AutowireCandidateResolver delegate : this.delegates) { - Object value = delegate.getLazyResolutionProxyIfNecessary(descriptor, beanName); - if (value!=null) { - return value; - } - } - return null; - } - -} diff --git a/src/main/java/org/springframework/guice/injector/SpringInjector.java b/src/main/java/org/springframework/guice/injector/SpringInjector.java index 0500f80..b79ba30 100644 --- a/src/main/java/org/springframework/guice/injector/SpringInjector.java +++ b/src/main/java/org/springframework/guice/injector/SpringInjector.java @@ -33,6 +33,14 @@ import com.google.inject.TypeLiteral; import com.google.inject.name.Named; import com.google.inject.spi.TypeConverterBinding; +/** + * An {@link Injector} that wraps an {@link ApplicationContext}, and can be used + * to expose the Guice APIs over a Spring application. Does not use Guice at all + * internally: just adapts the Spring API to the Guice one. + * + * @author Dave Syer + * + */ public class SpringInjector implements Injector { private Injector injector; @@ -40,7 +48,7 @@ public class SpringInjector implements Injector { public SpringInjector(ApplicationContext context) { this.beanFactory = (DefaultListableBeanFactory) context.getAutowireCapableBeanFactory(); - if (context.getBeanNamesForType(Injector.class, true, false).length>0) { + if (context.getBeanNamesForType(Injector.class, true, false).length > 0) { this.injector = context.getBean(Injector.class); } } @@ -97,11 +105,12 @@ public class SpringInjector implements Injector { @Override public Provider getProvider(Key key) { - // TODO: support for other metadata in the key (apart from name and type) + // TODO: support for other metadata in the key (apart from name and + // type) Class type = key.getTypeLiteral().getRawType(); final String name = extractName(key); - if (this.beanFactory.getBeanNamesForType(type, true, false).length==0) { - if (this.injector!=null) { + if (this.beanFactory.getBeanNamesForType(type, true, false).length == 0) { + if (this.injector != null) { return this.injector.getProvider(key); } // TODO: use prototype scope? diff --git a/src/main/java/org/springframework/guice/injector/GuiceAutowireCandidateResolver.java b/src/main/java/org/springframework/guice/module/GuiceAutowireCandidateResolver.java similarity index 96% rename from src/main/java/org/springframework/guice/injector/GuiceAutowireCandidateResolver.java rename to src/main/java/org/springframework/guice/module/GuiceAutowireCandidateResolver.java index 38ce73e..778b35d 100644 --- a/src/main/java/org/springframework/guice/injector/GuiceAutowireCandidateResolver.java +++ b/src/main/java/org/springframework/guice/module/GuiceAutowireCandidateResolver.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.springframework.guice.injector; +package org.springframework.guice.module; import javax.inject.Provider; @@ -32,7 +32,7 @@ import com.google.inject.Injector; * @author Taylor Wicksell * */ -public class GuiceAutowireCandidateResolver extends ContextAnnotationAutowireCandidateResolver { +class GuiceAutowireCandidateResolver extends ContextAnnotationAutowireCandidateResolver { private Provider injectorProvider; diff --git a/src/main/java/org/springframework/guice/module/SpringModule.java b/src/main/java/org/springframework/guice/module/SpringModule.java index 0aefd82..736b605 100644 --- a/src/main/java/org/springframework/guice/module/SpringModule.java +++ b/src/main/java/org/springframework/guice/module/SpringModule.java @@ -39,7 +39,6 @@ import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; import org.springframework.beans.factory.support.AbstractBeanDefinition; import org.springframework.beans.factory.support.DefaultListableBeanFactory; import org.springframework.context.ApplicationContext; -import org.springframework.guice.injector.GuiceAutowireCandidateResolver; import org.springframework.util.ClassUtils; /** diff --git a/src/test/java/org/springframework/guice/InjectorFactoryTests.java b/src/test/java/org/springframework/guice/InjectorFactoryTests.java index ef9b29a..ca7ba2c 100644 --- a/src/test/java/org/springframework/guice/InjectorFactoryTests.java +++ b/src/test/java/org/springframework/guice/InjectorFactoryTests.java @@ -7,8 +7,8 @@ import org.springframework.context.ApplicationContextException; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; -import org.springframework.guice.annotation.ModuleRegistryConfiguration; -import org.springframework.guice.injector.InjectorFactory; +import org.springframework.guice.annotation.EnableGuiceModules; +import org.springframework.guice.annotation.InjectorFactory; import com.google.inject.Guice; import com.google.inject.Module; @@ -26,7 +26,7 @@ public class InjectorFactoryTests { @Test public void testCustomInjectorIsCreated() { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(InjectorFactoryConfig.class, - ModuleRegistryConfiguration.class); + ModulesConfig.class); Mockito.verify(injectorFactory, Mockito.times(1)).createInjector(Mockito.anyListOf(Module.class)); context.close(); } @@ -34,10 +34,15 @@ public class InjectorFactoryTests { @Test(expected = ApplicationContextException.class) public void testMultipleInjectorFactoriesThrowsApplicationContextException() { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(InjectorFactoryConfig.class, - SecondInjectorFactoryConfig.class, ModuleRegistryConfiguration.class); + SecondInjectorFactoryConfig.class, ModulesConfig.class); context.close(); } + @Configuration + @EnableGuiceModules + static class ModulesConfig { + } + @Configuration static class InjectorFactoryConfig { @Bean diff --git a/src/test/java/org/springframework/guice/SimpleWiringTests.java b/src/test/java/org/springframework/guice/SimpleWiringTests.java index 6cb200c..92a72b3 100644 --- a/src/test/java/org/springframework/guice/SimpleWiringTests.java +++ b/src/test/java/org/springframework/guice/SimpleWiringTests.java @@ -7,7 +7,8 @@ import javax.inject.Inject; import org.junit.Test; import org.springframework.beans.factory.support.RootBeanDefinition; import org.springframework.context.annotation.AnnotationConfigApplicationContext; -import org.springframework.guice.annotation.ModuleRegistryConfiguration; +import org.springframework.context.annotation.Configuration; +import org.springframework.guice.annotation.EnableGuiceModules; import org.springframework.guice.injector.SpringInjector; import com.google.inject.AbstractModule; @@ -32,11 +33,16 @@ public class SimpleWiringTests { @Test public void hybridFoo() { - AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(TestConfig.class, ModuleRegistryConfiguration.class); + AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(TestConfig.class, ModulesConfig.class); Injector app = new SpringInjector(context); assertNotNull(app.getInstance(Foo.class)); } + @Configuration + @EnableGuiceModules + static class ModulesConfig { + } + public static class TestConfig extends AbstractModule { @Override protected void configure() {