Tidy up package structure a bit

This commit is contained in:
Dave Syer
2017-06-04 11:23:02 +01:00
parent b810a80ae7
commit 63c8c2e47f
11 changed files with 81 additions and 112 deletions

View File

@@ -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
*
*/

View File

@@ -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<T> implements FactoryBean<T> {
private final Provider<T> provider;
private final Class<T> beanType;
public GuiceFactoryBean(Class<T> beanType, Provider<T> 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<T> implements FactoryBean<T> {
private final Provider<T> provider;
private final Class<T> beanType;
@Override
public T getObject() throws Exception {
return (T) provider.get();
}
public GuiceFactoryBean(Class<T> beanType, Provider<T> 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;
}
}
@Override
public Class<?> getObjectType() {
return beanType;
}
@Override
public boolean isSingleton() {
return true;
}
}

View File

@@ -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<GuiceModuleMetadata> {
private Collection<? extends TypeFilter> includeFilters;

View File

@@ -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;

View File

@@ -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;

View File

@@ -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<AutowireCandidateResolver> delegates;
public CompositeAutowireCandidateResolver(List<AutowireCandidateResolver> 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;
}
}

View File

@@ -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 <T> Provider<T> getProvider(Key<T> 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<? super T> 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?

View File

@@ -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<Injector> injectorProvider;

View File

@@ -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;
/**

View File

@@ -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

View File

@@ -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() {