Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inject grails application configuration into service

I'm creating a grails service that will interact with a 3rd party REST API via a Java library. The Java library requires credentials for the REST API by means of a url, username and password.

I'd like to store these credentials in configuration/Config.groovy, make them available to a service and ensure that credentials are available to the service before it requires them.

I appreciate that grailsApplication.config is available to controllers and that through a method of a service the relevant config values can be provided to the service, such as this:

package example  class ExampleController {      def exampleService      def index = { }      def process = {         exampleService.setCredentials(grailsApplication.config.apiCredentials)         exampleService.relevantMethod()     } } 


package example  import com.example.ExampleApiClient;  class ExampleService {      def credentials      def setCredentials(credentials) {         this.credentials = credentials     }      def relevantMethod() {          def client = new ExampleApiClient(             credentials.baseUrl,             credentials.username,             credentials.password         )          return client.action();     } } 

I feel this approach is slightly flawed as it depends on a controller calling setCredentials(). Having the credentials made available to the service automagically would be more robust.

Is either of these two options viable (I currently not familiar enough with grails):

  1. Inject grailsApplication.config.apiCredentials into the service in the controller when the service is created?

  2. Provide some form of contructor on the service that allows the credentials to be passed in to the service at instantiation time?

Having the credentials injected into the service is ideal. How could this be done?

like image 551
Jon Cram Avatar asked Feb 11 '11 20:02

Jon Cram


People also ask

How do I Use dependency injection in Grails?

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:

What is a service in Grails?

Grails provides a "service layer", which are classes that encapsulate business logic and are wired (using dependency injection) into the application context, so that any controller can inject and use them. Services are the preferred vehicle for most application logic, not controllers.

How do I customize the application class in Grails?

There are several ways in which you can customize the Application class. By default Grails will scan all known source directories for controllers, domain class etc., however if there are packages in other JAR files you wish to scan you can do so by overriding the packageNames () method of the Application class:

How do I use Grails'interactive mode?

You can also use Grails' Interactive Mode to run the Grails runtime, from which you can issue any Grails command without waiting for the runtime to spin up for each task. In this guide we will be preferring the Grails wrapper for most commands.


1 Answers

The grailsApplication object is available within services, allowing this:

package example  import com.example.ExampleApiClient;  class ExampleService {      def grailsApplication      def relevantMethod() {          def client = new ExampleApiClient(             grailsApplication.config.apiCredentials.baseUrl             grailsApplication.config.apiCredentials.username,             grailsApplication.config.apiCredentials.password         )          return client.action();     } } 
like image 90
Jon Cram Avatar answered Nov 13 '22 19:11

Jon Cram