将Grails应用程序配置注入服务 [英] Inject grails application configuration into service

查看:95
本文介绍了将Grails应用程序配置注入服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个Grails服务,它将通过Java库与第三方REST API交互。 Java库需要通过URL,用户名和密码的方式为REST API提供凭据。



我想将这些凭证存储在配置/Config.groovy ,使它们可用于服务,并确保在需要服务之前凭据可用。



我明白 grailsApplication.config 可用于控制器,并且可以通过服务的方法向服务提供相关的配置值,例如:

 包示例

类ExampleController {

def exampleService

def index = {}

def process = {
exampleService.setCredentials(grailsApplication.config.apiCredentials)
exampleService.relevantMethod()
}
}


 包示例

import com.example.ExampleApiClient;

class ExampleService {

def credentials

def setCredentials(凭证){
this.credentials =凭证
}

def relevantMethod(){

def client = new ExampleApiClient(
credentials.baseUrl,
credentials.username,
credentials.password


return client.action();


$ / code $ / pre

我觉得这种方法存在一些小缺陷,因为它取决于一个控制器调用 setCredentials()



这两个选项中的任何一个都是可行的(我目前对grails不够熟悉):


  1. 注入 grailsApplication.config.apiCredentials 到服务器中的服务创建?


  2. 在服务上提供某种形式的构造函数,允许在实例化时将凭证传递给服务?


将凭证注入到服务中是理想的。如何做到这一点?

解决方案

grailsApplication 服务,允许这样做:

 包示例

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( );
}
}


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?

解决方案

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();
    }
}

这篇关于将Grails应用程序配置注入服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