Skip to content

Commit

Permalink
register autoconfigurations dynamically
Browse files Browse the repository at this point in the history
  • Loading branch information
codeconsole committed Nov 22, 2024
1 parent 9938408 commit 6a5a7db
Showing 1 changed file with 11 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 6a5a7db

Please sign in to comment.