此版本仍在开发中,尚不被认为是稳定的。对于最新的稳定版本,请使用 Spring Framework 6.2.10spring-doc.cadn.net.cn

动态语言支持

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

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

您可以找到这种动态语言支持的完全有效的示例 在场景中立即有用。spring-doc.cadn.net.cn

第一个例子

本章的大部分内容涉及描述 细节。在深入了解动态语言支持的所有细节之前, 我们看一个用动态语言定义的 bean 的快速示例。动态 第一个 bean 的语言是 Groovy。(这个例子的基础取自 Spring 测试套件。如果您想在任何其他示例中查看等效示例 支持的语言,请查看源代码)。spring-doc.cadn.net.cn

下一个示例显示了Messenger接口,Groovy bean 将要 实现。请注意,此接口是用纯 Java 定义的。依赖对象 注入引用Messenger不知道底层 实现是一个 Groovy 脚本。以下列表显示了Messenger接口:spring-doc.cadn.net.cn

package org.springframework.scripting;

public interface Messenger {

	String getMessage();
}

以下示例定义了一个依赖于Messenger接口:spring-doc.cadn.net.cn

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...
	}
}

以下示例实现了MessengerGroovy 中的界面:spring-doc.cadn.net.cn

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 Schema 前导码。 您还需要使用 SpringApplicationContext实现作为您的 IoC 容器。将动态语言支持的 bean 与普通BeanFactory支持实现,但您必须管理 Spring 内部的管道 这样做。spring-doc.cadn.net.cn

有关基于架构的配置的更多信息,请参阅 基于 XML 架构的配置spring-doc.cadn.net.cn

最后,以下示例显示了影响注入 Groovy 定义Messenger实现到DefaultBookingService类:spring-doc.cadn.net.cn

<?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>

bookingServicebean (aDefaultBookingService) 现在可以使用其私有的messengermember 变量,因为Messenger注入其中的实例是 一个Messenger实例。这里没有什么特别的事情——只有普通的 Java 和 普通的时髦。spring-doc.cadn.net.cn

希望前面的 XML 片段是不言自明的,但如果不是,请不要过分担心。 继续阅读有关上述配置的原因和原因的深入详细信息。spring-doc.cadn.net.cn

定义由动态语言支持的 Bean

本节准确描述了如何在任何 支持的动态语言。spring-doc.cadn.net.cn

请注意,本章不试图解释支持的 动态语言。例如,如果您想使用 Groovy 编写某些类 在您的应用程序中,我们假设您已经了解 Groovy。如果您需要更多详细信息 关于动态语言本身,请参阅末尾的更多资源 本章。spring-doc.cadn.net.cn

常见概念

使用动态语言支持的 Bean 所涉及的步骤如下:spring-doc.cadn.net.cn

  1. 编写动态语言源代码的测试(自然)。spring-doc.cadn.net.cn

  2. 然后编写动态语言源代码本身。spring-doc.cadn.net.cn

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

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

<lang:language/> 元素

一节中列表中的最后一步涉及定义动态语言支持的 Bean 定义,对于您 想要配置(这与正常的 JavaBean 配置没有什么不同)。然而 而不是指定要成为的类的完全限定类名 实例化并由容器配置,您可以使用<lang:language/>元素来定义动态语言支持的 bean。spring-doc.cadn.net.cn

每种受支持的语言都有相应的<lang:language/>元素:spring-doc.cadn.net.cn

可用于配置的确切属性和子元素取决于 Bean 的确切定义语言(特定于语言的部分 本章后面将详细介绍这一点)。spring-doc.cadn.net.cn

可刷新豆

动态语言最引人注目的附加值之一(也许是唯一的) Spring 中的支持是“可刷新 bean”功能。spring-doc.cadn.net.cn

可刷新 Bean 是动态语言支持的 Bean。用少量的 配置,动态语言支持的 Bean 可以监控其底层的更改 源文件资源,然后在动态语言源文件 已更改(例如,当您编辑和保存对文件系统上文件的更改时)。spring-doc.cadn.net.cn

这允许您将任意数量的动态语言源文件部署为 应用程序,配置 Spring 容器以创建由动态支持的 Bean 语言源文件(使用本章中描述的机制)和(稍后, 随着需求的变化或其他一些外部因素发挥作用)编辑动态 语言源文件,并将它们所做的任何更改反映在 Bean 中。 由更改的动态语言源文件支持。无需关闭 正在运行的应用程序(或在 Web 应用程序的情况下重新部署)。这 dynamic-language-backed bean so amended 从 更改了动态语言源文件。spring-doc.cadn.net.cn

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

