Initial experiments

This commit is contained in:
Dave Syer
2014-04-14 17:33:48 +01:00
commit 77e968bccd
11 changed files with 535 additions and 0 deletions

View File

@@ -0,0 +1,47 @@
package demo;
import static org.junit.Assert.assertNotNull;
import javax.inject.Inject;
import org.junit.Before;
import org.junit.Test;
import com.google.inject.Injector;
public abstract class AbstractCompleteWiringTests {
private Injector injector;
@Before
public void init() {
injector = createInjector();
}
protected abstract Injector createInjector();
@Test
public void getInstanceUnbound() {
assertNotNull(injector.getInstance(Foo.class));
}
@Test
public void getInstanceBound() {
assertNotNull(injector.getInstance(Service.class));
}
interface Service {
}
protected static class MyService implements Service {
}
public static class Foo {
@Inject
public Foo(Service service) {
}
}
}

View File

@@ -0,0 +1,39 @@
/*
* 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 com.google.inject.AbstractModule;
import com.google.inject.Guice;
import com.google.inject.Injector;
/**
* @author Dave Syer
*
*/
public class GuiceWiringTests extends AbstractCompleteWiringTests {
@Override
protected Injector createInjector() {
Injector app = Guice.createInjector(new TestConfig());
return app;
}
public static class TestConfig extends AbstractModule {
@Override
protected void configure() {
bind(Service.class).to(MyService.class);
}
}
}

View File

@@ -0,0 +1,42 @@
/*
* 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 com.google.inject.AbstractModule;
import com.google.inject.Injector;
/**
* @author Dave Syer
*
*/
public class HybridWiringTests extends AbstractCompleteWiringTests {
@Override
protected Injector createInjector() {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.register(TestConfig.class, ModuleRegistryConfiguration.class);
context.refresh();
return new SpringInjector(context);
}
public static class TestConfig extends AbstractModule {
@Override
protected void configure() {
bind(Service.class).to(MyService.class);
}
}
}

View File

@@ -0,0 +1,69 @@
/*
* 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 java.util.Collections;
import java.util.List;
import java.util.Map.Entry;
import javax.annotation.PostConstruct;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.google.inject.Binding;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.Key;
import com.google.inject.Module;
import com.google.inject.Provider;
@Configuration
public class ModuleRegistryConfiguration {
@Autowired
private ConfigurableListableBeanFactory beanFactory;
@Autowired(required=false)
private List<Module> modules = Collections.emptyList();
private Injector injector;
@Bean
public Injector injector() {
return injector;
}
@PostConstruct
public void init() {
injector = Guice.createInjector(modules);
for (Entry<Key<?>, Binding<?>> entry : injector.getBindings().entrySet()) {
if (entry.getKey().getTypeLiteral().getRawType().equals(Injector.class)) {
continue;
}
final Provider<?> provider = entry.getValue().getProvider();
beanFactory.registerResolvableDependency(entry.getKey().getTypeLiteral().getRawType(), new ObjectFactory<Object>() {
@Override
public Object getObject() throws BeansException {
return provider.get();
}
});
}
}
}

View File

@@ -0,0 +1,59 @@
package demo;
import static org.junit.Assert.assertNotNull;
import javax.inject.Inject;
import org.junit.Test;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import com.google.inject.AbstractModule;
import com.google.inject.Guice;
import com.google.inject.Injector;
public class SimpleWiringTests {
@Test
public void guiceyFoo() {
Injector app = Guice.createInjector(new TestConfig());
assertNotNull(app.getInstance(Foo.class));
}
@Test
public void springyFoo() {
@SuppressWarnings("resource")
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(TestConfig.class, MyService.class);
context.getDefaultListableBeanFactory().registerBeanDefinition(Foo.class.getSimpleName(), new RootBeanDefinition(Foo.class));
assertNotNull(context.getBean(Foo.class));
}
@Test
public void hybridFoo() {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(TestConfig.class, ModuleRegistryConfiguration.class);
Injector app = new SpringInjector(context);
assertNotNull(app.getInstance(Foo.class));
}
protected static class TestConfig extends AbstractModule {
@Override
protected void configure() {
bind(Service.class).to(MyService.class);
}
}
interface Service {
}
protected static class MyService implements Service {
}
protected static class Foo {
@Inject
public Foo(Service service) {
}
}
}

View File

@@ -0,0 +1,143 @@
/*
* 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 java.lang.annotation.Annotation;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.context.support.GenericApplicationContext;
import com.google.inject.Binding;
import com.google.inject.Injector;
import com.google.inject.Key;
import com.google.inject.MembersInjector;
import com.google.inject.Module;
import com.google.inject.Provider;
import com.google.inject.Scope;
import com.google.inject.TypeLiteral;
import com.google.inject.spi.TypeConverterBinding;
public class SpringInjector implements Injector {
private GenericApplicationContext context;
private Injector injector;
public SpringInjector(GenericApplicationContext context) {
this.context = context;
if (context.getBeanNamesForType(Injector.class).length>0) {
injector = context.getBean(Injector.class);
}
}
@Override
public void injectMembers(Object instance) {
}
@Override
public <T> MembersInjector<T> getMembersInjector(TypeLiteral<T> typeLiteral) {
return null;
}
@Override
public <T> MembersInjector<T> getMembersInjector(Class<T> type) {
return null;
}
@Override
public Map<Key<?>, Binding<?>> getBindings() {
return null;
}
@Override
public Map<Key<?>, Binding<?>> getAllBindings() {
return null;
}
@Override
public <T> Binding<T> getBinding(Key<T> key) {
return null;
}
@Override
public <T> Binding<T> getBinding(Class<T> type) {
return null;
}
@Override
public <T> Binding<T> getExistingBinding(Key<T> key) {
return null;
}
@Override
public <T> List<Binding<T>> findBindingsByType(TypeLiteral<T> type) {
return null;
}
@Override
public <T> Provider<T> getProvider(Key<T> key) {
return null;
}
@Override
public <T> Provider<T> getProvider(Class<T> type) {
return null;
}
@Override
public <T> T getInstance(Key<T> key) {
return null;
}
@Override
public <T> T getInstance(Class<T> type) {
if (context.getBeanNamesForType(type).length==0) {
if (injector!=null && injector.getExistingBinding(Key.get(type))!=null) {
return injector.getInstance(type);
}
// TODO: use prototype scope?
context.getDefaultListableBeanFactory().registerBeanDefinition(type.getSimpleName(), new RootBeanDefinition(type));
}
return context.getBean(type);
}
@Override
public Injector getParent() {
return null;
}
@Override
public Injector createChildInjector(Iterable<? extends Module> modules) {
return null;
}
@Override
public Injector createChildInjector(Module... modules) {
return null;
}
@Override
public Map<Class<? extends Annotation>, Scope> getScopeBindings() {
return null;
}
@Override
public Set<TypeConverterBinding> getTypeConverterBindings() {
return null;
}
}

View File

@@ -0,0 +1,44 @@
/*
* 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.Injector;
/**
* @author Dave Syer
*
*/
public class SpringWiringTests extends AbstractCompleteWiringTests {
@Override
protected Injector createInjector() {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.register(TestConfig.class);
context.refresh();
return new SpringInjector(context);
}
@Configuration
public static class TestConfig {
@Bean
public Service service() {
return new MyService();
}
}
}