Spring 为使用已 通过使用 Spring 的动态语言(如 Groovy)来定义。这种支持让 你用支持的动态语言编写任意数量的类,并拥有 Spring 容器透明地实例化、配置和依赖注入生成的 对象。Spring中文文档

Spring 的脚本支持主要针对 Groovy 和 BeanShell。除此之外 特别支持的语言,支持 JSR-223 脚本机制 用于与任何支持 JSR-223 的语言提供程序集成(从 Spring 4.2 开始), 例如:JRuby。Spring中文文档

您可以找到有关此动态语言支持的完整工作示例 在方案中立即有用。Spring中文文档

第一个例子

本章的大部分内容涉及描述动态语言支持 细节。在深入了解动态语言支持的所有细节之前, 我们看一个用动态语言定义的 Bean 的简单示例。动态 第一个 Bean 的语言是 Groovy。(此示例的基础取自 春季测试套件。如果您想在任何其他示例中看到等效示例 支持的语言,请查看源代码)。Spring中文文档

下一个示例显示了 Groovy bean 将要访问的接口 实现。请注意,此接口是用纯 Java 定义的。依赖对象 注入了对不知道底层的引用 实现是一个 Groovy 脚本。以下列表显示了该接口:MessengerMessengerMessengerSpring中文文档

package org.springframework.scripting;

public interface Messenger {

	String getMessage();
}

下面的示例定义一个依赖于接口的类:MessengerSpring中文文档

package org.springframework.scripting;

public class DefaultBookingService implements BookingService {

	private Messenger messenger;

	public void setMessenger(Messenger messenger) {
		this.messenger = messenger;
	}

	public void processBooking() {
		// use the injected Messenger object...
	}
}

以下示例在 Groovy 中实现接口:MessengerSpring中文文档

package org.springframework.scripting.groovy

// Import the Messenger interface (written in Java) that is to be implemented
import org.springframework.scripting.Messenger

// Define the implementation in Groovy in file 'Messenger.groovy'
class GroovyMessenger implements Messenger {

	String message
}

要使用自定义动态语言标签来定义动态语言支持的 Bean,您需要 需要在Spring XML配置文件的顶部有XML模式前导码。 您还需要使用 Spring 实现作为 IoC 容器。支持使用具有简单实现的动态语言支持的 bean,但您必须管理 Spring 内部的管道 这样做。ApplicationContextBeanFactorySpring中文文档

有关基于架构的配置的更多信息,请参 阅XML 基于架构的配置Spring中文文档

最后,下面的示例显示了影响注入 Groovy 定义的实现到类的实例中:MessengerDefaultBookingServiceSpring中文文档

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:lang="http://www.springframework.org/schema/lang"
	xsi:schemaLocation="
		http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/lang https://www.springframework.org/schema/lang/spring-lang.xsd">

	<!-- this is the bean definition for the Groovy-backed Messenger implementation -->
	<lang:groovy id="messenger" script-source="classpath:Messenger.groovy">
		<lang:property name="message" value="I Can Do The Frug" />
	</lang:groovy>

	<!-- an otherwise normal bean that will be injected by the Groovy-backed Messenger -->
	<bean id="bookingService" class="x.y.DefaultBookingService">
		<property name="messenger" ref="messenger" />
	</bean>

</beans>

bean (a ) 现在可以正常使用其私有成员变量,因为注入其中的实例是 一个实例。这里没有什么特别的事情发生——只是普通的 Java 和 普通的时髦。bookingServiceDefaultBookingServicemessengerMessengerMessengerSpring中文文档

希望前面的 XML 代码段是不言自明的,但如果不是,请不要过分担心。 请继续阅读,详细了解上述配置的原因和原因。Spring中文文档

要使用自定义动态语言标签来定义动态语言支持的 Bean,您需要 需要在Spring XML配置文件的顶部有XML模式前导码。 您还需要使用 Spring 实现作为 IoC 容器。支持使用具有简单实现的动态语言支持的 bean,但您必须管理 Spring 内部的管道 这样做。ApplicationContextBeanFactorySpring中文文档

