本文整理汇总了Java中com.google.inject.Binding类的典型用法代码示例。如果您正苦于以下问题:Java Binding类的具体用法?Java Binding怎么用?Java Binding使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
Binding类属于com.google.inject包,在下文中一共展示了Binding类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: visit
import com.google.inject.Binding; //导入依赖的package包/类
@Override
public Void visit(ExposedBinding<? extends T> binding) {
final PrivateBinder privateBinder = this.binder.newPrivateBinder();
final Scoper scoper = new Scoper(privateBinder, scoping);
for(Element element : binding.getPrivateElements().getElements()) {
if(element instanceof Binding) {
((Binding) element).acceptTargetVisitor(scoper);
} else {
element.applyTo(privateBinder);
}
}
for(Key key : binding.getPrivateElements().getExposedKeys()) {
privateBinder.expose(key);
}
return null;
}
示例2: getBean
import com.google.inject.Binding; //导入依赖的package包/类
@SuppressWarnings("unchecked")
@Override
public <T> T getBean(String beanId)
{
Injector injector = ensureInjector();
Key<Object> nameKey = Key.get(Object.class, Names.named(beanId));
Binding<Object> binding = injector.getExistingBinding(nameKey);
if( binding != null )
{
return (T) binding.getProvider().get();
}
ClassLoader classLoader = privatePluginService.getClassLoader(pluginId);
try
{
Class<?> clazz = classLoader.loadClass(beanId);
return (T) injector.getInstance(clazz);
}
catch( ClassNotFoundException e )
{
throw new RuntimeException(e);
}
}
示例3: GuiceQueryPluginFactory
import com.google.inject.Binding; //导入依赖的package包/类
@Inject
@SuppressWarnings("unchecked")
public GuiceQueryPluginFactory(Injector injector)
{
m_injector = injector;
Map<Key<?>, Binding<?>> bindings = injector.getAllBindings();
for (Key<?> key : bindings.keySet())
{
Class<?> bindingClass = key.getTypeLiteral().getRawType();
if (QueryPlugin.class.isAssignableFrom(bindingClass))
{
PluginName ann = (PluginName) bindingClass.getAnnotation(PluginName.class);
if (ann == null)
throw new IllegalStateException("Aggregator class " + bindingClass.getName() +
" does not have required annotation " + PluginName.class.getName());
m_plugins.put(ann.name(), (Class<QueryPlugin>)bindingClass);
}
}
}
示例4: startServices
import com.google.inject.Binding; //导入依赖的package包/类
public void startServices() throws KairosDBException
{
Map<Key<?>, Binding<?>> bindings =
m_injector.getAllBindings();
for (Key<?> key : bindings.keySet())
{
Class<?> bindingClass = key.getTypeLiteral().getRawType();
if (KairosDBService.class.isAssignableFrom(bindingClass))
{
KairosDBService service = (KairosDBService) m_injector.getInstance(bindingClass);
logger.info("Starting service " + bindingClass);
service.start();
m_services.add(service);
}
}
}
示例5: GuiceGroupByFactory
import com.google.inject.Binding; //导入依赖的package包/类
@Inject
@SuppressWarnings("unchecked")
public GuiceGroupByFactory(Injector injector)
{
this.injector = injector;
Map<Key<?>, Binding<?>> bindings = injector.getAllBindings();
for (Key<?> key : bindings.keySet())
{
Class<?> bindingClass = key.getTypeLiteral().getRawType();
if (GroupBy.class.isAssignableFrom(bindingClass))
{
GroupByName name = (GroupByName)bindingClass.getAnnotation(GroupByName.class);
if (name == null)
throw new IllegalStateException("Aggregator class "+bindingClass.getName()+
" does not have required annotation "+GroupByName.class.getName());
groupBys.put(name.name(), (Class<GroupBy>)bindingClass);
}
}
}
示例6: HealthCheckServiceImpl
import com.google.inject.Binding; //导入依赖的package包/类
@Inject
public HealthCheckServiceImpl(Injector injector)
{
checkNotNull(injector);
Map<Key<?>, Binding<?>> bindings = injector.getAllBindings();
for (Key<?> key : bindings.keySet())
{
Class<?> bindingClass = key.getTypeLiteral().getRawType();
if (HealthStatus.class.isAssignableFrom(bindingClass))
{
checks.add((HealthStatus) injector.getInstance(bindingClass));
}
}
}
示例7: provide
import com.google.inject.Binding; //导入依赖的package包/类
@Override
public <T> T provide(Class<T> objectType, AnnotationProvider annotationProvider, ObjectLocator locator) {
TypeLiteral<T> type = TypeLiteral.get(objectType);
List<Binding<T>> bindings = injector.findBindingsByType(type);
for (Binding<T> binding : bindings) {
Class<? extends Annotation> annotationType = binding.getKey().getAnnotationType();
Annotation annotation = annotationType != null ? annotationProvider.getAnnotation(annotationType) : null;
Key<T> key = annotation != null ? Key.get(type, annotation) : Key.get(type);
if (key.equals(binding.getKey())) {
return injector.getInstance(key);
}
}
return null;
}
示例8: testConfigureEndpoints_withInterceptor
import com.google.inject.Binding; //导入依赖的package包/类
@Test
public void testConfigureEndpoints_withInterceptor() {
Injector injector = Guice.createInjector(module, new InterceptorModule());
Visitor visitor = new Visitor();
for (Binding<?> binding : injector.getAllBindings().values()) {
binding.acceptTargetVisitor(visitor);
}
assertEquals("Servlet not bound.", 1, visitor.linkedServlets.size());
LinkedServletBinding servletBinding = visitor.linkedServlets.get(0);
assertEquals("URL pattern does not match", URL_PATTERN, servletBinding.getPattern());
assertEquals("Wrong initialization parameter provided", "false",
servletBinding.getInitParams().get("restricted"));
assertNotNull("SystemService named provider not found.", visitor.systemServiceProvider);
ServiceMap serviceMap = (ServiceMap) visitor.systemServiceProvider.getProvider().get();
Collection<Object> services = serviceMap.getServices();
assertEquals("Incorrect number of services provided", 1, services.size());
assertEquals("Service not enhanced correctly.", SERVICES.toArray()[0],
((Class<?>) services.toArray()[0].getClass()).getSuperclass());
}
示例9: testConfigureEndpoints_withoutInterceptor
import com.google.inject.Binding; //导入依赖的package包/类
@Test
public void testConfigureEndpoints_withoutInterceptor() {
Injector injector = Guice.createInjector(module, new DummyModule());
Visitor visitor = new Visitor();
for (Binding<?> binding : injector.getAllBindings().values()) {
binding.acceptTargetVisitor(visitor);
}
assertEquals("Servlet not bound.", 1, visitor.linkedServlets.size());
LinkedServletBinding servletBinding = visitor.linkedServlets.get(0);
assertEquals("URL pattern does not match", URL_PATTERN, servletBinding.getPattern());
assertEquals("Wrong initialization parameter provided", "false",
servletBinding.getInitParams().get("restricted"));
assertNotNull("SystemService named provider not found.", visitor.systemServiceProvider);
ServiceMap serviceMap = (ServiceMap) visitor.systemServiceProvider.getProvider().get();
Collection<Object> services = serviceMap.getServices();
assertEquals("Incorrect number of services provided", 1, services.size());
assertEquals("Service not provided correctly.", SERVICES.toArray()[0],
services.toArray()[0].getClass());
}
示例10: provideLinkRestRuntime
import com.google.inject.Binding; //导入依赖的package包/类
@Singleton
@Provides
LinkRestRuntime provideLinkRestRuntime(
Injector injector,
Set<LrFeatureProvider> featureProviders,
Set<LinkRestAdapter> adapters) {
LinkRestBuilder builder;
Binding<ServerRuntime> binding = injector.getExistingBinding(Key.get(ServerRuntime.class));
if (binding == null) {
builder = new LinkRestBuilder().cayenneService(new PojoCayennePersister());
} else {
ServerRuntime cayenneRuntime = binding.getProvider().get();
builder = new LinkRestBuilder().cayenneRuntime(cayenneRuntime);
}
featureProviders.forEach(builder::feature);
adapters.forEach(builder::adapter);
return builder.build();
}
示例11: initialize
import com.google.inject.Binding; //导入依赖的package包/类
@Inject
@Toolable
@SuppressWarnings("unchecked")
void initialize(Injector injector) {
final Binding<T> realBinding = injector.getBinding(this.rewritten);
final Provider<T> realProvider = injector.getProvider(realBinding.getKey());
// The proxy will be a sub type of the source type of the binding
final Class<T> proxyType = (Class<T>) realBinding.getKey()
.getTypeLiteral().getRawType();
this.dependencies = Collections.singleton(
Dependency.get(this.rewritten));
this.ref = InstanceBuilder.forType(proxyType)
.withConstructionStrategy(this.strategy)
.dispatchTo(realProvider)
.create(injector);
}
示例12: provideObjectFromNamedBindingOrJndi
import com.google.inject.Binding; //导入依赖的package包/类
protected Object provideObjectFromNamedBindingOrJndi(
TypeLiteral<?> requiredType, String name) {
Binding<?> binding = Injectors.getBinding(injector,
Key.get(requiredType, Names.named(name)));
if (binding != null) {
return binding.getProvider().get();
}
// TODO we may want to try avoid the dependency on JNDI classes
// for better operation in GAE?
try {
if (context == null) {
context = new InitialContext();
}
return context.lookup(name);
} catch (NamingException e) {
throw new ProvisionException("Failed to find name '" + name
+ "' in JNDI. Cause: " + e, e);
}
}
示例13: getCamelContextMap
import com.google.inject.Binding; //导入依赖的package包/类
protected Map<String, CamelContext> getCamelContextMap() {
Map<String, CamelContext> answer = Maps.newHashMap();
if (injector != null) {
Set<Map.Entry<Key<?>, Binding<?>>> entries = injector.getBindings().entrySet();
for (Map.Entry<Key<?>, Binding<?>> entry : entries) {
Key<?> key = entry.getKey();
Class<?> keyType = Injectors.getKeyType(key);
if (keyType != null && CamelContext.class.isAssignableFrom(keyType)) {
Binding<?> binding = entry.getValue();
Object value = binding.getProvider().get();
if (value != null) {
CamelContext castValue = CamelContext.class.cast(value);
answer.put(key.toString(), castValue);
}
}
}
}
return answer;
}
示例14: getInstancesOf
import com.google.inject.Binding; //导入依赖的package包/类
/**
* Returns a collection of all instances of the given base type
*
* @param baseClass
* the base type of objects required
* @param <T>
* the base type
* @return a set of objects returned from this injector
*/
public static <T> Set<T> getInstancesOf(Injector injector,
Class<T> baseClass) {
Set<T> answer = Sets.newHashSet();
Set<Entry<Key<?>, Binding<?>>> entries = injector.getBindings()
.entrySet();
for (Entry<Key<?>, Binding<?>> entry : entries) {
Key<?> key = entry.getKey();
Class<?> keyType = getKeyType(key);
if (keyType != null && baseClass.isAssignableFrom(keyType)) {
Binding<?> binding = entry.getValue();
Object value = binding.getProvider().get();
if (value != null) {
T castValue = baseClass.cast(value);
answer.add(castValue);
}
}
}
return answer;
}
示例15: getProvidersOf
import com.google.inject.Binding; //导入依赖的package包/类
/**
* Returns a collection of all of the providers matching the given matcher
*
* @param matcher
* matches the types to return instances
* @return a set of objects returned from this injector
*/
public static <T> Set<Provider<T>> getProvidersOf(Injector injector,
Matcher<Class> matcher) {
Set<Provider<T>> answer = Sets.newHashSet();
Set<Entry<Key<?>, Binding<?>>> entries = injector.getBindings()
.entrySet();
for (Entry<Key<?>, Binding<?>> entry : entries) {
Key<?> key = entry.getKey();
Class<?> keyType = getKeyType(key);
if (keyType != null && matcher.matches(keyType)) {
Binding<?> binding = entry.getValue();
answer.add((Provider<T>) binding.getProvider());
}
}
return answer;
}