Add support for bean names

Users can in inject Spring beans by name in Guice using
@Named, or they can inject Guice beans in Spring as long
as they have @Named providers.

Fixes gh-7
This commit is contained in:
Dave Syer
2016-06-02 10:23:14 +01:00
parent 72cf5256e8
commit 1002284360
14 changed files with 432 additions and 79 deletions

View File

@@ -4,11 +4,14 @@ import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import javax.inject.Inject;
import javax.inject.Named;
import org.junit.Before;
import org.junit.Test;
import com.google.inject.Injector;
import com.google.inject.Key;
import com.google.inject.name.Names;
public abstract class AbstractCompleteWiringTests {
@@ -16,7 +19,7 @@ public abstract class AbstractCompleteWiringTests {
@Before
public void init() {
injector = createInjector();
this.injector = createInjector();
}
protected abstract Injector createInjector();
@@ -24,42 +27,52 @@ public abstract class AbstractCompleteWiringTests {
@Test
public void injectInstance() {
Bar bar = new Bar();
injector.injectMembers(bar);
this.injector.injectMembers(bar);
assertNotNull(bar.service);
}
@Test
public void memberInjector() {
Bar bar = new Bar();
injector.getMembersInjector(Bar.class).injectMembers(bar);
this.injector.getMembersInjector(Bar.class).injectMembers(bar);
assertNotNull(bar.service);
}
@Test
public void getInstanceUnbound() {
assertNotNull(injector.getInstance(Foo.class));
assertNotNull(this.injector.getInstance(Foo.class));
}
@Test
public void getInstanceBound() {
assertNotNull(injector.getInstance(Service.class));
assertNotNull(this.injector.getInstance(Service.class));
}
@Test
public void getInstanceBoundWithNoInterface() {
Baz instance = injector.getInstance(Baz.class);
Baz instance = this.injector.getInstance(Baz.class);
assertNotNull(instance);
assertEquals(instance, injector.getInstance(Baz.class));
assertEquals(instance, this.injector.getInstance(Baz.class));
}
@Test
public void getProviderUnbound() {
assertNotNull(injector.getProvider(Foo.class).get());
assertNotNull(this.injector.getProvider(Foo.class).get());
}
@Test
public void getProviderBound() {
assertNotNull(injector.getProvider(Service.class).get());
assertNotNull(this.injector.getProvider(Service.class).get());
}
@Test
public void getNamedInstance() {
assertNotNull(this.injector.getInstance(Key.get(Thang.class, Names.named("thing"))));
}
@Test
public void getNamedInjectedInstance() {
assertNotNull(this.injector.getInstance(Thing.class).thang);
}
public interface Service {
@@ -95,4 +108,17 @@ public abstract class AbstractCompleteWiringTests {
}
public static class Thing {
private Thang thang;
@Inject
public void setThang(@Named("thing") Thang thang) {
this.thang = thang;
}
}
public static class Thang {
}
}

View File

@@ -18,6 +18,7 @@ import javax.inject.Singleton;
import com.google.inject.AbstractModule;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.name.Names;
/**
* @author Dave Syer
@@ -36,7 +37,8 @@ public class GuiceWiringTests extends AbstractCompleteWiringTests {
protected void configure() {
bind(Service.class).to(MyService.class);
bind(Baz.class).in(Singleton.class);
bind(Thang.class).annotatedWith(Names.named("thing")).to(Thang.class);
}
}
}

View File

@@ -0,0 +1,54 @@
/*
* 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;
import static org.junit.Assert.assertNotNull;
import javax.inject.Inject;
import org.junit.Test;
import com.google.inject.AbstractModule;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.name.Names;
/**
* @author Dave Syer
*
*/
public class NativeGuiceTests {
@Inject
private Foo bar;
@Test
public void test() {
Injector app = Guice.createInjector(new TestConfig());
NativeGuiceTests instance = app.getInstance(NativeGuiceTests.class);
assertNotNull(instance.bar);
}
public static class TestConfig extends AbstractModule {
@Override
protected void configure() {
bind(Foo.class).annotatedWith(Names.named("bar")).to(Foo.class);
}
}
public static class Foo {}
}

View File

@@ -15,6 +15,10 @@ package org.springframework.guice.annotation;
import static org.junit.Assert.assertNotNull;
import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
@@ -25,6 +29,7 @@ import org.springframework.guice.injector.SpringInjector;
import com.google.inject.AbstractModule;
import com.google.inject.Injector;
import com.google.inject.Provides;
/**
* @author Dave Syer
@@ -36,23 +41,23 @@ public class ModuleBeanWiringTests extends AbstractCompleteWiringTests {
@Override
protected Injector createInjector() {
context = new AnnotationConfigApplicationContext();
context.register(TestConfig.class);
context.refresh();
return new SpringInjector(context);
this.context = new AnnotationConfigApplicationContext();
this.context.register(TestConfig.class);
this.context.refresh();
return new SpringInjector(this.context);
}
@Test
public void bindToSpringBeanFromGuiceModule() throws Exception {
assertNotNull(context.getBean(Spam.class));
assertNotNull(this.context.getBean(Spam.class));
}
@EnableGuiceModules
@Configuration
public static class TestConfig extends AbstractModule {
@Autowired Service service;
@Override
protected void configure() {
bind(Service.class).to(MyService.class);
@@ -62,7 +67,20 @@ public class ModuleBeanWiringTests extends AbstractCompleteWiringTests {
public Spam spam(Service service) {
return new Spam(service);
}
}
@Provides
@Named("thing")
public Thang thing() {
return new Thang();
}
@Provides
@Inject
@Singleton
public Baz baz(Service service) {
return new Baz(service);
}
}
protected static class Spam {
public Spam(Service service) {

View File

@@ -0,0 +1,96 @@
/*
* 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 static org.junit.Assert.assertNotNull;
import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.guice.AbstractCompleteWiringTests;
import org.springframework.guice.injector.SpringInjector;
import com.google.inject.AbstractModule;
import com.google.inject.Injector;
import com.google.inject.Provides;
/**
* @author Dave Syer
*
*/
public class ModuleNamedBeanWiringTests extends AbstractCompleteWiringTests {
private AnnotationConfigApplicationContext context;
@Override
protected Injector createInjector() {
this.context = new AnnotationConfigApplicationContext();
this.context.register(TestConfig.class);
this.context.refresh();
return new SpringInjector(this.context);
}
@Test
public void bindToSpringBeanFromGuiceModule() throws Exception {
assertNotNull(this.context.getBean(Spam.class));
}
@EnableGuiceModules
@Configuration
public static class TestConfig extends AbstractModule {
@Autowired Service service;
@Override
protected void configure() {
bind(Service.class).to(MyService.class);
}
@Bean
public Spam spam(Service service) {
return new Spam(service);
}
@Provides
@Named("thing")
public Thang thing() {
return new Thang();
}
@Provides
@Named("other")
public Thang other() {
return new Thang();
}
@Provides
@Inject
@Singleton
public Baz baz(Service service) {
return new Baz(service);
}
}
protected static class Spam {
public Spam(Service service) {
}
}
}

View File

@@ -40,6 +40,14 @@ public class SpringWiringTests extends AbstractCompleteWiringTests {
public Service service() {
return new MyService();
}
@Bean
public Thang thing() {
return new Thang();
}
@Bean
public Thang other() {
return new Thang();
}
}
}

View File

@@ -51,6 +51,17 @@ public class SpringModuleWiringTests extends AbstractCompleteWiringTests {
public Baz baz() {
return new Baz(service());
}
@Bean
public Thang thing() {
return new Thang();
}
@Bean
public Thing that() {
return new Thing();
}
}
}