如何以编程方式获取 Grails 服务的实例? [英] How do I get an instance of a Grails service programmatically?

查看:17
本文介绍了如何以编程方式获取 Grails 服务的实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个外部数据源,它将返回一个字符串,指示要使用的 Grails 服务的名称.

I have an external data source, which will return a string indicating the name of a Grails service to use.

在给定字符串形式的服务名称的情况下,以编程方式获取此服务的实例的语法是什么?

What's the syntax to get an instance of this service programatically given the name of the service as a String?

即.给定GoogleWeather",给我一个 GoogleWeatherService 实例.

ie. given 'GoogleWeather', give me an instance of GoogleWeatherService.

谢谢!

推荐答案

Grails 文档 描述了一种方法在 servlet 中获取服务.如果您可以在上下文中获得相同的对象,这可能很有用:

The Grails documentation describes a way to get a service when in a servlet. This might be useful, if you can obtain the same objects in your context:

ApplicationContext ctx = (ApplicationContext)ApplicationHolder.getApplication().getMainContext();
CountryServiceInt service = (CountryServiceInt) ctx.getBean("countryService");
String str = service.sayHello(request.getParameter.("name"));    

这篇关于如何以编程方式获取 Grails 服务的实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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