I have a grails service:
MyService {
def sessionFactory
void someMethod() {
//...
sessionFactory.currentSession.createSQLQuery(sql);
//...
}
}
I want to invoke this service from a groovy script (located in src/groovy). Im invoking via the grails command – grails test run-script src\groovy\CallMyService.groovy.
The sessionFactory is null when called this way (it works fine when run as an integration test). But I would like to invoke it via a script. How can I inject the sessionFactory into the service? I tried modifying the RunScript.groovy, for eg like this:
def sessionFactory = appCtx.getBean("sessionFactory")
def session = SessionFactoryUtils.getSession(sessionFactory, true)
TransactionSynchronizationManager.bindResource(sessionFactory, new SessionHolder(session))
but that didnt help. Any help appreciated.
In a script run with
run-script, you should be able to get your service with:If spring is managing your service, the
sessionFactoryshould be initialized automatically.