From 6a5a7db1d80b4b3d31a1b36e5666426c70a12aa8 Mon Sep 17 00:00:00 2001 From: Scott Murphy Heiberg Date: Thu, 21 Nov 2024 21:44:40 -0800 Subject: [PATCH 1/3] register autoconfigurations dynamically --- .../testing/GrailsApplicationBuilder.groovy | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/grails-testing-support/src/main/groovy/org/grails/testing/GrailsApplicationBuilder.groovy b/grails-testing-support/src/main/groovy/org/grails/testing/GrailsApplicationBuilder.groovy index 1c8566e6..ae83fa84 100644 --- a/grails-testing-support/src/main/groovy/org/grails/testing/GrailsApplicationBuilder.groovy +++ b/grails-testing-support/src/main/groovy/org/grails/testing/GrailsApplicationBuilder.groovy @@ -27,6 +27,8 @@ import org.springframework.beans.factory.config.ConstructorArgumentValues import org.springframework.beans.factory.support.BeanDefinitionRegistry import org.springframework.beans.factory.support.DefaultListableBeanFactory import org.springframework.beans.factory.support.RootBeanDefinition +import org.springframework.boot.autoconfigure.AutoConfiguration +import org.springframework.boot.context.annotation.ImportCandidates import org.springframework.boot.test.context.ConfigDataApplicationContextInitializer import org.springframework.boot.web.servlet.context.AnnotationConfigServletWebApplicationContext import org.springframework.context.ConfigurableApplicationContext @@ -107,20 +109,25 @@ class GrailsApplicationBuilder { return context } + @CompileDynamic protected ConfigurableApplicationContext createMainContext(Object servletContext) { ConfigurableApplicationContext context - if (isServletApiPresent && servletContext != null) { context = (AnnotationConfigServletWebApplicationContext) ClassUtils.forName('org.springframework.boot.web.servlet.context.AnnotationConfigServletWebApplicationContext').getDeclaredConstructor().newInstance() ((AnnotationConfigServletWebApplicationContext) context).setServletContext((ServletContext) servletContext) } else { context = (ConfigurableApplicationContext) ClassUtils.forName('org.springframework.context.annotation.AnnotationConfigApplicationContext').getDeclaredConstructor().newInstance() } - ((AnnotationConfigRegistry) context).register(CoreConfiguration, CodecsConfiguration, DataBindingConfiguration, MimeTypesConfiguration) + def classLoader = this.class.classLoader + def autoConfigurationClasses = ImportCandidates.load(AutoConfiguration, classLoader) + .asList() + .findAll { it.startsWith("org.grails") } + .collect { ClassUtils.forName(it, classLoader) } + if (!autoConfigurationClasses.isEmpty()) { + ((AnnotationConfigRegistry) context).register(*autoConfigurationClasses as Class[]) + } - def applicationClassLoader = this.class.classLoader - def configuredEnvironment = context.getEnvironment() def beanFactory = context.getBeanFactory() (beanFactory as DefaultListableBeanFactory).with { setAllowBeanDefinitionOverriding(true) From d97a0bf19f12d80997bd44632b552d87bf784209 Mon Sep 17 00:00:00 2001 From: Scott Murphy Heiberg Date: Thu, 21 Nov 2024 22:12:17 -0800 Subject: [PATCH 2/3] Fix tests --- .../testing/GrailsApplicationBuilder.groovy | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/grails-testing-support/src/main/groovy/org/grails/testing/GrailsApplicationBuilder.groovy b/grails-testing-support/src/main/groovy/org/grails/testing/GrailsApplicationBuilder.groovy index ae83fa84..821712dd 100644 --- a/grails-testing-support/src/main/groovy/org/grails/testing/GrailsApplicationBuilder.groovy +++ b/grails-testing-support/src/main/groovy/org/grails/testing/GrailsApplicationBuilder.groovy @@ -109,9 +109,7 @@ class GrailsApplicationBuilder { return context } - @CompileDynamic protected ConfigurableApplicationContext createMainContext(Object servletContext) { - ConfigurableApplicationContext context if (isServletApiPresent && servletContext != null) { context = (AnnotationConfigServletWebApplicationContext) ClassUtils.forName('org.springframework.boot.web.servlet.context.AnnotationConfigServletWebApplicationContext').getDeclaredConstructor().newInstance() @@ -119,13 +117,13 @@ class GrailsApplicationBuilder { } else { context = (ConfigurableApplicationContext) ClassUtils.forName('org.springframework.context.annotation.AnnotationConfigApplicationContext').getDeclaredConstructor().newInstance() } + def classLoader = this.class.classLoader - def autoConfigurationClasses = ImportCandidates.load(AutoConfiguration, classLoader) - .asList() - .findAll { it.startsWith("org.grails") } - .collect { ClassUtils.forName(it, classLoader) } - if (!autoConfigurationClasses.isEmpty()) { - ((AnnotationConfigRegistry) context).register(*autoConfigurationClasses as Class[]) + ImportCandidates.load(AutoConfiguration, classLoader).asList().findAll { + it.startsWith("org.grails") + && !it.contains("UrlMappingsAutoConfiguration") // this currently is causing an issue with tests + }.each { + ((AnnotationConfigRegistry) context).register(ClassUtils.forName(it, classLoader)) } def beanFactory = context.getBeanFactory() @@ -136,7 +134,6 @@ class GrailsApplicationBuilder { prepareContext(context, beanFactory) context.refresh() context.registerShutdownHook() - return context } From 8e3f5a7cde5bd1e48011b8c0436534da9a7b0912 Mon Sep 17 00:00:00 2001 From: Scott Murphy Heiberg Date: Thu, 21 Nov 2024 22:17:29 -0800 Subject: [PATCH 3/3] Remove unused imports --- .../org/grails/testing/GrailsApplicationBuilder.groovy | 9 --------- 1 file changed, 9 deletions(-) diff --git a/grails-testing-support/src/main/groovy/org/grails/testing/GrailsApplicationBuilder.groovy b/grails-testing-support/src/main/groovy/org/grails/testing/GrailsApplicationBuilder.groovy index 821712dd..9cfb50bc 100644 --- a/grails-testing-support/src/main/groovy/org/grails/testing/GrailsApplicationBuilder.groovy +++ b/grails-testing-support/src/main/groovy/org/grails/testing/GrailsApplicationBuilder.groovy @@ -1,7 +1,6 @@ package org.grails.testing import grails.boot.config.GrailsApplicationPostProcessor -import grails.boot.config.GrailsAutoConfiguration import grails.core.GrailsApplication import grails.core.GrailsApplicationLifeCycle import grails.core.support.proxy.DefaultProxyHandler @@ -12,10 +11,6 @@ import groovy.transform.CompileDynamic import groovy.transform.CompileStatic import jakarta.servlet.ServletContext import org.grails.plugins.IncludingPluginFilter -import org.grails.plugins.codecs.CodecsConfiguration -import org.grails.plugins.core.CoreConfiguration -import org.grails.plugins.databinding.DataBindingConfiguration -import org.grails.plugins.web.mime.MimeTypesConfiguration import org.grails.spring.context.support.GrailsPlaceholderConfigurer import org.grails.spring.context.support.MapBasedSmartPropertyOverrideConfigurer import org.grails.transaction.TransactionManagerPostProcessor @@ -38,10 +33,6 @@ import org.springframework.context.support.ConversionServiceFactoryBean import org.springframework.context.support.PropertySourcesPlaceholderConfigurer import org.springframework.context.support.StaticMessageSource import org.springframework.core.Ordered -import org.springframework.core.convert.ConversionService -import org.springframework.core.env.ConfigurableEnvironment -import org.springframework.core.env.Environment -import org.springframework.core.env.PropertyResolver import org.springframework.util.ClassUtils /**