Created
October 29, 2017 16:07
-
-
Save prabhatkashyap/9e2a08d2c9ecde9bf1aa13f46bdd3b17 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import org.springframework.context.ApplicationContext | |
| import org.springframework.context.ApplicationContextAware | |
| import javax.servlet.ServletContext | |
| import org.codehaus.groovy.grails.commons.GrailsApplication | |
| import org.codehaus.groovy.grails.plugins.GrailsPluginManager | |
| import org.springframework.context.ApplicationContext | |
| import org.springframework.context.ApplicationContextAware | |
| @Singleton | |
| class ApplicationContextHolder implements ApplicationContextAware { | |
| private ApplicationContext ctx | |
| private static final Map<String, Object> TEST_BEANS = [:] | |
| void setApplicationContext(ApplicationContext applicationContext) { | |
| ctx = applicationContext | |
| } | |
| static ApplicationContext getApplicationContext() { | |
| getInstance().ctx | |
| } | |
| static Object getBean(String name) { | |
| TEST_BEANS[name] ?: getApplicationContext().getBean(name) | |
| } | |
| static GrailsApplication getGrailsApplication() { | |
| getBean('grailsApplication') | |
| } | |
| static ConfigObject getConfig() { | |
| getGrailsApplication().config | |
| } | |
| static ServletContext getServletContext() { | |
| getBean('servletContext') | |
| } | |
| static GrailsPluginManager getPluginManager() { | |
| getBean('pluginManager') | |
| } | |
| // For testing | |
| static void registerTestBean(String name, bean) { | |
| TEST_BEANS[name] = bean | |
| } | |
| // For testing | |
| static void unregisterTestBeans() { | |
| TEST_BEANS.clear() | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment