Search code examples
grailsgroovycassandragrails-ormspring-data-cassandra

How under the hood grails gets hold of cassandraTemplate instance?


Cassandra Low-level API

A lower level API is provided by the plugin that is based on the Spring Data Cassandra project.

Spring Data Cassandra provides a CassandraTemplate with methods to execute statements using the regular Cassandra Java Driver

To get hold of the cassandraTemplate instance inside a controller or service simply define a cassandraTemplate property. An example can be seen below:

def cassandraTemplate

def myAction = {
        def people = []
        people << new Person(firstName: "Fred", lastName: "Flintstone")
        people << new Person(firstName: "Barney", lastName: "Rubble")
        cassandraTemplate.insert(people)
}

Solution

  • From the docs:

    Dependency Injection Basics

    A key aspect of Grails services is the ability to use Spring Framework's dependency injection features. Grails supports "dependency injection by convention". In other words, you can use the property name representation of the class name of a service to automatically inject them into controllers, tag libraries, and so on.

    As an example, given a service called BookService, if you define a property called bookService in a controller as follows:

    class BookController {
        def bookService
        ...
    }
    

    In this case, the Spring container will automatically inject an instance of that service based on its configured scope. All dependency injection is done by name. You can also specify the type as follows:

    class AuthorService {
        BookService bookService
    }