Browse Source

Support for shared GroovyClassLoader in GroovyScriptFactory

Exposes setClassLoader method in ConfigurableApplicationContext interface as obvious first-class configuration option.

Closes gh-25177
pull/25758/head
Juergen Hoeller 6 years ago
parent
commit
196bb6fe32
  1. 11
      spring-context/src/main/java/org/springframework/context/ConfigurableApplicationContext.java
  2. 11
      spring-context/src/main/java/org/springframework/scripting/groovy/GroovyScriptFactory.java
  3. 14
      src/docs/asciidoc/languages/dynamic-languages.adoc

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

@ -1,5 +1,5 @@ @@ -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.
@ -147,6 +147,15 @@ public interface ConfigurableApplicationContext extends ApplicationContext, Life @@ -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.

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

@ -1,5 +1,5 @@ @@ -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.
@ -158,7 +158,14 @@ public class GroovyScriptFactory implements ScriptFactory, BeanFactoryAware, Bea @@ -158,7 +158,14 @@ public class GroovyScriptFactory implements ScriptFactory, BeanFactoryAware, Bea
@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);
}
}
/**

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

@ -159,9 +159,8 @@ The steps involved in using dynamic-language-backed beans are as follows: @@ -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
@ -578,9 +577,12 @@ If you do not use the Spring namespace support, you can still use the @@ -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).

Loading…
Cancel
Save