dataSource is a bean that is automatically inserted into services when used. All beans are automatically connected to grails artifacts (controllers, services, etc.) by default. In your case, you are using POGO, and I suppose it will be inside src/groovy .
You can explicitly introduce a dataSource bean into the POGO class by making the bean itself
//resources.groovy beans = { myPogo(MyPogo){ dataSource = ref('dataSource') } } //MyPogo.groovy MyPogo { def dataSource .... }
This is an expensive operation. If you are already accessing applicationContext or grailsApplication in POGO, you do not need to create a bean as described above.
dataSource bean can be directly extracted from the context as:
//ctx being ApplicationContext def dataSource = ctx.getBean('dataSource') //or if grailsApplication is available def dataSource = grailsApplication.mainContext.getBean('dataSource')
If you call the methods of the POGO class from the grails artifact, use the approach lower than all the above approaches. For instance:
//service class class MyService { def dataSource //autowired def serviceMethod(){ MyPogo pogo = new MyPogo() pogo.dataSource = dataSource //set dataSource in POGO } }
source share