How to PROPERLY inject Grails services using Spring resource.groovy

╄→гoц情女王★ 提交于 2019-12-06 01:34:03

问题


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.


回答1:


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".



来源:https://stackoverflow.com/questions/16918967/how-to-properly-inject-grails-services-using-spring-resource-groovy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!