diff --git a/spring-context/src/main/java/org/springframework/context/ConfigurableApplicationContext.java b/spring-context/src/main/java/org/springframework/context/ConfigurableApplicationContext.java index 2038e662ca6a..5a31673e5123 100644 --- a/spring-context/src/main/java/org/springframework/context/ConfigurableApplicationContext.java +++ b/spring-context/src/main/java/org/springframework/context/ConfigurableApplicationContext.java @@ -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 */ void addApplicationListener(ApplicationListener listener); + /** + * Specify the ClassLoader to load class path resources and bean classes with. + *

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. diff --git a/spring-context/src/main/java/org/springframework/scripting/groovy/GroovyScriptFactory.java b/spring-context/src/main/java/org/springframework/scripting/groovy/GroovyScriptFactory.java index 9e2871bf0f5f..90f812e08dfe 100644 --- a/spring-context/src/main/java/org/springframework/scripting/groovy/GroovyScriptFactory.java +++ b/spring-context/src/main/java/org/springframework/scripting/groovy/GroovyScriptFactory.java @@ -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 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); + } } /** diff --git a/src/docs/asciidoc/languages/dynamic-languages.adoc b/src/docs/asciidoc/languages/dynamic-languages.adoc index cae5239b0602..b255356df28b 100644 --- a/src/docs/asciidoc/languages/dynamic-languages.adoc +++ b/src/docs/asciidoc/languages/dynamic-languages.adoc @@ -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 ---- -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).