From 3eba7088c39747b15bf8c2ccd4e4a214199ad669 Mon Sep 17 00:00:00 2001 From: Michael Yan Date: Mon, 22 Jan 2024 23:53:47 +0800 Subject: [PATCH] Fixes Deploy Grails app to tomcat webapps error Closes gh-37 --- .../main/groovy/grails/boot/GrailsBuilder.java | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/grace-boot/src/main/groovy/grails/boot/GrailsBuilder.java b/grace-boot/src/main/groovy/grails/boot/GrailsBuilder.java index 6db405c8a8..71358b2b04 100644 --- a/grace-boot/src/main/groovy/grails/boot/GrailsBuilder.java +++ b/grace-boot/src/main/groovy/grails/boot/GrailsBuilder.java @@ -1,5 +1,5 @@ /* - * Copyright 2015-2022 the original author or authors. + * Copyright 2015-2023 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -15,22 +15,24 @@ */ package grails.boot; -import io.micronaut.spring.context.MicronautApplicationContext; import org.springframework.boot.SpringApplication; import org.springframework.boot.builder.SpringApplicationBuilder; +import org.springframework.context.ConfigurableApplicationContext; import org.springframework.core.io.ResourceLoader; import org.springframework.util.ClassUtils; +import org.springframework.util.ReflectionUtils; /** * Fluent API for constructing Grails instances. * Simple extension of {@link SpringApplicationBuilder}. * * @author Graeme Rocher + * @author Michael Yan * @since 3.0.6 */ public class GrailsBuilder extends SpringApplicationBuilder { - private MicronautApplicationContext micronautContext; + private ConfigurableApplicationContext micronautContext; public GrailsBuilder(Class... sources) { super(sources); @@ -43,7 +45,14 @@ protected SpringApplication createSpringApplication(ResourceLoader resourceLoade public GrailsBuilder enableMicronaut() { if (ClassUtils.isPresent("io.micronaut.spring.context.MicronautApplicationContext", getClass().getClassLoader())) { - this.micronautContext = new MicronautApplicationContext(); + try { + Class clazz = getClass().getClassLoader().loadClass("io.micronaut.spring.context.MicronautApplicationContext"); + this.micronautContext = (ConfigurableApplicationContext) ReflectionUtils.accessibleConstructor(clazz).newInstance(); + } + catch (Exception e) { + throw new IllegalStateException( + "Can't enable Micronaut as the parent application context of Grails", e); + } } else { throw new IllegalStateException(