将 grails 应用程序配置注入服务

编程入门 行业动态 更新时间:2024-10-18 20:29:00
本文介绍了将 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):

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

  • 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 应用程序配置注入服务

    本文发布于:2023-11-07 23:04:44,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1567687.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:应用程序   grails

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!