【发布时间】:2011-01-17 14:39:50
【问题描述】:
我有一个外部数据源,它将返回一个字符串,指示要使用的 Grails 服务的名称。
在给定服务名称作为字符串的情况下,以编程方式获取此服务实例的语法是什么?
即。给定“GoogleWeather”,给我一个 GoogleWeatherService 的实例。
谢谢!
【问题讨论】:
我有一个外部数据源,它将返回一个字符串,指示要使用的 Grails 服务的名称。
在给定服务名称作为字符串的情况下,以编程方式获取此服务实例的语法是什么?
即。给定“GoogleWeather”,给我一个 GoogleWeatherService 的实例。
谢谢!
【问题讨论】:
由于 ApplicationHolder 已被弃用,这是获取 ApplicationContext 的另一种方式:
ApplicationContext ctx = Holders.grailsApplication.mainContext
【讨论】:
The Grails documentation 描述了在 servlet 中获取服务的方法。如果您可以在上下文中获得相同的对象,这可能很有用:
ApplicationContext ctx = (ApplicationContext)ApplicationHolder.getApplication().getMainContext();
CountryServiceInt service = (CountryServiceInt) ctx.getBean("countryService");
String str = service.sayHello(request.getParameter.("name"));
【讨论】: