|
对于最新稳定版本,请使用 Spring Framework 7.0.6! |
动态语言支持
Spring 提供了全面的支持,用于在 Spring 中使用通过动态语言(例如 Groovy)定义的类和对象。该支持允许你使用受支持的动态语言编写任意数量的类,并由 Spring 容器透明地实例化、配置和依赖注入所生成的对象。
Spring 的脚本支持主要面向 Groovy 和 BeanShell。除了这些明确支持的语言之外,Spring 还支持 JSR-223 脚本机制(从 Spring 4.2 起),可用于集成任何符合 JSR-223 标准的语言提供者,例如 JRuby。
你可以在应用场景中找到这些动态语言支持可立即发挥作用的完整示例。
第一个示例
本章的大部分内容旨在详细描述动态语言支持。在深入探讨动态日消息支持的各种细节之前,我们先来看一个用动态语言定义的 Bean 的快速示例。此示例中的动态语言是 Groovy。(该示例基于 Spring 测试套件。如果你想查看其他受支持语言的等效示例,请参阅源代码。)
下一个示例展示了 Messenger 接口,Groovy bean 将实现该接口。请注意,此接口是用纯 Java 定义的。那些被注入了 Messenger 引用的依赖对象并不知道其底层实现实际上是一个 Groovy 脚本。以下代码清单展示了 Messenger 接口:
package org.springframework.scripting;
public interface Messenger {
String getMessage();
}
以下示例定义了一个依赖于 Messenger 接口的类:
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 实现了 Messenger 接口:
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 声明。此外,您还需要使用 Spring 的 有关基于 schema 的配置的更多信息,请参阅基于 XML Schema 的配置。 |
最后,以下示例展示了用于将 Groovy 定义的 Messenger 实现注入到 DefaultBookingService 类实例中的 bean 定义:
<?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>
现在,bookingService bean(一个 DefaultBookingService)可以像平常一样使用其私有的 messenger 成员变量,因为注入到其中的 Messenger 实例就是一个 Messenger 实例。这里并没有什么特殊之处——只是普通的 Java 和普通的 Groovy。
希望前面的 XML 片段已经不言自明,但如果还不清楚,也无需过分担心。 请继续阅读,以深入了解上述配置背后的原因和细节。
定义由动态语言支持的 Bean
本节详细描述了如何在任何受支持的动态语言中定义由 Spring 管理的 Bean。
请注意,本章并不试图解释所支持的动态语言的语法和惯用法。例如,如果你想使用 Groovy 编写应用程序中的某些类,我们假定你已经掌握了 Groovy。如果你需要进一步了解这些动态语言本身,请参阅本章末尾的更多资源。
通用概念
使用动态语言支持的 Bean 所涉及的步骤如下:
-
为动态语言源代码编写测试(自然而然地)。
-
然后编写动态语言本身的源代码。
-
通过在 XML 配置中使用相应的
<lang:language/>元素来定义由动态语言支持的 Bean(您也可以通过编程方式使用 Spring API 来定义此类 Bean,但需要查阅源代码以了解具体实现方法,因为本章未涵盖此类高级配置)。请注意,这是一个迭代步骤:每个动态语言源文件至少需要一个 Bean 定义(尽管多个 Bean 定义可以引用同一个源文件)。
前两个步骤(测试和编写动态语言源文件)超出了本章的范围。请参阅您所选动态语言的语言规范和参考手册,并着手开发您的动态语言源文件。不过,您首先应阅读完本章其余部分,因为 Spring 对动态语言的支持确实对您的动态语言源文件内容做出了一些(较小的)假设。
<lang:language/> 元素
上一节前面部分中列出的最后一步
涉及定义动态语言支持的 Bean 定义,每个您想要配置的 Bean 各定义一个(这与普通的 JavaBean 配置并无不同)。
然而,您无需指定由容器实例化和配置的类的完整限定类名,
而是可以使用 <lang:language/> 元素来定义动态语言支持的 Bean。
每种受支持的语言都有一个对应的 <lang:language/> 元素:
-
<lang:groovy/>(Groovy) -
<lang:bsh/>(BeanShell) -
<lang:std/>(JSR-223,例如配合 JRuby 使用)
可用于配置的确切属性和子元素取决于该 bean 是用哪种语言定义的(本章后面的语言特定部分将对此进行详细说明)。
可刷新 Bean
Spring 对动态语言支持最具吸引力(甚至可能是唯一最具吸引力)的价值之一就是“可刷新 Bean”特性。
可刷新的 Bean 是一种由动态语言支持的 Bean。通过少量配置,这种由动态语言支持的 Bean 就能监控其底层源文件资源的变化,并在动态语言源文件发生更改时(例如,当您在文件系统中编辑并保存该文件时)自动重新加载自身。
这使您能够将任意数量的动态语言源文件作为应用程序的一部分进行部署,配置 Spring 容器以创建由动态语言源文件支持的 Bean(使用本章所述的机制),并在之后(当需求发生变化或某些其他外部因素介入时)编辑动态语言源文件,使其所做的任何更改都能立即反映在由该已修改的动态语言源文件所支持的 Bean 中。无需关闭正在运行的应用程序(对于 Web 应用程序也无需重新部署)。经过这样修改的、由动态语言支持的 Bean 会自动从已更改的动态语言源文件中获取新的状态和逻辑。
| 此功能默认处于关闭状态。 |
现在,我们可以看一个示例,了解开始使用可刷新 Bean 有多么简单。要启用可刷新 Bean 功能,您必须在 Bean 定义的 <lang:language/> 元素上精确指定一个额外的属性。因此,如果我们继续沿用本章前面提到的示例,下面的示例展示了我们需要对 Spring XML 配置进行哪些更改,以实现可刷新 Bean:
<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 bean 定义中指定的 messenger 属性表示在底层动态语言源文件发生更改后,经过多少毫秒刷新该 bean。
你可以通过为 refresh-check-delay 属性赋予一个负值来关闭刷新行为。请注意,默认情况下,刷新行为是禁用的。如果你不希望启用刷新行为,请不要定义该属性。
如果我们随后运行以下应用程序,就可以测试可刷新功能。
(请原谅接下来这段代码中为了“暂停程序执行”而采取的繁琐操作。)
其中的 System.in.read() 调用只是为了在程序执行过程中暂停,
以便你(在此场景中作为开发者)可以去修改底层的动态语言源文件,
这样当程序恢复执行时,基于动态语言的 Bean 就会触发刷新。
以下列表展示了该示例应用程序:
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 源文件所做的修改:
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'(请注意其中额外添加的引号)。
如果脚本的更改发生在 refresh-check-delay 值所设定的时间窗口内,则不会触发刷新。实际上,对脚本的更改并不会立即生效,而是在调用基于动态语言的 Bean 的某个方法时才会被检测到。只有当调用基于动态语言的 Bean 的方法时,它才会检查其底层脚本源是否发生了更改。任何与刷新脚本相关的异常(例如遇到编译错误或发现脚本文件已被删除)都会导致一个致命异常被传播到调用代码中。
前面描述的可刷新 Bean 行为不适用于使用 <lang:inline-script/> 元素定义的动态语言源文件(参见内联动态语言源文件)。此外,该行为仅适用于那些底层源文件的更改能够被实际检测到的 Bean(例如,通过检查文件系统中动态语言源文件的最后修改日期的代码)。
内联动态语言源文件
动态语言支持还可以处理直接嵌入在 Spring Bean 定义中的动态语言源文件。更具体地说,<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 配置文件中定义动态语言源代码是否属于良好实践这一问题,<lang:inline-script/> 元素在某些场景下仍然很有用。例如,我们可能希望快速为 Spring MVC 的 Validator 添加一个 Spring Controller 实现。使用内联源代码只需片刻即可完成。(参见脚本化验证器中的示例。)
理解在动态语言支持的 Bean 上下文中的构造函数注入
关于 Spring 的动态语言支持,有一件非常重要的事情需要注意:目前你无法为基于动态语言的 bean 提供构造函数参数(因此,基于动态语言的 bean 不支持构造函数注入)。为了清晰地表明这种对构造函数和属性的特殊处理方式,以下代码与配置的混合示例是无效的:
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 注入(至于这是否是一件好事,我们留待日后讨论)。
Groovy Bean
本节介绍如何在 Spring 中使用 Groovy 定义的 Bean。
Groovy 官网首页包含以下描述:
“Groovy 是一种面向 Java 2 平台的敏捷动态语言,它具备许多类似 Python、Ruby 和 Smalltalk 等语言广受欢迎的特性,并以类似 Java 的语法提供给 Java 开发者使用。”
如果你是从本章开头一直读到这里,那么你已经看过一个由 Groovy 动态语言支持的 Bean 的示例。现在再来看另一个示例(同样取自 Spring 测试套件中的例子):
package org.springframework.scripting;
public interface Calculator {
int add(int x, int y);
}
以下示例使用 Groovy 实现了 Calculator 接口:
package org.springframework.scripting.groovy
// from the file 'calculator.groovy'
class GroovyCalculator implements Calculator {
int add(int x, int y) {
x + y
}
}
以下 Bean 定义使用了在 Groovy 中定义的计算器:
<!-- from the file 'beans.xml' -->
<beans>
<lang:groovy id="calculator" script-source="classpath:calculator.groovy"/>
</beans>
最后,以下小型应用程序演示了前述配置的使用:
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。
(如需更有趣的示例,请参阅动态语言展示项目以了解更复杂的例子,或参见本章稍后部分的场景示例。)
每个 Groovy 源文件中不应定义多个类。尽管这在 Groovy 中是完全合法的,但(可以说)这是一种不良实践。为了保持一致的编码风格,您应当(按照 Spring 团队的观点)遵循标准的 Java 约定,即每个源文件只包含一个(public)类。
使用回调自定义 Groovy 对象
GroovyObjectCustomizer 接口是一个回调接口,允许你在创建基于 Groovy 的 Bean 的过程中插入额外的创建逻辑。例如,该接口的实现可以调用任何所需的初始化方法、设置某些默认属性值,或指定一个自定义的 MetaClass。以下代码展示了 GroovyObjectCustomizer 接口的定义:
public interface GroovyObjectCustomizer {
void customize(GroovyObject goo);
}
Spring 框架会实例化一个由 Groovy 支持的 Bean 实例,然后将创建好的 GroovyObject 传递给指定的 GroovyObjectCustomizer(如果已定义)。你可以对提供的 GroovyObject 引用执行任意操作。我们预计大多数用户希望通过此回调设置一个自定义的 MetaClass,以下示例展示了如何实现这一点:
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的命名空间支持,使用GroovyObjectCustomizer是非常简单的,如下例所示:
<!-- 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 功能,如下例所示:
<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"/>
您也可以在与 Spring 的 CompilationCustomizer 相同的位置指定一个 Groovy ImportCustomizer(例如 CompilerConfiguration),
甚至可以指定一个完整的 Groovy GroovyObjectCustomizer 对象。此外,您可以在 GroovyClassLoader 级别为您的 Bean 设置一个带有自定义配置的公共 ConfigurableApplicationContext.setClassLoader;
这同样会导致共享 GroovyClassLoader 的使用,因此在存在大量脚本化 Bean 的情况下推荐采用此方式(避免为每个 Bean 创建独立的 GroovyClassLoader 实例)。 |
BeanShell Bean
本节介绍如何在 Spring 中使用 BeanShell bean。
BeanShell 主页包含以下描述:
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 支持的 Bean 时必须面向接口编程)。这意味着,对 BeanShell 支持的对象的每一次方法调用都会经过 JDK 动态代理的调用机制。
现在,我们可以展示一个完整且可运行的示例,该示例使用基于 BeanShell 的 bean 来实现本章前面定义的 Messenger 接口。我们再次展示 Messenger 接口的定义:
package org.springframework.scripting;
public interface Messenger {
String getMessage();
}
以下示例展示了 Messenger 接口的 BeanShell “实现”(此处我们对该术语的使用较为宽松):
String message;
String getMessage() {
return message;
}
void setMessage(String aMessage) {
message = aMessage;
}
以下示例展示了定义上述“类”的一个“实例”的 Spring XML 配置(再次说明,这里我们对这些术语的使用非常宽松):
<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 管理的 Bean 有许多不同且多样的适用场景。本节将介绍 Spring 动态语言支持的两个可能用例。
脚本化的 Spring MVC 控制器
有一类可以从使用动态语言支持的 Bean 中获益的类,那就是 Spring MVC 控制器。在纯 Spring MVC 应用程序中,Web 应用的导航流程在很大程度上由封装在 Spring MVC 控制器中的代码所决定。当 Web 应用的导航流程和其他表示层逻辑需要更新以应对支持问题或不断变化的业务需求时,通过编辑一个或多个动态语言源文件,并让这些更改立即反映到正在运行的应用程序状态中,可能会更容易实现所需的变更。
请记住,在 Spring 等项目所倡导的轻量级架构模型中,通常目标是让表示层非常薄,而将应用程序中所有核心业务逻辑都放在领域层和服务层的类中。将 Spring MVC 控制器开发为由动态语言支持的 Bean,可以让你通过编辑和保存文本文件来更改表示层逻辑。对这些动态语言源文件所做的任何更改(取决于配置)都会自动反映到由动态语言源文件支持的 Bean 中。
| 要实现对动态语言支持的 Bean 所做的任何更改的自动“拾取”,您必须启用“可刷新 Bean”(refreshable beans)功能。有关此功能的完整说明,请参阅可刷新 Bean。 |
以下示例展示了使用 Groovy 动态语言实现的 org.springframework.web.servlet.mvc.Controller:
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 所提供的灵活性的领域是验证(validation)。与使用常规 Java 相比,使用松散类型的动态语言(该语言可能还支持内联正则表达式)来表达复杂的验证逻辑可能会更加简便。
同样,将验证器开发为基于动态语言的 Bean,可让您通过编辑并保存一个简单的文本文件来更改验证逻辑。任何此类更改(取决于配置)都会自动反映在正在运行的应用程序的执行中,而无需重新启动应用程序。
| 要实现对动态语言支持的 Bean 所做的任何更改的自动“拾取”,您必须启用“可刷新 Bean”(refreshable beans)功能。有关此功能的完整详细说明,请参阅可刷新 Bean。 |
以下示例展示了一个使用 Groovy 动态语言实现的 Spring org.springframework.validation.Validator(有关 xref page 接口的讨论,请参见使用 Spring 的 Validator 接口进行验证):
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.")
}
}
附加详情
最后一节包含了一些与动态语言支持相关的附加细节。
AOP — 通知脚本化 Bean
你可以使用 Spring AOP 框架来对脚本化 Bean 进行通知(advice)。实际上,Spring AOP 框架并不知道被通知的 Bean 可能是一个脚本化 Bean,因此你所使用(或打算使用)的所有 AOP 用例和功能都可以与脚本化 Bean 一起正常工作。当你对脚本化 Bean 进行通知时,不能使用基于类的代理,而必须使用基于接口的代理。
您不仅限于对脚本化 Bean 进行通知。您还可以使用受支持的动态语言直接编写切面本身,并使用此类 Bean 来通知其他 Spring Bean。 不过,这确实属于动态语言支持的高级用法。
作用域
如果这一点尚不明显,脚本化 Bean 的作用域可以与其他任何 Bean 一样进行设置。各种 scope 元素上的 <lang:language/> 属性允许你控制底层脚本化 Bean 的作用域,这与普通 Bean 的方式相同。(默认作用域是 singleton,与“普通” Bean 一致。)
以下示例使用 scope 属性来定义一个作用域为 原型(prototype) 的 Groovy bean:
<?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>
这langXML 模式
Spring XML 配置中的 lang 元素用于将使用动态语言(例如 Groovy 或 BeanShell)编写的对象暴露为 Spring 容器中的 bean。
这些元素(以及动态语言支持)在动态语言支持一节中有全面介绍。有关此支持功能及lang元素的完整详情,请参阅该部分。
要在 lang 命名空间中使用这些元素,您需要在 Spring XML 配置文件的顶部包含以下前导声明。以下代码片段中的文本引用了正确的 schema,从而使 lang 命名空间中的标签对您可用:
<?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>