Skip to content

Commit

Permalink
Support for shared GroovyClassLoader in GroovyScriptFactory
Browse files Browse the repository at this point in the history
Exposes setClassLoader method in ConfigurableApplicationContext interface as obvious first-class configuration option.

Closes gh-25177
  • Loading branch information
jhoeller committed Jun 6, 2020
1 parent ad5710c commit 196bb6f
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 9 deletions.
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2020 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.
Expand Down Expand Up @@ -147,6 +147,15 @@ public interface ConfigurableApplicationContext extends ApplicationContext, Life
*/
void addApplicationListener(ApplicationListener<?> listener);

/**
* Specify the ClassLoader to load class path resources and bean classes with.
* <p>This context class loader will be passed to the internal bean factory.
* @since 5.2.7
* @see org.springframework.core.io.DefaultResourceLoader#DefaultResourceLoader(ClassLoader)
* @see org.springframework.beans.factory.config.ConfigurableBeanFactory#setBeanClassLoader
*/
void setClassLoader(ClassLoader classLoader);

/**
* Register the given protocol resolver with this application context,
* allowing for additional resource protocols to be handled.
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2020 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.
Expand Down Expand Up @@ -158,7 +158,14 @@ public void setBeanFactory(BeanFactory beanFactory) {

@Override
public void setBeanClassLoader(ClassLoader classLoader) {
this.groovyClassLoader = buildGroovyClassLoader(classLoader);
if (classLoader instanceof GroovyClassLoader &&
(this.compilerConfiguration == null ||
((GroovyClassLoader) classLoader).hasCompatibleConfiguration(this.compilerConfiguration))) {
this.groovyClassLoader = (GroovyClassLoader) classLoader;
}
else {
this.groovyClassLoader = buildGroovyClassLoader(classLoader);
}
}

/**
Expand Down
14 changes: 8 additions & 6 deletions src/docs/asciidoc/languages/dynamic-languages.adoc
Expand Up @@ -159,9 +159,8 @@ The steps involved in using dynamic-language-backed beans are as follows:
element in the XML configuration (you can define such beans programmatically by
using the Spring API, although you will have to consult the source code for
directions on how to do this, as this chapter does not cover this type of advanced configuration).
Note that this is an iterative step. You need at least one bean
definition for each dynamic language source file (although multiple bean definitions can reference the same dynamic language source
file).
Note that this is an iterative step. You need at least one bean definition for each dynamic
language source file (although multiple bean definitions can reference the same source file).

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

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



Expand Down

0 comments on commit 196bb6f

Please sign in to comment.