我正在创建一个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应用程序配置注入服务
发布评论