A light-weight kotlin IoC container and 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.
- Flavor has limited functionality for languages other than Kotlin.
- Services:
- Built-in service locator for @Service classes.
- Control lifecycle through the @PostConstruct and @PreDestroy annotations.
- Create boilerplate binder modules and inherit them through Flavor#inherit
- Use the @Extract annotation to extract constructed object instances from a FlavorBinderContainer.
- 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.
- Locating:
- 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:
- The closest constructor with all bound instances will be used.
flavor.injected<SomeObjectInjected>()
Binder modules (similar to Guice) can be created through a new FlavorBinderContainer
:
@Override
fun bind()
{
}
flavor.inherit(container)
An example of a Flavor service:
@Service
// @IgnoreAutoScan - if you do not want Flavor to
// automatically register this service
object SomeService
{
@Inject
@Named("SomethingInjected")
lateinit var something: String
@Inject
@Named("SomethingInjected")
fun onInjectSomething(string: String)
{
// injection through methods
}
@PostConstruct
fun postConstruct()
{
// this method is invoked once all
// fields have been injected!
}
@PreDestroy
fun preDestroy()
{
// 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:
- All services will be automatically located and constructed on startup.
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