Skip to content

Commit 196bb6f

Browse files
committed
Support for shared GroovyClassLoader in GroovyScriptFactory
Exposes setClassLoader method in ConfigurableApplicationContext interface as obvious first-class configuration option. Closes gh-25177
1 parent ad5710c commit 196bb6f

File tree

3 files changed

+27
-9
lines changed

3 files changed

+27
-9
lines changed

spring-context/src/main/java/org/springframework/context/ConfigurableApplicationContext.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -147,6 +147,15 @@ public interface ConfigurableApplicationContext extends ApplicationContext, Life
147147
*/
148148
void addApplicationListener(ApplicationListener<?> listener);
149149

150+
/**
151+
* Specify the ClassLoader to load class path resources and bean classes with.
152+
* <p>This context class loader will be passed to the internal bean factory.
153+
* @since 5.2.7
154+
* @see org.springframework.core.io.DefaultResourceLoader#DefaultResourceLoader(ClassLoader)
155+
* @see org.springframework.beans.factory.config.ConfigurableBeanFactory#setBeanClassLoader
156+
*/
157+
void setClassLoader(ClassLoader classLoader);
158+
150159
/**
151160
* Register the given protocol resolver with this application context,
152161
* allowing for additional resource protocols to be handled.

spring-context/src/main/java/org/springframework/scripting/groovy/GroovyScriptFactory.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -158,7 +158,14 @@ public void setBeanFactory(BeanFactory beanFactory) {
158158

159159
@Override
160160
public void setBeanClassLoader(ClassLoader classLoader) {
161-
this.groovyClassLoader = buildGroovyClassLoader(classLoader);
161+
if (classLoader instanceof GroovyClassLoader &&
162+
(this.compilerConfiguration == null ||
163+
((GroovyClassLoader) classLoader).hasCompatibleConfiguration(this.compilerConfiguration))) {
164+
this.groovyClassLoader = (GroovyClassLoader) classLoader;
165+
}
166+
else {
167+
this.groovyClassLoader = buildGroovyClassLoader(classLoader);
168+
}
162169
}
163170

164171
/**

src/docs/asciidoc/languages/dynamic-languages.adoc

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -159,9 +159,8 @@ The steps involved in using dynamic-language-backed beans are as follows:
159159
element in the XML configuration (you can define such beans programmatically by
160160
using the Spring API, although you will have to consult the source code for
161161
directions on how to do this, as this chapter does not cover this type of advanced configuration).
162-
Note that this is an iterative step. You need at least one bean
163-
definition for each dynamic language source file (although multiple bean definitions can reference the same dynamic language source
164-
file).
162+
Note that this is an iterative step. You need at least one bean definition for each dynamic
163+
language source file (although multiple bean definitions can reference the same source file).
165164

166165
The first two steps (testing and writing your dynamic language source files) are beyond
167166
the scope of this chapter. See the language specification and reference manual
@@ -578,9 +577,12 @@ If you do not use the Spring namespace support, you can still use the
578577
<bean class="org.springframework.scripting.support.ScriptFactoryPostProcessor"/>
579578
----
580579

581-
NOTE: As of Spring Framework 4.3.3, you may also specify a Groovy `CompilationCustomizer`
582-
(such as an `ImportCustomizer`) or even a full Groovy `CompilerConfiguration` object
583-
in the same place as Spring's `GroovyObjectCustomizer`.
580+
NOTE: You may also specify a Groovy `CompilationCustomizer` (such as an `ImportCustomizer`)
581+
or even a full Groovy `CompilerConfiguration` object in the same place as Spring's
582+
`GroovyObjectCustomizer`. Furthermore, you may set a common `GroovyClassLoader` with custom
583+
configuration for your beans at the `ConfigurableApplicationContext.setClassLoader` level;
584+
this also leads to shared `GroovyClassLoader` usage and is therefore recommendable in case of
585+
a large number of scripted beans (avoiding an isolated `GroovyClassLoader` instance per bean).
584586

585587

586588

0 commit comments

Comments
 (0)