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

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

问题描述

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



获取此服务的实例的语法是什么以编程方式将服务的名称作为字符串?



即。给定'GoogleWeather',给我一个GoogleWeatherService的实例。



谢谢!

解决方案

Grails文档描述了在servlet中获得服务的方式。这可能是有用的,如果你可以在你的上下文中获得相同的对象:
$ b $ pre $ ApplicationContext ctx =(ApplicationContext)ApplicationHolder.getApplication ).getMainContext();
CountryServiceInt service =(CountryServiceInt)ctx.getBean(countryService);
String str = service.sayHello(request.getParameter。(name));


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?

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

Thanks!

解决方案

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天全站免登陆