Inject sessionFactory into grails service when calling from groovy script

I have a grails service:

MyService { def sessionFactory void someMethod() { //... sessionFactory.currentSession.createSQLQuery(sql); //... } } 

I want to call this service from a groovy script (located in src / groovy). Invoking with the grails command - grails trial test - script src \ groovy \ CallMyService.groovy.

SessionFactory is null when this method is called (it works fine when run as an integration test). But I would like to call it through a script. How can I inject sessionFactory into a service? I tried modifying RunScript.groovy, for example, for example:

 def sessionFactory = appCtx.getBean("sessionFactory") def session = SessionFactoryUtils.getSession(sessionFactory, true) TransactionSynchronizationManager.bindResource(sessionFactory, new SessionHolder(session)) 

but it did not help. Any help was appreciated.

+4
source share
1 answer

In a script with run-script you can get your service with

 myService = ctx.myService 

If spring manages your service, sessionFactory should be initialized automatically.

+3
source

All Articles