Load spring beans from custom groovy files in grails app

余生长醉 提交于 2019-12-01 17:01:20

I had a similar problem just a few days ago, with a groovy configuration file that I added into grails-app/conf. While this works with other resources (they are copied and available on the classpath), the problem with the groovy file was simply that it was compiled and the class file was included, i.e. not the groovy file itself.

I didn't find any good documentation on how this should be done and finally just added it to web-app/WEB-INF/classes. Groovy files placed here will be copied (not compiled) and available on the classpath.

I had the same problem with custom XML files in Grails 2.1.2.

Having XML resources in grails-app/conf/spring didn't work in production environment AFAIR.

To make it working both in development and production environments I finally put the resources into src/java. I think you can achieve the same result by putting your groovy files into src/groovy.

We can import beans from different groovy/xml file in the following way too : - use the following in resources.groovy -

importBeans 'file:camel-beans.groovy' OR importBeans('classpath:/camel-config.xml')

Place camel-beans.groovy along with resources.groovy and provide package as "package spring" for first case, otherwise put it in web app classpath and use the second way to do it.

If your resources.groovy is at following path

grails-app/conf/spring/resources.groovy

and your camel-beans.groovy is at following path

grails-app/conf/spring/camel-beans.groovy

then you can reference camel-beans.groovy in resources.groovy file by adding following line in resources.groovy

importBeans('file:**/camel-beans.groovy')
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!