Project Kronslott
DropWizard bundle to set the SendGrid API key in your DropWizard configuration and expose the SendGrid object through Dagger.
A simple mail sending service is also exposed via Dagger.
sendGrid:
apiKey: ...
This also shows how I add Dagger to DropWizard: I run bootstrap.addBundle() with an object expression (anonymous class) that instantiates the main Dagger component for the application and sets it as a property of the DropWizard application.
@Singleton
@Component(dependencies = [SendGridComponent::class], modules = [/*...*/])
interface ExampleComponent {
// ...
}
class ExampleConfig : Configuration(), SendGridConfig {
@Valid
@JsonProperty("sendGrid")
override var sendGridSettings: SendGridSettings = SendGridSettings()
// ...
}
class ExampleApp : Application<ExampleConfig>() {
private lateinit var component: ExampleComponent
override fun initialize(bootstrap: Bootstrap<ExampleConfig>) {
val sendGridBundle = SendGridBundle<ExampleConfig>()
bootstrap.addBundle(sendGridBundle)
bootstrap.addBundle(object : ConfiguredBundle<ExampleConfig> {
override fun run(configuration: ExampleConfig, environment: Environment) {
component = DaggerExampleComponent.builder()
.exampleModule(ExampleModule(configuration, environment))
.sendGridComponent(sendGridBundle.sendGridComponent)
.build()
}
override fun initialize(bootstrap: Bootstrap<*>) {}
})
// ...
}
// ...
}
// Class that needs SendGrid
class ExampleService1 @Inject constructor(private val sendGrid: SendGrid) {
// ...
}
// Class that needs the MailSendingService
class ExampleService2 @Inject constructor(private val mailSendingService: MailSendingService) {
fun sendMail() {
mailSendingService.sendMail(Email("from@example.com"), "*SUBJECT*", Email("to@example.com"), Content("text/html", "*HTML*"))
}
}
This project is not yet available from Maven Central Repository, but it's available via JitPack.io.