2

I am adding features to a Grails 2.2.3 application, and I have the following two Services:

abstract class AbstractProcessService {
    def grailsApplication

    ConfigObject getConfig() {
        return grailsApplication.config.processes
    }
}

class PhotoMoverService extends AbstractProcessService {
    void processPhotos() {
        // Method body
    }

    private ConfigObject getPhotoConfig() {
        config.photoMover
    }
}

When a Quartz job finally goes to run the processPhotos() method of PhotoMoverService, the following error occurs:

core.ErrorLogger An error occured instantiating job to be executed. job= 'Photo Processes.com.company.processes.PhotoMoverJob'
org.quartz.SchedulerException: Job instantiation failed [See nested exception: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.company.processes.PhotoMoverJob': Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'photoMoverService': Cannot create inner bean '(inner bean)' while setting bean property 'target'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#2': Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public java.lang.Object org.codehaus.groovy.grails.commons.AbstractGrailsClass.newInstance()] threw exception; nested exception is org.codehaus.groovy.grails.exceptions.NewInstanceCreationException: Could not create a new instance of class [com.company.processes.PhotoMoverService]!]
    at org.quartz.core.JobRunShell.initialize(JobRunShell.java:127)
    at org.quartz.core.QuartzSchedulerThread.run(QuartzSchedulerThread.java:375)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.company.processes.PhotoMoverJob': Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'photoMoverService': Cannot create inner bean '(inner bean)' while setting bean property 'target'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#2': Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public java.lang.Object org.codehaus.groovy.grails.commons.AbstractGrailsClass.newInstance()] threw exception; nested exception is org.codehaus.groovy.grails.exceptions.NewInstanceCreationException: Could not create a new instance of class [com.company.processes.PhotoMoverService]!
    at grails.plugins.quartz.GrailsJobFactory.createJobInstance(GrailsJobFactory.java:48)
    at grails.plugins.quartz.QuartzMonitorJobFactory.createJobInstance(QuartzMonitorJobFactory.groovy:22)
    ... 2 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'photoMoverService': Cannot create inner bean '(inner bean)' while setting bean property 'target'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#2': Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public java.lang.Object org.codehaus.groovy.grails.commons.AbstractGrailsClass.newInstance()] threw exception; nested exception is org.codehaus.groovy.grails.exceptions.NewInstanceCreationException: Could not create a new instance of class [com.company.processes.PhotoMoverService]!
    ... 4 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#2': Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public java.lang.Object org.codehaus.groovy.grails.commons.AbstractGrailsClass.newInstance()] threw exception; nested exception is org.codehaus.groovy.grails.exceptions.NewInstanceCreationException: Could not create a new instance of class [com.company.processes.PhotoMoverService]!
    ... 4 more
Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public java.lang.Object org.codehaus.groovy.grails.commons.AbstractGrailsClass.newInstance()] threw exception; nested exception is org.codehaus.groovy.grails.exceptions.NewInstanceCreationException: Could not create a new instance of class [com.company.processes.PhotoMoverService]!
    ... 4 more
Caused by: org.codehaus.groovy.grails.exceptions.NewInstanceCreationException: Could not create a new instance of class [com.company.processes.PhotoMoverService]!
    ... 4 more
Caused by: java.lang.NullPointerException: Cannot get property 'config' on null object
    at com.company.processes.AbstractProcessService.getConfig(AbstractProcessService.groovy:23)
    at com.company.processes.PhotoMoverService.getPhotoConfig(PhotoMoverService.groovy:213)
    ... 4 more

When I debug the application, grailsApplication in AbstractProcessService is null like it is not being injected properly. Can anyone tell me 1) Why it is not being injected and 2) what I can do to make that happen. Thanks!

grantmcconnaughey
  • 10,130
  • 10
  • 37
  • 66
  • I have run into the exact same problem, both in services and in manually managed src/groovy bean registered classes. My question http://stackoverflow.com/questions/25774828/how-can-i-access-injected-grails-beans-in-an-abstract-class-method?noredirect=1#comment40394107_25774828 went unanswered. We really need one of the Grails gurus to jump in on this. I got one claim that it worked, but it fails consistently for me as well. – Steve Hole Sep 29 '14 at 20:29
  • Well shoot, that's really depressing. I don't suppose you had any luck after upgrading your application to Grails 2.4.3? – grantmcconnaughey Sep 29 '14 at 20:32
  • Have you tried defining the abstract bean and setting the propeery abstract=true on that – allthenutsandbolts Sep 29 '14 at 20:45
  • I did try adding abstract=true. I experienced the same issue. – grantmcconnaughey Sep 29 '14 at 20:53
  • I just created a new Grails 2.2.5 test app, created an abstract service with `grailsApplication` injected into it, created a service that extends the abstract service, and everything worked fine. Still not working on my other app, though. Weird. – grantmcconnaughey Sep 29 '14 at 21:33
  • @SteveHole I just left an answer over at http://stackoverflow.com/questions/25774828/how-can-i-access-injected-grails-beans-in-an-abstract-class-method. Let me know if that works for you. – Jeff Scott Brown Sep 30 '14 at 00:38
  • Try putting the abstract base class(es) in `src/groovy`. It should work if they're in `grails-app/services` but depending on other code and configuration that you have and aren't showing it's possible that the abstract class is getting misconfigured somehow. But there's no reason for it to be under `grails-app`, only the concrete classes that will end up as Spring beans should be there. With the base class in `src/groovy` the properties and methods (including the `setGrailsApplication` that the Groovy compiler adds, which Spring calls to inject `grailsApplication`) should be inherited. – Burt Beckwith Sep 30 '14 at 07:48

0 Answers0