Package com.google.inject.binder

Examples of com.google.inject.binder.ScopedBindingBuilder


    boundClasses.add(requiredType);
    logger.debug("Binding {} to {}", requiredType, componentType);
    if (StereotypeHandler.class.isAssignableFrom(requiredType)) {
      stereotypeHandlers.addBinding().to(requiredType);
    }
    ScopedBindingBuilder binding = bindToConstructor(requiredType, componentType);
    if (defaultScope(componentType)) {
      binding.in(GuiceProvider.REQUEST);
    }
    registerFactory(componentType);
  }
View Full Code Here


    this.binder = binder;
  }
  public void register(Class requiredType, Class componentType) {
    boundClasses.add(requiredType);
    logger.debug("Binding {} to {}", requiredType, componentType);
    ScopedBindingBuilder binding = bindToConstructor(requiredType, componentType);
    if (defaultScope(componentType)) {
      binding.in(GuiceProvider.REQUEST);
    }
    registerFactory(componentType);
  }
View Full Code Here

    @SuppressWarnings({"unchecked"})
    private <T> void configure(Context context, Binder binder, Type type, Bind annotation, InjectedProvider<T> provider) {
        AnnotatedBindingBuilder<T> builder1 = (AnnotatedBindingBuilder<T>) binder.bind(TypeLiteral.get(type));
        LinkedBindingBuilder<T> builder2 = annotation.annotatedBy().equals(NoAnnotation.class) ? builder1 : builder1.annotatedWith(annotation.annotatedBy());
        ScopedBindingBuilder builder3 = builder2.toProvider(provider);
        if (!annotation.scope().equals(NoAnnotation.class)) {
            builder3.in(annotation.scope());
        }
        context.<List<InjectedProvider<T>>>getAttribute("guice.providers").add(provider);
    }
View Full Code Here

    @SuppressWarnings({"unchecked"})
    private <T> void configure(TestContext context, Binder binder, Type type, Bind annotation, InjectedProvider<T> provider) {
        AnnotatedBindingBuilder<T> builder1 = (AnnotatedBindingBuilder<T>) binder.bind(TypeLiteral.get(type));
        LinkedBindingBuilder<T> builder2 = annotation.annotatedBy().equals(NoAnnotation.class) ? builder1 : builder1.annotatedWith(annotation.annotatedBy());
        ScopedBindingBuilder builder3 = builder2.toProvider(provider);
        if (!annotation.scope().equals(NoAnnotation.class)) {
            builder3.in(annotation.scope());
        }
        context.attributes().<List<InjectedProvider<T>>>get("guice.providers").add(provider);
    }
View Full Code Here

      new Builder()
          .name("bind A to PlainA")
          .module(new AbstractModule() {
            protected void configure() {
              ScopedBindingBuilder sbb = bind(A.class).to(PlainA.class);
              scoper.configure(sbb);
            }
          })
          .scoper(scoper)
          .addToSuite(suite);

      new Builder()
          .name("bind A to PlainAProvider.class")
          .module(new AbstractModule() {
            protected void configure() {
              ScopedBindingBuilder sbb = bind(A.class).toProvider(PlainAProvider.class);
              scoper.configure(sbb);
            }
          })
          .scoper(scoper)
          .addToSuite(suite);

      new Builder()
          .name("bind A to new PlainAProvider()")
          .module(new AbstractModule() {
            protected void configure() {
              ScopedBindingBuilder sbb = bind(A.class).toProvider(new PlainAProvider());
              scoper.configure(sbb);
            }
          })
          .scoper(scoper)
          .addToSuite(suite);

      new Builder()
          .name("bind AWithProvidedBy")
          .key(Key.get(AWithProvidedBy.class), InjectsAWithProvidedBy.class)
          .module(new AbstractModule() {
            protected void configure() {
              ScopedBindingBuilder sbb = bind(AWithProvidedBy.class);
              scoper.configure(sbb);
            }
          })
          .scoper(scoper)
          .addToSuite(suite);

      new Builder()
          .name("bind AWithImplementedBy")
          .key(Key.get(AWithImplementedBy.class), InjectsAWithImplementedBy.class)
          .module(new AbstractModule() {
            protected void configure() {
              ScopedBindingBuilder sbb = bind(AWithImplementedBy.class);
              scoper.configure(sbb);
            }
          })
          .scoper(scoper)
          .addToSuite(suite);

      new Builder()
          .name("bind ScopedA")
          .key(Key.get(ScopedA.class), InjectsScopedA.class)
          .module(new AbstractModule() {
            protected void configure() {
              ScopedBindingBuilder sbb = bind(ScopedA.class);
              scoper.configure(sbb);
            }
          })
          .expectedValues(new PlainA(201), new PlainA(201), new PlainA(202), new PlainA(202))
          .scoper(scoper)
