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

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

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

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

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

    解决方案

    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):

  • 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?

    解决方案

    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:03:59,感谢您对本站的认可!
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:应用程序   Grails

    发布评论

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

    >www.elefans.com

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