如何使用Spring resource.groovy正确注入Grails服务 [英] How to PROPERLY inject Grails services using Spring resource.groovy

查看:409
本文介绍了如何使用Spring resource.groovy正确注入Grails服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Grails 2.2.1



我定义了以下Grails服务:

  package poc 

class TestService {
def helperService
}
$ b $ class HelperService {
}

我使用了TestService(resources.groovy):

< pre $ test(poc.TestService){

}

jmsContainer(org.springframework.jms.listener.DefaultMessageListenerContainer){
connectionFactory = jmsConnectionFactory
destinationName =Test
messageListener = test
autoStartup = true
}

除了自动注入helperService之外,所有的工作都可以使用,正如Grails创建服务时所预期的那样。我能做到的唯一方法就是手动注入它:

  //增加
helper( poc.HelperService){
}

//改变了
test(poc.TestService){
helperSerivce = helper
}

问题在于它的注入方式与Grails不同。我的实际服务非常复杂,如果我不得不手动注入所有东西,包括所有的依赖项。

解决方案

resources.groovy是正常的Spring bean,不会默认参与自动装配。您可以通过显式设置autowire属性来实现:

  aBean(BeanClass){bean  - > 
bean.autowire ='byName'
}

在您的特定情况下,您不需要在resources.groovy中定义testService bean,只需从jmsContainer bean中设置对它的引用即可: c $ c> jmsContainer(org.springframework.jms.listener.DefaultMessageListenerContainer){
connectionFactory = jmsConnectionFactory
destinationName =Test
messageListener = ref('testService')//< - 运行时对Grails artefact的引用
autoStartup = true
}

参考现有豆类下的Grails文档的Grails and Spring部分, 。


Using Grails 2.2.1

I have the following Grails services defined:

package poc

class TestService {
    def helperService
}

class HelperService {
}

I have used the TestService as follow (resources.groovy):

test(poc.TestService) {

}

jmsContainer(org.springframework.jms.listener.DefaultMessageListenerContainer) {
    connectionFactory = jmsConnectionFactory
    destinationName = "Test"
    messageListener = test
    autoStartup = true
}

Everything works except for automatic injection of the helperService, as it is expected when the service create by Grails. The only way I can get it to work is to manually inject it as follow:

//added 
helper(poc.HelperService) {
}

//changed
test(poc.TestService) {
    helperSerivce = helper
}

The problem is that it is not injecting the same way as Grails does. My actual service is quite complex, and if I will have to inject everything manually, including all the dependencies.

解决方案

Beans declared in resources.groovy are normal Spring beans and do not by default participate in autowiring. You can do so by setting the autowire property on them explicitly:

aBean(BeanClass) { bean ->
    bean.autowire = 'byName'
}

In your specific case, you don't need to define the testService bean in your resources.groovy, merely set up a reference to it from your jmsContainer bean like so:

jmsContainer(org.springframework.jms.listener.DefaultMessageListenerContainer) {
    connectionFactory = jmsConnectionFactory
    destinationName = "Test"
    messageListener = ref('testService') // <- runtime reference to Grails artefact
    autoStartup = true
}

This is documented in the "Grails and Spring" section of the Grails Documentation under "Referencing Existing Beans".

这篇关于如何使用Spring resource.groovy正确注入Grails服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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