现在我们可以看一个例子,看看开始使用 refreshable 是多么容易 豆。要打开可刷新 Bean 功能,您必须只指定一个 附加属性<lang:language/>元素。所以 如果我们坚持前面的例子 本章中,以下示例展示了我们将在 Spring XML 中更改的内容 配置以实现可刷新的 bean:spring-doc.cadn.net.cn

<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>

这确实是你所要做的。这refresh-check-delay属性定义在messengerbean 定义是 bean 之后的毫秒数 刷新对底层动态语言源文件所做的任何更改。 您可以通过为refresh-check-delay属性。请记住,默认情况下,刷新行为为 禁用。如果不希望刷新行为,请不要定义属性。spring-doc.cadn.net.cn

如果我们随后运行以下应用程序,我们可以行使可刷新功能。 (请原谅“跳过障碍暂停执行”的恶作剧 在下一段代码中。这System.in.read()调用只是为了 程序的执行会暂停,而您(在这种情况下的开发人员)会离开 并编辑基础动态语言源文件,以便刷新触发 在程序恢复执行时,在动态语言支持的 Bean 上。spring-doc.cadn.net.cn

以下列表显示了此示例应用程序:spring-doc.cadn.net.cn

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()方法Messenger必须更改实现,以便消息是 用引号括起来。以下列表显示了您所做的更改 (开发商)应向Messenger.groovy当 程序的执行暂停:spring-doc.cadn.net.cn

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 Frug. 对源文件进行更改并保存并恢复程序执行后, 调用getMessage()动态语言支持的方法Messenger实现是'I Can Do The Frug'(请注意,包含 附加引号)。spring-doc.cadn.net.cn

如果对脚本的更改发生在 这refresh-check-delay价值。对脚本的更改直到 在动态语言支持的 Bean 上调用一个方法。只有当一个方法被 调用动态语言支持的 Bean 时,它会检查其底层脚本是否 来源已更改。与刷新脚本相关的任何异常(例如 遇到编译错误或发现脚本文件已被删除) 导致将致命异常传播到调用代码。spring-doc.cadn.net.cn

前面描述的可刷新 Bean 行为不适用于动态语言 源文件定义了<lang:inline-script/>元素表示法(参见内联动态语言源文件)。 此外,它仅适用于对底层源文件的更改可以 实际被检测到(例如,通过检查 动态语言源文件)。spring-doc.cadn.net.cn

内联动态语言源文件

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

<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>

如果我们把围绕定义是否是良好做法的问题放在一边 动态语言源代码,则<lang:inline-script/>元素在某些情况下可能很有用。例如,我们可能想要快速添加一个 SpringValidator实现到 Spring MVCController.这只是片刻的 使用内联源代码工作。(有关此类示例,请参阅脚本验证器。spring-doc.cadn.net.cn

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

关于 Spring 的动态,有一件非常重要的事情需要注意 语言支持。也就是说,您不能(当前)提供构造函数参数 到动态语言支持的 bean(因此,构造函数注入不适用于 dynamic-language-backed bean)。为了对 构造函数和属性 100% 清晰,以下代码和配置的混合 不起作用:spring-doc.cadn.net.cn

行不通的方法
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-doc.cadn.net.cn

Groovy 豆子

本节介绍如何使用 Spring 的 Groovy 中定义的 bean。spring-doc.cadn.net.cn

Groovy 主页包含以下描述:spring-doc.cadn.net.cn

“Groovy 是一种用于 Java 2 平台的敏捷动态语言,它具有许多 人们非常喜欢 Python、Ruby 和 Smalltalk 等语言的功能,使 它们可供使用类似 Java 语法的 Java 开发人员使用。spring-doc.cadn.net.cn

如果您直接从头开始阅读了本章,那么您已经看到了 Groovy-dynamic-language-backed 的示例 豆。现在考虑另一个示例(再次使用 Spring 测试套件中的示例):spring-doc.cadn.net.cn

package org.springframework.scripting;

public interface Calculator {

	int add(int x, int y);
}

以下示例实现了CalculatorGroovy 中的界面:spring-doc.cadn.net.cn

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-doc.cadn.net.cn

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

最后,以下小型应用程序执行上述配置:spring-doc.cadn.net.cn

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));
	}
}

运行上述程序的结果输出是(毫不奇怪)10. (更多有趣的示例,请参阅动态语言展示项目,了解更多 复杂示例或参见本章后面的示例场景)。spring-doc.cadn.net.cn

不得为每个 Groovy 源文件定义多个类。虽然这是完美的 合法,这(可以说)是一种糟糕的做法。为了保持一致 方法,你应该(在 Spring 团队看来)尊重标准 Java 每个源文件一个(公共)类的约定。spring-doc.cadn.net.cn

使用回调自定义 Groovy 对象

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

public interface GroovyObjectCustomizer {

	void customize(GroovyObject goo);
}

