2012-04-02

Scala + Guice + Gradle: Singleton (Part 3)

In the previous part of this tutorial I explained how to use Guice in your Gradle + Scala project. In this short post I will build up on that and show you how to create a singleton scoped instance, as the default scope in Grails is new every time requested.

Singleton

This is the singleton to be class:
class PersistenceLibraryRepository
@Inject() (private val persistenceProvider: PersistenceProvider)
extends LibraryRepository {
// ...
}
Nothing special here!

As far as I know, you cannot use the @Singleton annotation, so this is how you make this class a singleton:

class AudioliciousMigrationModule extends AbstractModule {

 @Override
 protected def configure() {
 bind(classOf[LibraryRepository])
   .to(classOf[PersistenceLibraryRepository])
   .in(Scopes.SINGLETON)  }
}
Voila!

In Part 4 I will show you how to connect to a MySQL database using JPA 2 implemented by EclipseLink.

Download source code

Source code for this article can be obtained via GitHub.

No comments:

Post a Comment