How to effectively implement Grails services with Spring resource.groovy

Using Grails 2.2.1

I have the following Grails services:

package poc class TestService { def helperService } class HelperService { } 

I used TestService as follows (resources.groovy):

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

Everything works, except for automatically entering helperService, as expected when the service creates Grails. The only way to make it work is to manually enter it as follows:

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

The problem is that it is not introduced the same way Grails does. My actual maintenance is quite complicated, and if I have to enter everything manually, including all the dependencies.

+6
source share
1 answer

Beans declared in resources.groovy are normal Spring beans and are not automatically prepared by default. You can do this by setting the autwire property for them explicitly:

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

In your specific case, you do not need to define a testService bean in your .groovy resources, just set a link to it from your jmsContainer bean, for example:

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

This is described in the Grails and Spring section of the Grails documentation in the Link to Existing Beans section.

+9
source

Source: https://habr.com/ru/post/946503/


All Articles