-
-
Notifications
You must be signed in to change notification settings - Fork 974
Add method-based doWithSpring(BeanBuilder) plugin lifecycle hook #15939
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
jdaugherty
merged 3 commits into
apache:8.0.x
from
codeconsole:feat/plugin-dowithspring-method
Jul 9, 2026
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
153 changes: 153 additions & 0 deletions
153
grails-core/src/test/groovy/grails/plugins/PluginDoWithSpringMethodSpec.groovy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,153 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
| package grails.plugins | ||
|
|
||
| import grails.core.DefaultGrailsApplication | ||
| import grails.plugins.exceptions.PluginException | ||
| import grails.spring.BeanBuilder | ||
| import org.grails.plugins.DefaultGrailsPlugin | ||
| import org.grails.spring.DefaultRuntimeSpringConfiguration | ||
| import org.grails.spring.RuntimeSpringConfiguration | ||
| import org.springframework.beans.propertyeditors.ClassEditor | ||
| import spock.lang.Specification | ||
|
|
||
| /** | ||
| * Tests the method-based {@link Plugin#doWithSpring(BeanBuilder)} hook that complements the closure-returning | ||
| * {@link Plugin#doWithSpring()} hook. | ||
| */ | ||
| class PluginDoWithSpringMethodSpec extends Specification { | ||
|
|
||
| void "a Plugin subclass can register beans by overriding doWithSpring(BeanBuilder)"() { | ||
| given: "a plugin that registers a bean through the method-based hook" | ||
| def app = new DefaultGrailsApplication() | ||
| def plugin = new MethodHookPlugin(grailsApplication: app) | ||
| def bb = new BeanBuilder() | ||
|
|
||
| when: "the method-based hook is invoked with the builder" | ||
| plugin.doWithSpring(bb) | ||
| def ctx = bb.createApplicationContext() | ||
|
|
||
| then: "the bean registered against the builder is present" | ||
| ctx.containsBean('methodEditor') | ||
| } | ||
|
|
||
| void "the base Plugin doWithSpring(BeanBuilder) is a no-op"() { | ||
| given: "a plugin that overrides neither hook" | ||
| def app = new DefaultGrailsApplication() | ||
| def plugin = new NoOpPlugin(grailsApplication: app) | ||
| def bb = new BeanBuilder() | ||
|
|
||
| when: "the method-based hook is invoked" | ||
| plugin.doWithSpring(bb) | ||
| def ctx = bb.createApplicationContext() | ||
|
|
||
| then: "no beans are registered by the plugin and the closure hook still returns null" | ||
| plugin.doWithSpring() == null | ||
| ctx.beanDefinitionNames.length == 0 | ||
| } | ||
|
|
||
| void "DefaultGrailsPlugin invokes the method-based doWithSpring(BeanBuilder) hook"() { | ||
| given: "a plugin descriptor that uses only the method-based hook" | ||
| def app = new DefaultGrailsApplication() | ||
| def plugin = new DefaultGrailsPlugin(MethodHookGrailsPlugin, app) | ||
| RuntimeSpringConfiguration springConfig = new DefaultRuntimeSpringConfiguration() | ||
|
|
||
| when: "runtime configuration is performed" | ||
| plugin.doWithRuntimeConfiguration(springConfig) | ||
| def ctx = springConfig.applicationContext | ||
|
|
||
| then: "the bean registered via the method hook is present" | ||
| ctx.containsBean('methodEditor') | ||
| } | ||
|
|
||
| void "DefaultGrailsPlugin throws when a plugin defines both doWithSpring hooks"() { | ||
| given: "a plugin descriptor that overrides both hooks" | ||
| def app = new DefaultGrailsApplication() | ||
| def plugin = new DefaultGrailsPlugin(BothHooksGrailsPlugin, app) | ||
| RuntimeSpringConfiguration springConfig = new DefaultRuntimeSpringConfiguration() | ||
|
|
||
| when: "runtime configuration is performed" | ||
| plugin.doWithRuntimeConfiguration(springConfig) | ||
|
|
||
| then: "a PluginException is thrown identifying the conflicting hooks" | ||
| PluginException e = thrown() | ||
| e.message.contains('doWithSpring()') | ||
| e.message.contains('doWithSpring(BeanBuilder)') | ||
| } | ||
|
|
||
| void "DefaultGrailsPlugin still supports the legacy closure-returning doWithSpring hook"() { | ||
| given: "a plugin descriptor that overrides only the closure-returning hook" | ||
| def app = new DefaultGrailsApplication() | ||
| def plugin = new DefaultGrailsPlugin(LegacyClosureGrailsPlugin, app) | ||
| RuntimeSpringConfiguration springConfig = new DefaultRuntimeSpringConfiguration() | ||
|
|
||
| when: "runtime configuration is performed" | ||
| plugin.doWithRuntimeConfiguration(springConfig) | ||
| def ctx = springConfig.applicationContext | ||
|
|
||
| then: "the bean registered via the closure hook is present" | ||
| ctx.containsBean('classEditor') | ||
| } | ||
| } | ||
|
|
||
| class MethodHookPlugin extends Plugin { | ||
| @Override | ||
| void doWithSpring(BeanBuilder beans) { | ||
| beans.methodEditor(ClassEditor, grailsApplication.classLoader) | ||
| } | ||
| } | ||
|
|
||
| class NoOpPlugin extends Plugin { | ||
| } | ||
|
|
||
| class MethodHookGrailsPlugin extends Plugin { | ||
| def version = '1.0' | ||
|
|
||
| @Override | ||
| void doWithSpring(BeanBuilder beans) { | ||
| beans.methodEditor(ClassEditor, grailsApplication.classLoader) | ||
| } | ||
| } | ||
|
|
||
| class BothHooksGrailsPlugin extends Plugin { | ||
| def version = '1.0' | ||
|
|
||
| @Override | ||
| Closure doWithSpring() { | ||
| { -> | ||
| classEditor(ClassEditor, application.classLoader) | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| void doWithSpring(BeanBuilder beans) { | ||
| beans.customEditor(ClassEditor, grailsApplication.classLoader) | ||
| } | ||
| } | ||
|
|
||
| class LegacyClosureGrailsPlugin extends Plugin { | ||
| def version = '1.0' | ||
|
|
||
| @Override | ||
| Closure doWithSpring() { | ||
| { -> | ||
| classEditor(ClassEditor, application.classLoader) | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.