下面列出了怎么用com.google.inject.binder.ScopedBindingBuilder的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* Adds a Binder for Map<String, T> to anything in the context
* that keyed to a type that implements T. Note that this has to be a literal class
* because runtime reflection can't handle generics.
* @param binder
* @param type
* @return
*/
@SuppressWarnings("unchecked")
public static <T> ScopedBindingBuilder bindMapToInstancesOf(Binder binder, final Class<T> type) {
return bindMapToInstancesOf(
binder,
(TypeLiteral<Map<String, T>>) TypeLiteral.get(new ParameterizedType() {
final Type [] args = new Type[] { String.class, type };
@Override
public Type getRawType() {
return Map.class;
}
@Override
public Type getOwnerType() {
return null;
}
@Override
public Type[] getActualTypeArguments() {
return args;
}
})
);
}
@SuppressWarnings("unchecked")
public static <T> ScopedBindingBuilder bindListToInstancesOf(Binder binder, final Class<T> type, Comparator<T> comparator) {
return bindListToInstancesOf(
binder,
(TypeLiteral<List<T>>) TypeLiteral.get(new ParameterizedType() {
final Type [] args = new Type[] { type };
@Override
public Type getRawType() {
return List.class;
}
@Override
public Type getOwnerType() {
return null;
}
@Override
public Type[] getActualTypeArguments() {
return args;
}
}),
comparator
);
}
public Module bindingModule() {
return new KeyedManifest.Impl(method) {
@Override
public void configure() {
final Binder binder = binder().withSource(method);
if(!hasReturnValue()) {
binder.addError("Cannot bind this method as a provider because it does not return a value");
return;
}
install(InjectableMethod.this);
final ScopedBindingBuilder builder = binder.bind(providedKey).toProvider(asProvider());
if(scope != null) builder.in(scope);
}
};
}
@SuppressWarnings({"unchecked", "rawtypes"})
@Before
public void before() {
PrivateBinder binder = mock(PrivateBinder.class);
AnnotatedBindingBuilder ab = mock(AnnotatedBindingBuilder.class);
when(binder.bind(any(Class.class))).thenReturn(ab);
when(ab.annotatedWith(any(Annotation.class))).thenReturn(ab);
AnnotatedElementBuilder aeb = mock(AnnotatedElementBuilder.class);
when(binder.expose(any(Class.class))).thenReturn(aeb);
ScopedBindingBuilder sb = mock(ScopedBindingBuilder.class);
when(ab.toProvider(any(Provider.class))).thenReturn(sb);
when(binder.bind(any(TypeLiteral.class))).thenReturn(ab);
when(binder.skipSources(any(Class.class), any(Class.class))).thenReturn(binder);
when(binder.skipSources(any(Class.class))).thenReturn(binder);
securityConfigurer = mock(SecurityConfigurer.class);
underTest = new SecurityInternalModule(securityConfigurer, new HashMap<>());
Whitebox.setInternalState(underTest, "binder", binder);
}
public static <T> ScopedBindingBuilder bindMapToInstancesOf(Binder binder, TypeLiteral<Map<String, T>> type) {
return bindMapToInstancesOf(binder, type, new Function<T,String>() {
@Override
public String apply(T service) {
return Injectors.getServiceName(service);
}
});
}
@Override
public void configure(Binder binder)
{
final ScopedBindingBuilder builder = binder.bind(objectMapperKey)
.toProvider(new ObjectMapperProvider(modulesToInject, modulesToAdd, objectMapper));
if (scope != null) {
builder.in(scope);
}
}
public static <K, V> ScopedBindingBuilder bindMapToInstancesOf(Binder binder, TypeLiteral<Map<K, V>> type, Function<V, K> keyFunction) {
Class<V> containedType = extractContainedMapType(type);
return binder
.bind(type)
.toProvider(new MapProvider<K, V>(containedType, keyFunction));
}
public static <T> ScopedBindingBuilder bindListToInstancesOf(Binder binder, Class<T> type) {
return bindListToInstancesOf(binder, type, null);
}
public static <T> ScopedBindingBuilder bindListToInstancesOf(Binder binder, TypeLiteral<List<T>> type) {
return bindListToInstancesOf(binder, type, null);
}
public static <T> ScopedBindingBuilder bindListToInstancesOf(Binder binder, TypeLiteral<List<T>> type, Comparator<T> comparator) {
Class<T> containedType = extractContainedListType(type);
return binder
.bind(type)
.toProvider(new ListProvider<T>(containedType, comparator));
}
protected <T> ScopedBindingBuilder bindMapToInstancesOf(Class<T> containedType) {
return Binders.bindMapToInstancesOf(this.binder(), containedType);
}
protected <T> ScopedBindingBuilder bindMapToInstancesOf(TypeLiteral<Map<String, T>> type) {
return Binders.bindMapToInstancesOf(this.binder(), type);
}
protected <K, V> ScopedBindingBuilder bindMapToInstancesOf(TypeLiteral<Map<K, V>> type, Function<V, K> keyFunction) {
return Binders.bindMapToInstancesOf(binder(), type, keyFunction);
}
protected <T> ScopedBindingBuilder bindListToInstancesOf(Class<T> containedType) {
return Binders.bindListToInstancesOf(this.binder(), containedType);
}
protected <T> ScopedBindingBuilder bindListToInstancesOf(Class<T> containedType, Comparator<T> comparator) {
return Binders.bindListToInstancesOf(this.binder(), containedType, comparator);
}
protected <T> ScopedBindingBuilder bindListToInstancesOf(TypeLiteral<List<T>> type) {
return Binders.bindListToInstancesOf(this.binder(), type);
}
protected <T> ScopedBindingBuilder bindListToInstancesOf(TypeLiteral<List<T>> type, Comparator<T> comparator) {
return Binders.bindListToInstancesOf(this.binder(), type, comparator);
}
@Override
public ScopedBindingBuilder to(Class<? extends T> implementation) {
return to(Key.get(implementation));
}
@Override
public ScopedBindingBuilder to(TypeLiteral<? extends T> implementation) {
return to(Key.get(implementation));
}
@Override
public ScopedBindingBuilder toProvider(Provider<? extends T> provider) {
return toProvider((javax.inject.Provider<? extends T>) provider);
}
@Override
public ScopedBindingBuilder toProvider(Class<? extends javax.inject.Provider<? extends T>> providerType) {
return toProvider(Key.get(providerType));
}
@Override
public ScopedBindingBuilder toProvider(TypeLiteral<? extends javax.inject.Provider<? extends T>> providerType) {
return toProvider(Key.get(providerType));
}
@Override
public <S extends T> ScopedBindingBuilder toConstructor(Constructor<S> constructor) {
return toConstructor(constructor, TypeLiteral.get(constructor.getDeclaringClass()));
}
/**
* Important moment: request scoped jersey objects must be bound to guice request scope (if guice web used)
* because otherwise scope delegation to other thread will not work
* (see {@link com.google.inject.servlet.ServletScopes#transferRequest(java.util.concurrent.Callable)}).
* <p>
* WARNING: bean instance must be obtained in current (request) thread in order to be us used later
* inside transferred thread (simply call {@code provider.get()} (for jersey-managed bean like {@link UriInfo})
* before {@code ServletScopes.transferRequest()}.
*
* @param type jersey type to bind
* @param global true for global type binding
*/
private void jerseyToGuiceBinding(final Class<?> type, final boolean global) {
final ScopedBindingBuilder binding = bindJerseyComponent(binder(), provider, type);
if (!global && guiceServletSupport) {
binding.in(RequestScoped.class);
}
}
/**
* Sets up a "choice" for the injector to resolve at injection time.
*
* @param binder the binder for the injector that is being configured
* @param property the property that will be checked to determine the implementation choice
* @param interfaceKey the interface that will be injected using this choice
* @param defaultKey the default instance to be injected if the property doesn't match a choice. Can be null
* @param <T> interface type
* @return A ScopedBindingBuilder so that scopes can be added to the binding, if required.
*/
public static <T> ScopedBindingBuilder createChoice(
Binder binder,
String property,
Key<T> interfaceKey,
Key<? extends T> defaultKey
)
{
return createChoiceWithDefault(binder, property, interfaceKey, defaultKey, null);
}
/**
* Sets up a "choice" for the injector to resolve at injection time.
*
* @param binder the binder for the injector that is being configured
* @param property the property that will be checked to determine the implementation choice
* @param interfaceKey the interface that will be injected using this choice
* @param defaultKey the default instance to be injected if the property doesn't match a choice. Can be null
* @param defaultPropertyValue the default property value to use if the property is not set.
* @param <T> interface type
* @return A ScopedBindingBuilder so that scopes can be added to the binding, if required.
*/
public static <T> ScopedBindingBuilder createChoiceWithDefault(
Binder binder,
String property,
Key<T> interfaceKey,
Key<? extends T> defaultKey,
String defaultPropertyValue
)
{
return binder.bind(interfaceKey).toProvider(new ConfiggedProvider<T>(interfaceKey, property, defaultKey, defaultPropertyValue));
}
/**
* Used to bind jersey beans in guice context (lazily). Guice context is started first, so there is
* no way to bind instances. Instead "lazy bridge" installed, which will resolve target type on first call.
* Guice is not completely started and direct injector lookup is impossible here, so lazy injector provider used.
*
* @param binder guice binder
* @param provider provider for guice injector
* @param type jersey type to register
* @param <T> type
* @return scoped binder object to optionally define binding scope.
* @see ru.vyarus.dropwizard.guice.injector.lookup.InjectorProvider
*/
public static <T> ScopedBindingBuilder bindJerseyComponent(final Binder binder, final Provider<Injector> provider,
final Class<T> type) {
return binder.bind(type).toProvider(new JerseyComponentProvider<>(provider, type));
}