如何使用Struts 2 Spring插件来集成Spring和Struts

本文介绍了如何使用Struts2Spring插件将Spring框架与Struts2集成,以管理ActionSupport类的依赖关系,增强应用的灵活性和可维护性。

Spring和Struts 2

 

介绍

Struts 2 Spring插件

Spring配置文件

替代方案 - 让Spring管理ActionSupport类的创建

摘要



本教程的示例代码spring-struts可用于struts-examples的签出

介绍

在许多Struts 2的execute方法中,ActionSupport类是创建对象然后让这些对象执行执行所需任务的方法的语句。每当一个类创建另一个类的对象时,会引入两个类之间的依赖关系。Spring框架使应用程序开发人员可以更轻松地管理这些依赖项,并使应用程序更加灵活和可维护。本教程将向您展示如何一起使用Struts 2和Spring来管理ActionSupport类与应用程序中其他类之间的依赖关系。

本教程假定您了解如何使用Spring框架来管理类之间的依赖关系。您可以通过阅读https://spring.io/docs上的文档了解有关Spring的更多信息

Struts 2的用户邮件列表是一个很好的地方,以获得帮助。如果您在使用教程示例应用程序时遇到问题,请搜索Struts 2邮件列表。如果找不到问题的答案,请在邮件列表上发布问题。

如果您检查Struts 2主题教程的示例应用程序,您将在EditAction类中看到此代码

EditAction类硬编码依赖

private EditService editService = new EditServiceInMemory();

上面的语句硬编码了EditAction类和EditServiceInMemory类之间的依赖关系。这是糟糕的设计有两个原因。

  1. 如果我需要替换EditServiceInMemory另一个实现EditService接口的类,我将不得不寻找并替换我对依赖项进行硬编码的所有语句。
  2. 我不能在EditAction不使用EditServiceInMemory课程的情况下进行测试。在写我的测试用例时,我无法EditAction通过使用存根实现进行隔离,EditService因为使用了EditServiceInMemory硬编码。

Spring提供了一种通过在运行时注入依赖项来管理依赖项的机制。Struts 2 ActionSupport类 - 与任何其他Java类一样 - 可以通过Spring框架注入依赖对象。所以我没有上面的代码,而是将此语句包含在内EditAction

EditAction类没有硬编码依赖

private EditService editService;

在运行时,Spring框架将提供实现EditService接口的类的对象。

Struts 2 Spring插件

Struts 2提供了一个插件,使Spring能够将您在Spring配置文件中指定的任何依赖对象注入ActionSupport类。有关插件如何工作的更多信息,请参阅Spring Plugin文档

对于Maven应用程序,您需要向struts2-spring-plugin jar添加依赖项(请参阅本教程的Maven示例应用程序)。插件的pom.xml包含对Spring jar文件的传递依赖性。

2.5.10.1Struts 2 Spring插件的当前版本()具有Spring 4.1.6.RELEASE版本的传递依赖性。如果您想使用最新版本的Spring,那么您应该在pom.xml中为Struts 2 Spring插件排除传递依赖关系,然后将依赖关系节点声明为当前版本的Spring jar文件。如果您正在使用Ant并在应用程序中明确包含jar文件,那么只需包含最新版本的Spring jar文件。

在您的ActionSupport类中,您必须具有遵循标准Java bean命名约定的依赖对象的set方法。如果您检查EditAction本教程的应用程序的类,您将看到此set方法。

public void setEditService(EditService editService) {
    this.editService = editService;
}

Spring将使用该set方法在运行时EditServiceEditAction类提供类型的对象。

为了使我们的应用程序“Spring感知”,我们需要将此行添加到web.xml。

Spring Listener在web.xml中

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

当Servlet容器启动应用程序时,上面的代码将激活Spring框架。默认情况下,Spring将在WEB-INF中查找配置文件名applicationContext.xml(请参阅Spring文档,了解如何更改Spring的外观以及配置文件名是什么)。

Spring配置文件

在Spring配置文件中,我们为那些我们希望Spring创建实例并注入ActionSupport类的对象创建一个bean节点。在示例应用程序中是这样的applicationContext.xml

Spring配置文件

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

    <bean id="editService" class="org.apache.struts.edit.service.EditServiceInMemory" />

</beans>

注意上面的id值。默认情况下,Spring插件与Spring一起使用以自动装配ActionClass的依赖项name。Spring将创建一个EditServiceMemory类的对象,并将该对象提供给任何具有setEditService方法的ActionSupport类,该方法的参数类型为EditService。有关 如何更改默认autowire方法,请参阅Spring Plugin文档。

由Spring创建的editService bean将具有单例范围,因为这是默认范围。有关如何配置bean定义以使用不同的范围(例如请求或会话),请参阅Spring文档的第3.5节。

替代方案 - 让Spring管理ActionSupport类的创建

使用上述方法,Struts 2框架仍将管理ActionSupport类的创建。如果您愿意,可以配置应用程序,以便Spring也将创建ActionSupport类。要支持此技术,您需要将Bean节点添加到ActionSupport类的Spring配置文件中。

Spring管理的ActionSupport类的Spring配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
            http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd">
            
    <bean id="editService" class="org.apache.struts.edit.service.EditServiceInMemory" />

    <bean id="editAction" class="org.apache.struts.edit.action.EditAction" scope="prototype">
        <property name="editService" ref="editService" />
    </bean>

</beans>

请注意,上面有一个editAction bean,其editService 属性设置为editService bean。由于我们正在使用Spring管理EditAction类,因此我们必须指定EditAction 我们希望Spring注入的任何属性。请记住,必须在每个请求上创建操作,它们不能  singletons- 这是默认值 scope,因为它必须更改为  prototype

struts.xml 配置文件中,您必须为操作节点的class属性指定Spring id值。这告诉Struts从Spring获取一个带有来自Spring的id值的bean。

Spring托管ActionSupport类的Struts配置

<action name="edit" class="editAction" method="input">
    <result name="input">/edit.jsp</result>
</action>

摘要

在本教程中,我们回顾了如何使用Struts 2 Spring插件来集成Spring和Struts。通过使用Struts 2 Spring插件,您可以让Spring管理ActionSupport类的依赖项。当然,您还可以利用Spring框架提供的许多其他好处(AOP,Spring JDBC)。

本文翻译自:https://struts.apache.org/getting-started/spring.html

Struts 官方指南

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值