View Full Code Here

  public void testBindWithMultipleScopesAddsError() {
    checkModule(
        new AbstractModule() {
          protected void configure() {
            ScopedBindingBuilder sbb = bind(List.class).to(ArrayList.class);
            sbb.in(Scopes.NO_SCOPE);
            sbb.asEagerSingleton();
          }
        },

        new FailingElementVisitor() {
          @Override public <T> Void visit(Binding<T> command) {
View Full Code Here

            final Named bindingName = getBindingName( providerType );
            final Class<?>[] types = getBindingTypes( providerType );

            final Key key = getBindingKey( args[0], bindingName );
            final ScopedBindingBuilder sbb = binder.bind( key ).toProvider( providerType );
            if ( isEagerSingleton( providerType ) )
            {
                sbb.asEagerSingleton();
            }
            else if ( isSingleton( providerType ) )
            {
                sbb.in( Scopes.SINGLETON );
            }

            if ( null != types )
            {
                for ( final Class bindingType : types )
View Full Code Here

     *
     * @param qualifiedType The qualified type
     */
    private void bindQualifiedType( final Class<?> qualifiedType )
    {
        final ScopedBindingBuilder sbb = binder.bind( qualifiedType );
        if ( isEagerSingleton( qualifiedType ) )
        {
            sbb.asEagerSingleton();
        }

        final Named bindingName = getBindingName( qualifiedType );
        final Class<?>[] types = getBindingTypes( qualifiedType );

View Full Code Here

    boundClasses.add(requiredType);
    logger.debug("Binding {} to {}", requiredType, componentType);
    if (StereotypeHandler.class.isAssignableFrom(requiredType)) {
      stereotypeHandlers.addBinding().to(requiredType);
    }
    ScopedBindingBuilder binding = bindToConstructor(requiredType, componentType);
    if (defaultScope(componentType)) {
      binding.in(GuiceProvider.REQUEST);
    }
    registerFactory(componentType);
  }
View Full Code Here

            key = Key.get(beanBinding.type);
          }
          LinkedBindingBuilder b = bind(key);

          //
          ScopedBindingBuilder c;
          if (beanBinding instanceof BeanBinding.ToInstance) {
            BeanBinding.ToInstance d = (BeanBinding.ToInstance)beanBinding;
            b.toInstance(d.instance);
            c = b;
          }
          else if (beanBinding instanceof BeanBinding.ToProviderInstance) {
            BeanBinding.ToProviderInstance d = (BeanBinding.ToProviderInstance)beanBinding;
            c = b.toProvider(d);
            if (beanBinding.scopeType != null) {
              c.in(beanBinding.scopeType);
            }
          }
          else {
            if (beanBinding instanceof BeanBinding.ToProviderType) {
              BeanBinding.ToProviderType d = (BeanBinding.ToProviderType)beanBinding;
              c = b.toProvider(d.provider);
            }
            else {
              BeanBinding.ToType d = (BeanBinding.ToType)beanBinding;
              if (d.qualifiers != null) {
                if (d.implementationType != null) {
                  c = b.to(d.implementationType);
                  // Guice trick : need to bind alias so the implementation type will be bound with the qualifier
                  bind(Key.get(d.implementationType, key.getAnnotation())).toProvider(new BeanAlias(key));
                }
                else {
                  c = b.to(beanBinding.type);
                }
              }
              else {
                if (d.implementationType != null) {
                  c = b.to(d.implementationType);
                }
                else {
                  c = b;
                }
              }
            }
            if (beanBinding.scopeType != null) {
              c.in(beanBinding.scopeType);
            }
          }
        }

        // Bind the manager itself
View Full Code Here

TOP

Related Classes of com.google.inject.binder.ScopedBindingBuilder

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.