From 36df0c7e2a6bd0985260add0361e9175dcdb5db6 Mon Sep 17 00:00:00 2001 From: Michael Yan Date: Fri, 27 Sep 2024 14:20:51 +0800 Subject: [PATCH] Make `grailsResourceLocator` Bean Auto-Configure Closes gh-605 --- .../grails/plugins/core/CoreConfiguration.java | 18 +++++++++++++++++- .../plugins/core/CoreGrailsPlugin.groovy | 6 +----- .../web/GroovyPagesAutoConfiguration.java | 6 +++--- 3 files changed, 21 insertions(+), 9 deletions(-) diff --git a/grace-plugin-core/src/main/groovy/org/grails/plugins/core/CoreConfiguration.java b/grace-plugin-core/src/main/groovy/org/grails/plugins/core/CoreConfiguration.java index 8b58979e2b..b41534a96f 100644 --- a/grace-plugin-core/src/main/groovy/org/grails/plugins/core/CoreConfiguration.java +++ b/grace-plugin-core/src/main/groovy/org/grails/plugins/core/CoreConfiguration.java @@ -1,5 +1,5 @@ /* - * Copyright 2004-2023 the original author or authors. + * Copyright 2004-2024 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,15 +15,21 @@ */ package org.grails.plugins.core; +import java.io.IOException; +import java.util.List; + import org.springframework.beans.factory.ObjectProvider; import org.springframework.boot.autoconfigure.AutoConfiguration; import org.springframework.boot.autoconfigure.AutoConfigureOrder; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Primary; +import org.springframework.core.annotation.Order; import grails.config.ConfigProperties; import grails.core.GrailsApplication; +import grails.util.BuildSettings; +import org.grails.core.io.DefaultResourceLocator; /** * Core Auto-Configuration. @@ -50,4 +56,14 @@ public ConfigProperties grailsConfigProperties(ObjectProvider return new ConfigProperties(grailsApplication.getIfAvailable().getConfig()); } + @Bean + @Order(0) + @ConditionalOnMissingBean + public DefaultResourceLocator grailsResourceLocator() throws IOException { + DefaultResourceLocator defaultResourceLocator = new DefaultResourceLocator(); + defaultResourceLocator.setSearchLocations(List.of(BuildSettings.BASE_DIR.getCanonicalPath())); + + return defaultResourceLocator; + } + } diff --git a/grace-plugin-core/src/main/groovy/org/grails/plugins/core/CoreGrailsPlugin.groovy b/grace-plugin-core/src/main/groovy/org/grails/plugins/core/CoreGrailsPlugin.groovy index 307daf429a..458dfdecb2 100644 --- a/grace-plugin-core/src/main/groovy/org/grails/plugins/core/CoreGrailsPlugin.groovy +++ b/grace-plugin-core/src/main/groovy/org/grails/plugins/core/CoreGrailsPlugin.groovy @@ -1,5 +1,5 @@ /* - * Copyright 2004-2023 the original author or authors. + * Copyright 2004-2024 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. @@ -35,7 +35,6 @@ import grails.util.Environment import grails.util.GrailsUtil import org.grails.beans.support.PropertiesEditor -import org.grails.core.io.DefaultResourceLocator import org.grails.core.support.ClassEditor import org.grails.dev.support.DevelopmentShutdownHook import org.grails.spring.DefaultRuntimeSpringConfiguration @@ -113,9 +112,6 @@ class CoreGrailsPlugin extends Plugin implements PriorityOrdered { abstractGrailsResourceLocator { searchLocations = [BuildSettings.BASE_DIR.absolutePath] } - grailsResourceLocator(DefaultResourceLocator) { bean -> - bean.parent = 'abstractGrailsResourceLocator' - } customEditors(CustomEditorConfigurer) { customEditors = [(Class): ClassEditor, diff --git a/grace-plugin-gsp/src/main/groovy/org/grails/plugins/web/GroovyPagesAutoConfiguration.java b/grace-plugin-gsp/src/main/groovy/org/grails/plugins/web/GroovyPagesAutoConfiguration.java index 5f04d7af51..c8f23726b0 100644 --- a/grace-plugin-gsp/src/main/groovy/org/grails/plugins/web/GroovyPagesAutoConfiguration.java +++ b/grace-plugin-gsp/src/main/groovy/org/grails/plugins/web/GroovyPagesAutoConfiguration.java @@ -21,7 +21,6 @@ import java.util.Map; import org.springframework.beans.factory.ObjectProvider; -import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.config.PropertiesFactoryBean; import org.springframework.boot.autoconfigure.AutoConfiguration; import org.springframework.boot.autoconfigure.AutoConfigureOrder; @@ -156,6 +155,7 @@ public CachingGrailsConventionGroovyPageLocator groovyPageLocator(ObjectProvider } @Bean + @Order(-20) @Primary @ConditionalOnMissingBean public ResourceLocator grailsResourceLocator(ObjectProvider grailsApplication) { @@ -177,8 +177,8 @@ public ResourceLocator grailsResourceLocator(ObjectProvider g @Bean @ConditionalOnMissingBean - public ErrorsViewStackTracePrinter errorsViewStackTracePrinter(@Qualifier("grailsResourceLocator") ResourceLocator grailsResourceLocator) { - return new ErrorsViewStackTracePrinter(grailsResourceLocator); + public ErrorsViewStackTracePrinter errorsViewStackTracePrinter(ObjectProvider grailsResourceLocator) { + return new ErrorsViewStackTracePrinter(grailsResourceLocator.getObject()); } @Bean