public

How to bind multiple Guice singletons of the same type under different binding annotations

  • Download Gist
MultipleGuiceSingletonsTest.java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
/**
* Copyright (C) 2011 Etienne Neveu
*
* 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 com.headexplodes.example.guice;
 
import java.lang.annotation.Retention;
 
import javax.inject.Singleton;
 
import com.google.inject.AbstractModule;
import com.google.inject.Binder;
import com.google.inject.BindingAnnotation;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.Key;
import com.google.inject.Scopes;
import org.testng.annotations.Test;
 
import static java.lang.annotation.RetentionPolicy.RUNTIME;
import static org.testng.Assert.assertNotSame;
import static org.testng.Assert.assertSame;
 
/**
* Demo for
* http://stackoverflow.com/questions/1221920/getting-multiple-guice-singletons-of-the-same-type/1407719#1407719
* <p/>
* This demonstrates that you can bind multiple singletons of the same type under different
* binding annotations.
* <p/>
* As explained in the
* <a href="http://code.google.com/p/google-guice/wiki/Scopes">Guice Documentation</a>:
* <pre>
* In linked bindings, scopes apply to the binding source, not the binding target. Suppose
* we have a class Applebees that implements both Bar and Grill interfaces. These bindings
* allow for two instances of that type, one for Bars and another for Grills:
*
* bind(Bar.class).to(Applebees.class).in(Singleton.class);
* bind(Grill.class).to(Applebees.class).in(Singleton.class);
*
* This is because the scopes apply to the bound type (Bar, Grill), not the type that
* satisfies that binding (Applebees). To allow only a single instance to be created,
* use a @Singleton annotation on the declaration for that class. Or add another binding:
*
* bind(Applebees.class).in(Singleton.class);
*
* This binding makes the other two .in(Singleton.class) clauses above unnecessary.
* </pre>
*/
public class MultipleGuiceSingletonsTest {
 
@Retention(RUNTIME)
@BindingAnnotation
@interface One {}
 
@Retention(RUNTIME)
@BindingAnnotation
@interface Two {}
 
static class MySingleton {}
 
@Singleton
static class MyAnnotatedSingleton {}
 
/**
* {@code toInstance()} bindings obviously return different instances. But they have
* drawbacks.
* As explained in the {@link Binder} javadoc, they result in "eager loading" behavior
* that you can't control.
* Moreover, you can't use constructor injection for {@code toInstance()} bindings.
*/
@Test
public void testToInstanceBinding() {
Injector injector = Guice.createInjector(new AbstractModule() {
protected void configure() {
bind(MySingleton.class).annotatedWith(One.class)
.toInstance(new MySingleton());
bind(MySingleton.class).annotatedWith(Two.class)
.toInstance(new MySingleton());
}
});
 
assertNotSame(
injector.getInstance(Key.get(MySingleton.class, One.class)),
injector.getInstance(Key.get(MySingleton.class, Two.class))
);
}
 
/**
* As explained in this test's javadoc, scopes apply to the bound type, not the type
* that satisfies the binding.
* In this case, the bound types are {@code [MySingleton, @One]} and
* {@code [MySingleton, @Two]}, and Guice constructs two instances.
*/
@Test
public void testAnnotatedSingletonBinding_multipleInstances() {
Injector injector = Guice.createInjector(new AbstractModule() {
protected void configure() {
bind(MySingleton.class).annotatedWith(One.class)
.to(MySingleton.class).in(Singleton.class);
bind(MySingleton.class).annotatedWith(Two.class)
.to(MySingleton.class).in(Singleton.class);
}
});
 
assertNotSame(
injector.getInstance(Key.get(MySingleton.class, One.class)),
injector.getInstance(Key.get(MySingleton.class, Two.class))
);
}
 
/**
* We can force the creation of a single instance by adding a singleton binding.
*/
@Test
public void testAnnotatedSingletonBinding_singleInstance_singletonBinding() {
Injector injector = Guice.createInjector(new AbstractModule() {
protected void configure() {
bind(MySingleton.class).in(Scopes.SINGLETON);
bind(MySingleton.class).annotatedWith(One.class)
.to(MySingleton.class).in(Singleton.class);
bind(MySingleton.class).annotatedWith(Two.class)
.to(MySingleton.class).in(Singleton.class);
}
});
 
assertSame(
injector.getInstance(Key.get(MySingleton.class, One.class)),
injector.getInstance(Key.get(MySingleton.class, Two.class))
);
}
 
/**
* We can also force the creation of a single instance by annotating the bound class
* with {@code @Singleton}.
* <p/>
* <b>Notice that we use {@link MyAnnotatedSingleton} instead of {@link MySingleton}
* in this test.</b>
*/
@Test
public void testAnnotatedSingletonBinding_singleInstance_annotatedSingleton() {
Injector injector = Guice.createInjector(new AbstractModule() {
protected void configure() {
bind(MyAnnotatedSingleton.class).annotatedWith(One.class)
.to(MyAnnotatedSingleton.class).in(Singleton.class);
bind(MyAnnotatedSingleton.class).annotatedWith(Two.class)
.to(MyAnnotatedSingleton.class).in(Singleton.class);
}
});
 
assertSame(
injector.getInstance(Key.get(MyAnnotatedSingleton.class, One.class)),
injector.getInstance(Key.get(MyAnnotatedSingleton.class, Two.class))
);
}
 
/**
* We can still obtain multiple singletons when the bound class is annotated with
* {@code @Singleton}. To achieve this, we override {@code @Singleton} annotation
* by specifying the {@link Scopes#NO_SCOPE} in the Guice module.
* <p/>
* <b>Notice that we use {@link MyAnnotatedSingleton} instead of {@link MySingleton}
* in this test.</b>
*/
@Test
public void testAnnotatedSingletonBinding_multipleInstances_annotatedSingletonOverriddenInModule() {
Injector injector = Guice.createInjector(new AbstractModule() {
protected void configure() {
// Override the @Singleton annotation to force Guice to create two singletons
bind(MyAnnotatedSingleton.class).in(Scopes.NO_SCOPE);
bind(MyAnnotatedSingleton.class).annotatedWith(One.class)
.to(MyAnnotatedSingleton.class).in(Singleton.class);
bind(MyAnnotatedSingleton.class).annotatedWith(Two.class)
.to(MyAnnotatedSingleton.class).in(Singleton.class);
}
});
 
assertNotSame(
injector.getInstance(Key.get(MyAnnotatedSingleton.class, One.class)),
injector.getInstance(Key.get(MyAnnotatedSingleton.class, Two.class))
);
}
 
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.