A light-weight kotlin dependency injection & lifecycle management framework.
- Flavor is supposed to be an easy-to-use alternative to guice.
- Flavor also incorperates several design elements from HK2, a DI framework.
- We use kotlin-exclusive features such as reified types & inline functions heavily.
- Due to this, flavor is only compatible with kotlin-exclusive projects.
- I do not plan on adding support for other JVM languages.
- Due to this, flavor is only compatible with kotlin-exclusive projects.
- Hate my code? Despise annotations? Check out depenject by string.
- Depenject takes a delegate-based approach, rather than annotation-based. This is achieved by using Kotlin's ReadWriteProperty.
- Unfortunately, the delegate based approach makes it unusable in languages other than kotlin.
- Depenject takes a delegate-based approach, rather than annotation-based. This is achieved by using Kotlin's ReadWriteProperty.
- Services:
- Searches all singletons (kotlin objects) within a specified package for objects marked with @Service.
- Control lifecycle through the @Configure and @Close annotations.
- DI:
- Allows for simple yet effective dependency injection in both objects & classes.
- Similar to almost every other DI framework, fields are injected eagerly.
- You can create a simple work-around to this by injecting a lazy delegate.
- Listeners:
- Allows for a simple yet effective method to look for methods with specific annotations on startup.
Creating a Flavor instance:
val flavor = Flavor.create<FlavorTest>(
// This argument is optional
FlavorOptions(logger = yourLogger)
)Binding a class to an instance:
flavor.bind<SomeObject>()
.annotated<Named> {
it.value == "SomethingString"
}
.to(someObjectInstance)Instantiating an injected class (with optional parameters):
flavor.injected<SomeObjectInjected>(
// These constructor parameters are not required.
constructorParamOne, constructorParamTwo
)An example of a Flavor service:
@Service
// @IgnoreAutoScan - if you do not want Flavor to
// automatically register this service
object SomeService
{
@Inject @Named("SomethingString")
lateinit var something: String
@Configure
fun configure()
{
// this method is invoked once all
// fields have been injected!
}
@Close
fun close()
{
// this method is invoked on your
// platform's shutdown!
}
}If you want to manually register/inject into a service/class:
flavor.inject(instance)
Your Flavor instance can be started and stopped using:
flavor.startup()
flavor.close()- NOTE: Flavor automatically iterates through each class in the base package of the registration class, registers any available service, and injects all fields which need injection.
gg.scala.flavor.FlavorTest - registration class
gg.scala.flavor - package which flavor will scan
- Flavor is available on jitpack.io
If you would like compile flavor yourself, use:
- Flavor will automatically install itself to your local maven repository.
./gradlew build