I am trying to access grailsApplication
in groovy class under src/groovy
but I get a null pointer exception. I also tried to inject a service into the same class and same result. How can I access grailsApplication
or a service from groovy class? (I am using Grails 1.3.7)
相关问题
- Grails External Configuration. Can't access to
- grails unit test + Thread
- How to upgrade/install Upstart deamon version 1.5
- Can I override cast operator in Groovy?
- Use Groovy to Sort XML File
相关文章
- Service层和Dao层一定要对应吗?
- k8s 访问Pod 时好时坏
- Using Spring Dynamic Languages Support from Groovy
- Async task does not work properly (doInBackground
- Grails: How to make everything I create Upper Case
- Keeping alive Intent Service after application is
- A proper way to Debug a Service in Android Studio?
- Simple service in Swift, persists, foreground only
If you have classes that you want to participate with dependency injection from src/groovy or src/java or even 3rd party jars all you have to do is configure them in grails-app/conf/spring/resources.groovy.
If you had the class mypackage.MyClass in your src/groovy directory that looked like this:
Then by adding the following to grails-app/conf/spring/resoruces.groovy it would get auto-injected:
This will work in any version of grails thusfar, and like I said you can even use 3rd party jars - for example I ALWAYS have the following in my resources.groovy:
For more comprehensive Spring/Grails documentation see:
http://grails.github.io/grails-doc/latest/guide/spring.html
After Grails 2.0, you should use:
Dependency injection does not work for groovy classes under
src/groovy
. You can get the access tograilsApplication
usingApplicationHolder
like this:You can access all services like this:
The ApplicationHolder class is deprecated in newer Grails versions (2.0 and above).
There is another way, which is described in one of Burt's blogposts: http://burtbeckwith.com/blog/?p=1017