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

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

问题描述

我正在创建一个 grails 服务,它将通过 Java 库与第 3 方 REST API 进行交互.Java 库需要通过 url、用户名和密码获取 REST API 的凭据.

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.

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

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.

我很欣赏 grailsApplication.config 可用于控制器,并且可以通过服务的方法将相关的配置值提供给服务,例如:

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

我觉得这种方法有点缺陷,因为它依赖于调用 setCredentials() 的控制器.自动向服务提供凭据会更可靠.

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.

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

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

  1. 在创建服务时将grailsApplication.config.apiCredentials 注入到控制器中的服务中?

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

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

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?

推荐答案

grailsApplication 对象在服务中可用,允许:

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天全站免登陆