有关基于架构的配置的更多信息,请参 阅XML 基于架构的配置Spring中文文档

定义由动态语言支持的 Bean

本节将准确描述如何在任何 支持的动态语言。Spring中文文档

请注意,本章并不试图解释所支持的语法和习语 动态语言。例如,如果要使用 Groovy 编写某些类 在您的应用程序中,我们假设您已经了解 Groovy。如果您需要更多详细信息 关于动态语言本身,请参阅末尾的更多参考资料 本章。Spring中文文档

常见概念

使用动态语言支持的 Bean 所涉及的步骤如下:Spring中文文档

  1. (自然)编写动态语言源代码的测试。Spring中文文档

  2. 然后编写动态语言源代码本身。Spring中文文档

  3. 使用 XML 配置中的相应元素定义动态语言支持的 Bean(您可以通过以下方式以编程方式定义此类 Bean) 使用 Spring API,尽管您必须查阅源代码 有关如何执行此操作的说明,因为本章不介绍此类高级配置)。 请注意,这是一个迭代步骤。每个动态至少需要一个 Bean 定义 语言源文件(尽管多个 Bean 定义可以引用同一个源文件)。<lang:language/>Spring中文文档

前两个步骤(测试和编写动态语言源文件)超出了 本章的范围。请参阅语言规范和参考手册 为您选择的动态语言,并继续开发您的动态语言 源文件。不过,您首先要阅读本章的其余部分,因为 Spring 的动态语言支持确实对内容做出了一些(小的)假设 动态语言源文件。Spring中文文档

<lang:language/> 元素

上一节中列表中的最后一步涉及定义动态语言支持的 Bean 定义,每个 Bean 定义一个定义 想要配置(这与普通的 JavaBean 配置没有什么不同)。然而 而不是指定要成为的类的完全限定类名 由容器实例化和配置后,您可以使用该元素来定义动态语言支持的 Bean。<lang:language/>Spring中文文档

每种支持的语言都有相应的元素:<lang:language/>Spring中文文档

可用于配置的确切属性和子元素取决于 确切地说,Bean 是用哪种语言定义的(特定于语言的部分 本章后面将详细介绍这一点)。Spring中文文档

可刷新豆类

动态语言最引人注目的附加值之一(也许是唯一的) Spring 中的支持是“可刷新 Bean”功能。Spring中文文档

可刷新的 Bean 是动态语言支持的 Bean。用少量 配置中,动态语言支持的 Bean 可以监视其底层的变化 源文件资源,然后在动态语言源文件 已更改(例如,在文件系统上编辑和保存对文件的更改时)。Spring中文文档

这允许您部署任意数量的动态语言源文件作为 应用程序,配置 Spring 容器以创建动态支持的 Bean 语言源文件(使用本章中描述的机制)和(稍后 随着需求的变化或其他外部因素的出现)编辑动态 语言源文件,并使它们所做的任何更改都反映在 Bean 中 由更改的动态语言源文件提供支持。无需关闭 正在运行的应用程序(如果是 Web 应用程序,则重新部署)。这 动态语言支持的 Bean 如此修正从 更改了动态语言源文件。Spring中文文档

默认情况下,此功能处于关闭状态。

现在我们可以看一个例子,看看开始使用刷新是多么容易 豆。要打开可刷新的 Bean 功能,您必须指定一个 Bean 定义元素的附加属性。所以 如果我们坚持前面的例子 本章中,下面的示例显示了我们将在 Spring XML 中更改的内容 用于影响可刷新 Bean 的配置:<lang:language/>Spring中文文档

<beans>

	<!-- this bean is now 'refreshable' due to the presence of the 'refresh-check-delay' attribute -->
	<lang:groovy id="messenger"
			refresh-check-delay="5000" <!-- switches refreshing on with 5 seconds between checks -->
			script-source="classpath:Messenger.groovy">
		<lang:property name="message" value="I Can Do The Frug" />
	</lang:groovy>

	<bean id="bookingService" class="x.y.DefaultBookingService">
		<property name="messenger" ref="messenger" />
	</bean>

