romanroe / BindForge (http://bindforge.org/)

Scala DSL for Guice

Clone this repository (size: 3.2 MB): HTTPS / SSH
$ hg clone http://bitbucket.org/romanroe/bindforge/

Welcome at the BindForge!

After the release of version 0.5, a major redesign of the configuration API took place. Please check the test specification BindForgeModuleSpec for the latest DSL syntax. New builds are already available on a maven repository. Please check the BindForgeMaven example for further information.

Additionally, BindForge now relies on the Peaberry activation mechanism for the OSGi Extender features.

Introduction

BindForge provides a Scala configuration DSL on top of the Guice dependency injection framework. Special support for the Peaberry project is provided. The DSL layer stays compatible with normal Guice modules and can be used together with "classic" Guice configuration elements at the same time.

Basic configuration:

import org.bindforge.BindForgeModule
val module = new BindForgeModule {
  def config() {
    %[ServiceA] to %[ServiceAImpl]
  } 
}
Guice.createInjector(module)

The above code will create a Guice module with one binding. The configuration is the equivalent to the Java code `bind(ServiceA.class).to(ServiceAImpl.class)`.

More examples:

%[ServiceA] toInstance new ServiceAImpl

%[ServiceA] ? Names.named("testid") to %[ServiceAImpl]  // classic version
%[ServiceA] ? "testid" to %[ServiceAImpl]               // shorthand version for Names

%[ServiceA] to %[ServiceAImpl] in Scopes.SINGLETON

// callback will be called on instance creation of ServiceBImpl
%[ServiceB] to %[ServiceBImpl] callback {b =>  // 'b' will be of type ServiceBImpl 
  b.serviceA = get(%[ServiceA])                // get(...) provides access to the Injector
}

// Instruct BindForge to inject a method
%[ServiceB] to %[ServiceBImpl] callback {b =>
  inject(b.serviceA_=)
}

OSGi Peaberry examples:

To use the Peaberry support, your Module has to mixin the OSGiSupport trait as well:

import org.bindforge.{BindForgeModule, OSGiSupport}
val module = new BindForgeModule with OSGiSupport {
  def config() {
  } 
}
Guice.createInjector(module, Peaberry.osgiModule(context))

Peaberry examples:

%[PackageAdmin] importService()                      // simple import
%[PackageAdmin] importService("service.vendor=ACME") // import with LDAP filter

%[ServiceAImpl] exportService()                      // simple service registration
%[ServiceAImpl] exportService("key" -> "value")      // registration with properties

This revision is from 2009-08-31 12:04