Thursday, March 4, 2010

ApplicationContext and ServletContext within GRAILS services


Q: How do I get a resource from the servletContext or applicationContext from a service?

To get the ApplicationContext, have your service implement ApplicationContextAware and declare an 'applicationContext' field. Then from within your service:
def resource = applicationContext.getResource("foo")
Accessing the Servlet Context:
import org.codehaus.groovy.grails.web.context.ServletContextHolder as SCH
def servletContext = SCH.servletContext

1 comment: