jw3 / akka-injects

Dependency Injection for Akka using Guice
Apache License 2.0
3 stars 4 forks source link
akka dependency-injection guice

Akka Injects

Build Status Dependencies

Dependency Injection DSL for Akka. Powered by Guice and implemented as an Akka Extension.

Goals

Installation

By default the Inject Extension will be loaded on first use.

Or

If you want to force load the extension at ActorSystem creation time you can Load from Configuration.

akka {
  extensions = ["com.rxthings.di.InjectExt"]
}

Artifacts

To include in your SBT project add a resolver to your sbt build

resolvers += Resolver.bintrayRepo("jw3", "maven")

and add the dependency

libraryDependencies += "com.rxthings" %% "akka-injects" % "0.9"

Imports

All required imports are included with

import com.rxthings.di._

Configuration Options

Module Discovery Modes

The default mode is manual

Example Usage

Injection is implicit when the lhs is explicitly typed

val config: Config = inject[Config]

val thing: Thing = inject[Thing]

val named: String = inject[String] annotated "namedString"

val actor: ActorRef = injectActor[MyActor]

Optional injection is also implicit when the lhs is an Option

val optionalThing: Option[Thing] = inject[Thing]

val optionalActor: Option[ActorRef] = injectActor[MyActor]

val noThing: Option[Unbound] = inject[Unbound] // == None

val noActor: Option[ActorRef] = injectActor[UnboundActor] // == None

When lhs is not explicitly typed, the required or optional method must be called

val thing = inject[Thing] required

val actor = injectActor[MyActor] required

val optionalThing = inject[Thing] optional

val optionalActor = injectActor[MyActor] optional

Parameters can be passed to ctors using arguments

val thingWithCtorArgs: Thing = inject[Thing] arguments("foo", 999)

Shortcuts for binding annotations, like @Named

val bob: Option[ActorRef] = injectActor[MyActor] named "bob"

Injection within an Actor is easy and final

class MyActor extends Actor {
    val otherActor: ActorRef = inject[MyOtherActor] // otherActor.parent == self
    val configProp: String = inject[String] named "myactor.hostname"
}

Notes

Bugs and Feedback

For bugs, questions and discussions please use the Github Issues.

License

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

https://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.