Spring Framework 实例化 Groovy 支持的 bean 的实例,然后 将创建的GroovyObject到指定的GroovyObjectCustomizer(如果有一个 已被定义)。您可以使用提供的GroovyObject参考。我们预计大多数人都希望设置自定义MetaClass有了这个 callback,以下示例显示了如何执行此作:spring-doc.cadn.net.cn

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 参考手册的相关部分或执行 在线搜索。很多文章都涉及这个话题。实际上,利用GroovyObjectCustomizer如果您使用 Spring 命名空间支持,则很容易,因为 以下示例显示:spring-doc.cadn.net.cn

<!-- 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 命名空间支持,您仍然可以使用GroovyObjectCustomizer功能,如以下示例所示:spring-doc.cadn.net.cn

<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"/>
您还可以指定一个 GroovyCompilationCustomizer(例如ImportCustomizer) 甚至是完整的 GroovyCompilerConfiguration对象与 Spring 的GroovyObjectCustomizer.此外,您可以设置一个共同的GroovyClassLoader与自定义 配置ConfigurableApplicationContext.setClassLoader水平; 这也导致共享GroovyClassLoader使用,因此建议在以下情况下使用 大量脚本化 Bean(避免隔离的GroovyClassLoader实例)。

BeanShell 豆

本节介绍如何在 Spring 中使用 BeanShell bean。spring-doc.cadn.net.cn

BeanShell 主页包括以下内容 描述:spring-doc.cadn.net.cn

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 动态代理,它实现了所有 在script-interfaces属性值<lang:bsh>元素(这就是为什么您必须在值中提供至少一个接口的原因 属性,因此,当您使用 BeanShell 支持时,将编程到接口 豆子)。这意味着对 BeanShell 支持的对象的每个方法调用都会通过 JDK 动态代理调用机制。spring-doc.cadn.net.cn

现在我们可以展示一个完全有效的示例,使用基于 BeanShell 的 bean 实现 这Messenger本章前面定义的接口。我们再次展示 定义Messenger接口:spring-doc.cadn.net.cn

package org.springframework.scripting;

public interface Messenger {

	String getMessage();
}

以下示例显示了 BeanShell 的“实现”(我们在这里松散地使用该术语) 的Messenger接口:spring-doc.cadn.net.cn

String message;

String getMessage() {
	return message;
}

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

以下示例显示了定义上述“实例”的 Spring XML “类”(同样,我们在这里非常宽松地使用这些术语):spring-doc.cadn.net.cn

<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-doc.cadn.net.cn

场景

在脚本语言中定义 Spring 托管 Bean 的可能场景 有益是多种多样的。本节介绍 Spring 中的动态语言支持。spring-doc.cadn.net.cn

脚本化 Spring MVC 控制器

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

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

要自动“拾取”对动态语言支持的任何更改 bean,您必须启用“可刷新 bean”功能。有关此功能的完整处理,请参阅可刷新豆

以下示例显示了org.springframework.web.servlet.mvc.Controller实现 通过使用 Groovy 动态语言:spring-doc.cadn.net.cn

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-doc.cadn.net.cn

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

自动“拾取”对动态语言支持的任何更改 bean,您必须启用“可刷新 bean”功能。有关此功能的完整详细处理,请参阅可刷新 Bean

以下示例显示了 Springorg.springframework.validation.Validator使用 Groovy 动态语言实现(有关 Spring 的 Validator 接口的讨论,请参阅使用 Spring 的 Validator 接口进行验证Validator接口):spring-doc.cadn.net.cn

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.")
	}
}

其他详细信息

最后一节包含与动态语言支持相关的一些其他详细信息。spring-doc.cadn.net.cn

AOP — 为脚本化 Bean 提供建议

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

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

范围

如果不是立即显而易见,脚本化 Bean 的范围可以与任何其他 Bean 相同。 这scope属性<lang:language/>元素让 您可以控制底层脚本 Bean 的范围,就像它对常规 豆。(默认作用域为单例, 就像“普通”豆子一样。spring-doc.cadn.net.cn

以下示例使用scope属性来定义作用域为 原型:spring-doc.cadn.net.cn

<?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>

有关 Spring Framework 中作用域支持的完整讨论,请参阅 IoC 容器中的 Bean 作用域spring-doc.cadn.net.cn

langXML 架构

lang元素处理公开已 用动态语言(例如 Groovy 或 BeanShell)编写为 Spring 容器中的 bean。spring-doc.cadn.net.cn

动态语言支持中全面介绍了这些元素(以及动态语言支持)。请参阅该部分 有关此支持的完整详细信息,以及lang元素。spring-doc.cadn.net.cn

要使用langschema,您需要在 Spring XML 配置文件的顶部。以下代码片段中的文本引用 正确的架构,以便lang命名空间可供您使用:spring-doc.cadn.net.cn

<?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-doc.cadn.net.cn