</beans>

这真的是你所要做的。Bean 定义上定义的属性是 Bean 的毫秒数 使用对基础动态语言源文件所做的任何更改进行刷新。 您可以通过为属性分配负值来关闭刷新行为。请记住,默认情况下,刷新行为为 禁用。如果不希望刷新行为,请不要定义该属性。refresh-check-delaymessengerrefresh-check-delaySpring中文文档

如果我们随后运行以下应用程序,我们可以执行可刷新功能。 (请原谅“跳过箍暂停执行”的恶作剧 在下一段代码中。调用仅在那里,以便 当您(此方案中的开发人员)关闭时,程序的执行将暂停 并编辑基础动态语言源文件,以便刷新触发 当程序恢复执行时,在动态语言支持的 Bean 上。System.in.read()Spring中文文档

以下列表显示了此示例应用程序:Spring中文文档

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.scripting.Messenger;

public final class Boot {

	public static void main(final String[] args) throws Exception {
		ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
		Messenger messenger = (Messenger) ctx.getBean("messenger");
		System.out.println(messenger.getMessage());
		// pause execution while I go off and make changes to the source file...
		System.in.read();
		System.out.println(messenger.getMessage());
	}
}

然后,就此示例而言,假设必须更改对实现方法的所有调用,以便消息 用引号括起来。以下列表显示了 (开发者)应该在源文件时 程序的执行暂停:getMessage()MessengerMessenger.groovySpring中文文档

package org.springframework.scripting

class GroovyMessenger implements Messenger {

	private String message = "Bingo"

	public String getMessage() {
		// change the implementation to surround the message in quotes
		return "'" + this.message + "'"
	}

	public void setMessage(String message) {
		this.message = message
	}
}

当程序运行时,输入暂停前的输出将为 。 对源文件进行更改并保存后,程序恢复执行, 在动态语言支持的实现上调用该方法的结果是(请注意,包含 附加引号)。I Can Do The FruggetMessage()Messenger'I Can Do The Frug'Spring中文文档

如果对脚本的更改发生在 值。直到对脚本的更改才真正被拾取 在动态语言支持的 Bean 上调用一个方法。只有当一个方法 调用一个动态语言支持的 Bean,它检查它的底层脚本是否 源已更改。与刷新脚本相关的任何异常(例如 遇到编译错误或发现脚本文件已被删除) 导致致命异常传播到调用代码。refresh-check-delaySpring中文文档

前面描述的可刷新 Bean 行为不适用于动态语言 使用元素表示法定义的源文件(请参阅内联动态语言源文件)。此外,它仅适用于以下情况的豆类 实际上可以检测到对基础源文件的更改(例如,通过代码 检查存在于 文件系统)。<lang:inline-script/>Spring中文文档

内联动态语言源文件

动态语言支持还可以满足动态语言源文件的需求,这些文件是 直接嵌入到 Spring Bean 定义中。更具体地说,该元素允许您立即定义动态语言源 在 Spring 配置文件中。一个示例可能会阐明内联脚本如何 特色作品:<lang:inline-script/>Spring中文文档

<lang:groovy id="messenger">
	<lang:inline-script>

		package org.springframework.scripting.groovy

		import org.springframework.scripting.Messenger

		class GroovyMessenger implements Messenger {
			String message
		}

	</lang:inline-script>
	<lang:property name="message" value="I Can Do The Frug" />
</lang:groovy>

如果我们把围绕定义是否是好做法的问题放在一边 动态语言源 在 Spring 配置文件中,该元素在某些情况下可能很有用。例如,我们可能想要快速添加一个 Spring MVC 的 Spring 实现。这只是一瞬间 使用内联源。(请参阅脚本验证器 示例。<lang:inline-script/>ValidatorControllerSpring中文文档

在动态语言支持的 Bean 上下文中理解构造函数注入

关于Spring的动态,有一件非常重要的事情需要注意 语言支持。也就是说,您(目前)不能提供构造函数参数 到动态语言支持的 Bean(因此,构造函数注入不可用于 动态语言支持的 Bean)。为了对以下事项进行特殊处理 构造函数和属性 100% 清除,以下代码和配置的混合 不起作用:Spring中文文档

一种行不通的方法
package org.springframework.scripting.groovy

import org.springframework.scripting.Messenger

// from the file 'Messenger.groovy'
class GroovyMessenger implements Messenger {

	GroovyMessenger() {}

	// this constructor is not available for Constructor Injection
	GroovyMessenger(String message) {
		this.message = message;
	}

	String message

	String anotherMessage
}
<lang:groovy id="badMessenger"
	script-source="classpath:Messenger.groovy">
	<!-- this next constructor argument will not be injected into the GroovyMessenger -->
	<!-- in fact, this isn't even allowed according to the schema -->
	<constructor-arg value="This will not work" />

	<!-- only property values are injected into the dynamic-language-backed object -->
	<lang:property name="anotherMessage" value="Passed straight through to the dynamic-language-backed object" />

</lang>

在实践中,这种限制并不像最初看起来那么重要,因为 setter 注入是绝大多数开发者青睐的注入方式 (我们把关于这是否是一件好事的讨论留到另一天)。Spring中文文档

时髦的豆子

本节介绍如何使用 Groovy in Spring 中定义的 bean。Spring中文文档

Groovy主页包括以下描述:Spring中文文档

“Groovy 是 Java 2 平台的敏捷动态语言,它具有许多 人们在 Python、Ruby 和 Smalltalk 等语言中非常喜欢的功能,使 它们可供使用类似 Java 语法的 Java 开发人员使用。Spring中文文档

如果你直接从顶部阅读了这一章,你已经看到了一个 Groovy 动态语言支持的例子 豆。现在考虑另一个示例(再次使用 Spring 测试套件中的示例):Spring中文文档

package org.springframework.scripting;

public interface Calculator {

	int add(int x, int y);
}

以下示例在 Groovy 中实现接口:CalculatorSpring中文文档

package org.springframework.scripting.groovy

// from the file 'calculator.groovy'
class GroovyCalculator implements Calculator {

	int add(int x, int y) {
		x + y
	}
}

以下 Bean 定义使用 Groovy 中定义的计算器:Spring中文文档

<!-- from the file 'beans.xml' -->
<beans>
	<lang:groovy id="calculator" script-source="classpath:calculator.groovy"/>
</beans>

最后,以下小型应用程序将执行上述配置:Spring中文文档

package org.springframework.scripting;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {

	public static void main(String[] args) {
		ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
		Calculator calc = ctx.getBean("calculator", Calculator.class);
		System.out.println(calc.add(2, 8));
	}
}

运行上述程序的结果输出是(不出所料)。 (有关更多有趣的示例,请参阅动态语言展示项目以获取更多信息 复杂示例或请参阅本章后面的示例方案)。10Spring中文文档

每个 Groovy 源文件定义的类不得超过一个。虽然这是完美的 在Groovy中是合法的,这(可以说)是一种不好的做法。为了一致的利益 方法,你应该(在 Spring 团队看来)尊重标准的 Java 每个源文件一个(公共)类的约定。Spring中文文档

使用回调自定义 Groovy 对象

该接口是一个回调,可让您挂钩其他 创建逻辑到创建 Groovy 支持的 Bean 的过程中。例如 此接口的实现可以调用任何所需的初始化方法, 设置一些默认属性值,或指定自定义 .以下列表 显示接口定义:GroovyObjectCustomizerMetaClassGroovyObjectCustomizerSpring中文文档

public interface GroovyObjectCustomizer {

	void customize(GroovyObject goo);
}

Spring Framework 实例化 Groovy 支持的 bean 的实例,然后 将创建的传递给指定的(如果一个 已定义)。您可以使用提供的引用执行任何操作。我们预计大多数人都想用它来设置一个自定义 callback,以下示例演示如何执行此操作:GroovyObjectGroovyObjectCustomizerGroovyObjectMetaClassSpring中文文档

public final class SimpleMethodTracingCustomizer implements GroovyObjectCustomizer {

	public void customize(GroovyObject goo) {
		DelegatingMetaClass metaClass = new DelegatingMetaClass(goo.getMetaClass()) {

			public Object invokeMethod(Object object, String methodName, Object[] arguments) {
				System.out.println("Invoking '" + methodName + "'.");
				return super.invokeMethod(object, methodName, arguments);
			}
		};
		metaClass.initialize();
		goo.setMetaClass(metaClass);
	}

}

在 Groovy 中对元编程的全面讨论超出了 Spring 的范围 参考手册。请参阅 Groovy 参考手册的相关部分或执行 在线搜索。很多文章都谈到了这个话题。实际上,如果您使用 Spring 命名空间支持,则使用 a 很容易,因为 以下示例显示:GroovyObjectCustomizerSpring中文文档

<!-- define the GroovyObjectCustomizer just like any other bean -->
<bean id="tracingCustomizer" class="example.SimpleMethodTracingCustomizer"/>

	<!-- ... and plug it into the desired Groovy bean via the 'customizer-ref' attribute -->
	<lang:groovy id="calculator"
		script-source="classpath:org/springframework/scripting/groovy/Calculator.groovy"
		customizer-ref="tracingCustomizer"/>

如果不使用 Spring 命名空间支持,则仍可以使用该功能,如以下示例所示:GroovyObjectCustomizerSpring中文文档

<bean id="calculator" class="org.springframework.scripting.groovy.GroovyScriptFactory">
	<constructor-arg value="classpath:org/springframework/scripting/groovy/Calculator.groovy"/>
	<!-- define the GroovyObjectCustomizer (as an inner bean) -->
	<constructor-arg>
		<bean id="tracingCustomizer" class="example.SimpleMethodTracingCustomizer"/>
	</constructor-arg>
</bean>

<bean class="org.springframework.scripting.support.ScriptFactoryPostProcessor"/>
您还可以指定 Groovy(例如 ) 甚至是一个完整的 Groovy 对象,与 Spring 的 .此外,您可以设置一个与自定义的共同点 在级别上配置 Bean; 这也会导致共享使用,因此在以下情况下是推荐的 大量脚本化 Bean(避免每个 Bean 都有一个孤立的实例)。CompilationCustomizerImportCustomizerCompilerConfigurationGroovyObjectCustomizerGroovyClassLoaderConfigurableApplicationContext.setClassLoaderGroovyClassLoaderGroovyClassLoader

BeanShell 豆

本节介绍如何在 Spring 中使用 BeanShell bean。Spring中文文档

BeanShell 主页包括以下内容 描述:Spring中文文档

BeanShell is a small, free, embeddable Java source interpreter with dynamic language
features, written in Java. BeanShell dynamically runs standard Java syntax and
extends it with common scripting conveniences such as loose types, commands, and method
closures like those in Perl and JavaScript.

与 Groovy 相比,BeanShell 支持的 Bean 定义需要一些(小的)附加 配置。Spring 中 BeanShell 动态语言支持的实现是 有趣的是,因为 Spring 创建了一个 JDK 动态代理,它实现了所有 在元素的属性值中指定的接口(这就是为什么必须在值中提供至少一个接口的原因 属性,因此,当您使用 BeanShell 支持时,会对接口进行编程 豆类)。这意味着对 BeanShell 支持的对象的每个方法调用都要经过 JDK 动态代理调用机制。script-interfaces<lang:bsh>Spring中文文档

现在,我们可以展示一个使用基于 BeanShell 的 Bean 的完整工作示例,该 Bean 实现了 本章前面定义的接口。我们再次展示 接口定义:MessengerMessengerSpring中文文档

package org.springframework.scripting;

public interface Messenger {

	String getMessage();
}

下面的示例显示了 BeanShell“实现”(我们在这里松散地使用术语) 的接口:MessengerSpring中文文档

String message;

String getMessage() {
	return message;
}

void setMessage(String aMessage) {
	message = aMessage;
}

下面的示例显示了定义上述“实例”的 Spring XML “类”(同样,我们在这里非常宽松地使用这些术语):Spring中文文档

<lang:bsh id="messageService" script-source="classpath:BshMessenger.bsh"
	script-interfaces="org.springframework.scripting.Messenger">

	<lang:property name="message" value="Hello World!" />
</lang:bsh>

有关您可能想要使用的某些方案,请参阅方案 基于 BeanShell 的 bean。Spring中文文档

默认情况下,此功能处于关闭状态。
您还可以指定 Groovy(例如 ) 甚至是一个完整的 Groovy 对象,与 Spring 的 .此外,您可以设置一个与自定义的共同点 在级别上配置 Bean; 这也会导致共享使用,因此在以下情况下是推荐的 大量脚本化 Bean(避免每个 Bean 都有一个孤立的实例)。CompilationCustomizerImportCustomizerCompilerConfigurationGroovyObjectCustomizerGroovyClassLoaderConfigurableApplicationContext.setClassLoaderGroovyClassLoaderGroovyClassLoader

场景

在脚本语言中定义 Spring 托管 Bean 的可能场景将 有益的是多种多样的。本节介绍 Spring 中的动态语言支持。Spring中文文档

脚本化 Spring MVC 控制器

可以从使用动态语言支持的 Bean 中受益的一组类是 Spring MVC 控制器。在纯 Spring MVC 应用程序中,导航流 通过 Web 应用程序在很大程度上是由封装在 您的 Spring MVC 控制器。作为导航流和其他表示层逻辑 需要更新 Web 应用程序以响应支持问题或更改 业务需求,通过以下方式实现任何此类所需的更改可能更容易 编辑一个或多个动态语言源文件,并查看这些更改 立即反映在正在运行的应用程序的状态中。Spring中文文档

请记住,在诸如 Spring,你通常的目标是有一个非常薄的表示层,所有 包含在域和服务中的应用程序的丰富业务逻辑 图层类。将 Spring MVC 控制器开发为动态语言支持的 Bean 可以 您可以通过编辑和保存文本文件来更改表示层逻辑。任何 对此类动态语言源文件的更改是(取决于配置) 自动反映在动态语言源文件支持的 Bean 中。Spring中文文档

要实现对动态语言支持的任何更改的自动“拾取”效果 beans,您必须启用“可刷新的 bean”功能。请参阅 Refreshable Beans 了解此功能的完整处理。

以下示例显示了已实现的 通过使用 Groovy 动态语言:org.springframework.web.servlet.mvc.ControllerSpring中文文档

package org.springframework.showcase.fortune.web

import org.springframework.showcase.fortune.service.FortuneService
import org.springframework.showcase.fortune.domain.Fortune
import org.springframework.web.servlet.ModelAndView
import org.springframework.web.servlet.mvc.Controller

import jakarta.servlet.http.HttpServletRequest
import jakarta.servlet.http.HttpServletResponse

// from the file '/WEB-INF/groovy/FortuneController.groovy'
class FortuneController implements Controller {

	@Property FortuneService fortuneService

	ModelAndView handleRequest(HttpServletRequest request,
			HttpServletResponse httpServletResponse) {
		return new ModelAndView("tell", "fortune", this.fortuneService.tellFortune())
	}
}
<lang:groovy id="fortune"
		refresh-check-delay="3000"
		script-source="/WEB-INF/groovy/FortuneController.groovy">
	<lang:property name="fortuneService" ref="fortuneService"/>
</lang:groovy>

脚本化验证器

Spring 应用程序开发的另一个领域可能会从中受益 动态语言支持的 Bean 提供的灵活性是验证的灵活性。它可以 使用松散类型的动态语言更容易表达复杂的验证逻辑 (也可能支持内联正则表达式)而不是常规 Java。Spring中文文档

同样,将验证器开发为动态语言支持的 Bean 可以让您更改 通过编辑和保存简单的文本文件来验证逻辑。任何此类更改都是 (取决于配置)自动反映在执行 正在运行的应用程序,并且不需要重新启动应用程序。Spring中文文档

自动“拾取”对动态语言支持的任何更改 beans,您必须启用“可刷新的 beans”功能。请参阅 Refreshable Beans 了解此功能的完整和详细处理。

以下示例显示了使用 Groovy 动态语言实现的 Spring(有关该接口的讨论,请参阅使用 Spring 的 Validator 接口进行验证):org.springframework.validation.ValidatorValidatorSpring中文文档

import org.springframework.validation.Validator
import org.springframework.validation.Errors
import org.springframework.beans.TestBean

class TestBeanValidator implements Validator {

	boolean supports(Class clazz) {
		return TestBean.class.isAssignableFrom(clazz)
	}

	void validate(Object bean, Errors errors) {
		if(bean.name?.trim()?.size() > 0) {
			return
		}
		errors.reject("whitespace", "Cannot be composed wholly of whitespace.")
	}
}
要实现对动态语言支持的任何更改的自动“拾取”效果 beans,您必须启用“可刷新的 bean”功能。请参阅 Refreshable Beans 了解此功能的完整处理。
自动“拾取”对动态语言支持的任何更改 beans,您必须启用“可刷新的 beans”功能。请参阅 Refreshable Beans 了解此功能的完整和详细处理。

其他详细信息

最后一部分包含与动态语言支持相关的一些其他详细信息。Spring中文文档

AOP — 为脚本化 Bean 提供建议

您可以使用 Spring AOP 框架来建议脚本化 Bean。春季AOP 框架实际上并不知道被建议的 Bean 可能是脚本化的 Bean bean,因此您使用(或打算使用)的所有 AOP 用例和功能 使用脚本化 Bean。当您建议使用脚本化 Bean 时,您不能使用基于类的 Bean 代理。您必须使用基于接口的代理Spring中文文档

您不仅限于为脚本化 Bean 提供建议。您也可以自己编写方面 在受支持的动态语言中,并使用此类 Bean 来建议其他 Spring Bean。 不过,这确实是动态语言支持的高级使用。Spring中文文档

范围

如果它不是很明显,脚本化 Bean 的作用域可以采用与 任何其他豆子。各种元素上的属性让 您可以控制底层脚本化 Bean 的范围,就像控制常规 Bean 一样 豆。(默认范围为单例, 就像“普通”豆子一样。scope<lang:language/>Spring中文文档

下面的示例使用该属性来定义作用域为 原型scopeSpring中文文档

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:lang="http://www.springframework.org/schema/lang"
	xsi:schemaLocation="
		http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/lang https://www.springframework.org/schema/lang/spring-lang.xsd">

	<lang:groovy id="messenger" script-source="classpath:Messenger.groovy" scope="prototype">
		<lang:property name="message" value="I Can Do The RoboCop" />
	</lang:groovy>

	<bean id="bookingService" class="x.y.DefaultBookingService">
		<property name="messenger" ref="messenger" />
	</bean>

</beans>

请参阅 IoC 容器中的 Bean 作用域,了解 Spring 框架中范围支持的完整讨论。Spring中文文档

XML 架构lang

Spring XML 配置中的元素处理公开已 用动态语言(如 Groovy 或 BeanShell)编写为 Spring 容器中的 bean。langSpring中文文档

这些元素(以及动态语言支持)在动态语言支持中进行了全面介绍。请参阅该部分 有关此支持和元素的完整详细信息。langSpring中文文档

若要使用架构中的元素,需要在 Spring XML 配置文件的顶部。以下代码段中的文本引用 正确的架构,以便命名空间中的标记可供您使用:langlangSpring中文文档

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:lang="http://www.springframework.org/schema/lang"
	xsi:schemaLocation="
		http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/lang https://www.springframework.org/schema/lang/spring-lang.xsd">

	<!-- bean definitions here -->

</beans>

更多资源

以下链接指向有关引用的各种动态语言的更多资源 本章内容:Spring中文文档