diff --git a/grails-core/src/main/groovy/grails/boot/config/GrailsApplicationPostProcessor.groovy b/grails-core/src/main/groovy/grails/boot/config/GrailsApplicationPostProcessor.groovy index 9de746d7e8f..5c5b0d02709 100644 --- a/grails-core/src/main/groovy/grails/boot/config/GrailsApplicationPostProcessor.groovy +++ b/grails-core/src/main/groovy/grails/boot/config/GrailsApplicationPostProcessor.groovy @@ -68,15 +68,10 @@ class GrailsApplicationPostProcessor implements BeanDefinitionRegistryPostProces GrailsApplicationPostProcessor(GrailsApplicationLifeCycle lifeCycle, ApplicationContext applicationContext, Class...classes) { this.lifeCycle = lifeCycle - if(lifeCycle instanceof GrailsApplicationClass) { - this.applicationClass = (GrailsApplicationClass)lifeCycle - } - else { - this.applicationClass = null - } - this.classes = classes != null ? classes : [] as Class[] - grailsApplication = applicationClass != null ? new DefaultGrailsApplication(applicationClass) : new DefaultGrailsApplication() - pluginManager = new DefaultGrailsPluginManager(grailsApplication) + this.applicationClass = lifeCycle instanceof GrailsApplicationClass ? (GrailsApplicationClass) lifeCycle : null + this.classes = classes ?: [] as Class[] + this.grailsApplication = applicationClass ? new DefaultGrailsApplication(applicationClass) : new DefaultGrailsApplication() + this.pluginManager = applicationContext?.getBeanNamesForType(GrailsPluginManager) ? applicationContext.getBean(GrailsPluginManager) : new DefaultGrailsPluginManager(grailsApplication) if(applicationContext != null) { setApplicationContext(applicationContext) } @@ -290,30 +285,22 @@ class GrailsApplicationPostProcessor implements BeanDefinitionRegistryPostProces } } - @SuppressWarnings("GrMethodMayBeStatic") private void loadPluginConfigurationsToMicronautContext(ConfigurableApplicationContext applicationContext) { - String[] beanNames = applicationContext.getBeanNamesForType(GrailsPluginManager) - if (beanNames.length == 0) { - // do not continue if PluginManager is not available + ConfigurableApplicationContext parentApplicationContext = (ConfigurableApplicationContext) applicationContext.parent + if (!parentApplicationContext) { + // No Micronaut parent context, return early return } - - GrailsPluginManager pluginManager = applicationContext.getBean(GrailsPluginManager) - ConfigurableApplicationContext parentApplicationContext = (ConfigurableApplicationContext) applicationContext.parent ConfigurableEnvironment parentContextEnv = parentApplicationContext.getEnvironment() if (parentContextEnv instanceof MicronautEnvironment) { - if (log.isDebugEnabled()) { - log.debug("Loading configurations from the plugins to the parent Micronaut context") - } + log.debug('Loading configurations from the plugins to the parent Micronaut context') final io.micronaut.context.env.Environment micronautEnv = ((io.micronaut.context.env.Environment) parentContextEnv.getEnvironment()) final GrailsPlugin[] plugins = pluginManager.allPlugins Integer priority = AbstractPropertySourceLoader.DEFAULT_POSITION Arrays.stream(plugins) .filter({ GrailsPlugin plugin -> plugin.propertySource != null }) .forEach({ GrailsPlugin plugin -> - if (log.isDebugEnabled()) { - log.debug("Loading configurations from {} plugin to the parent Micronaut context", plugin.name) - } + log.debug('Loading configurations from {} plugin to the parent Micronaut context', plugin.name) micronautEnv.addPropertySource(PropertySource.of("grails.plugins.$plugin.name", (Map) plugin.propertySource.source, --priority)) }) micronautEnv.refresh() diff --git a/grails-core/src/test/groovy/org/grails/plugins/GrailsPluginConfigurationClass.groovy b/grails-core/src/test/groovy/org/grails/plugins/GrailsPluginConfigurationClass.groovy index 6f413170fce..65e0056a8f9 100644 --- a/grails-core/src/test/groovy/org/grails/plugins/GrailsPluginConfigurationClass.groovy +++ b/grails-core/src/test/groovy/org/grails/plugins/GrailsPluginConfigurationClass.groovy @@ -1,13 +1,10 @@ package org.grails.plugins import grails.boot.config.GrailsAutoConfiguration -import grails.core.DefaultGrailsApplication import grails.core.GrailsApplication import grails.plugins.GrailsPlugin import grails.plugins.GrailsPluginManager import groovy.transform.CompileStatic -import org.grails.plugins.DefaultGrailsPlugin -import org.grails.plugins.MockGrailsPluginManager import org.springframework.context.annotation.Bean import org.springframework.context.annotation.Configuration import org.springframework.core.io.FileSystemResource @@ -24,17 +21,17 @@ class GrailsPluginConfigurationClass extends GrailsAutoConfiguration { @Bean(name = "grailsPluginManager") GrailsPluginManager getGrailsPluginManager() { - String tempDir = System.getProperty("java.io.tmpdir") - GrailsApplication grailsApplication = new DefaultGrailsApplication() - final MockGrailsPluginManager pluginManager = new MockGrailsPluginManager(grailsApplication) - final List plugins = createGrailsPlugins(grailsApplication) - plugins.forEach({ plugin -> pluginManager.registerMockPlugin((GrailsPlugin) plugin)}) - return pluginManager + MockGrailsPluginManager pluginManager = new MockGrailsPluginManager() + createGrailsPlugins(pluginManager.application).each { + pluginManager.registerMockPlugin(it) + } + pluginManager } - private List createGrailsPlugins(DefaultGrailsApplication grailsApplication) { + private List createGrailsPlugins(GrailsApplication grailsApplication) { final String grailsVersion = '4.0.1' def gcl = new GroovyClassLoader() + GrailsPlugin plugin = new MockTestGrailsPlugin(gcl.parseClass("""class TestGrailsPlugin { def version = '1.0.0' def grailsVersion = '$grailsVersion' @@ -45,7 +42,8 @@ class GrailsPluginConfigurationClass extends GrailsAutoConfiguration { def version = '1.0.0' def grailsVersion = '$grailsVersion' }"""), grailsApplication) - [plugin, plugin2] + + List.of(plugin, plugin2) } class MockTestGrailsPlugin extends DefaultGrailsPlugin { @@ -82,10 +80,6 @@ class GrailsPluginConfigurationClass extends GrailsAutoConfiguration { class MockTestTwoGrailsPlugin extends DefaultGrailsPlugin { - MockTestTwoGrailsPlugin(Class pluginClass, Resource resource, GrailsApplication application) { - super(pluginClass, resource, application) - } - MockTestTwoGrailsPlugin(Class pluginClass, GrailsApplication application) { super(pluginClass, application) }