`

将Spring、Hibernate与WAS一起使用(1)

    博客分类:
  • J2ee
阅读更多

如果您考虑将 Spring 或 Hibernate 与 IBM® WebSphere® Application Server 一起使用,则本文向您阐述了如何配置这些框架,以适用于 WebSphere Application Server 的各种场景。本文不是对任一框架的详尽评论,而是帮助您成功实现此类场景的重要参考(针对 Spring Framework 2.1 更新)。
引言

Spring Framework(通常称为 Spring)是一种开源项目,目的是为了使 J2EE™ 环境更具可访问性。Spring 为简单 Java™ 对象提供框架,使这些对象可以通过包装类和 XML 配置使用 J2EE 容器。Spring 的目标是为这些项目提供显著的好处,提高这些项目的开发效率和运行时性能,同时改进测试覆盖率和应用程序质量。

Hibernate 是开放源代码持久性和查询框架,提供 POJO(传统 Java 对象)到关系数据库表的与对象相关的映射,以及数据查询和检索功能。

尽管许多组织感兴趣的是了解使用这些框架能够获得什么好处,但 IBM 希望让使用这些框架的客户知道,他们可以通过 WebSphere Application Server 以强健和可靠的方式做到这一点。本文介绍这些框架如何能够与 WebSphere Application Server 一起使用,并介绍针对各种用例的最佳实践,以帮助您尽快地使用 Spring 或 Hibernate。


使用 Spring

通常将 Spring 描述为轻量级容器环境,但是将其描述为用于简化开发的框架可能更适当。Spring Framework 由 Interface21 根据 Rod Johnson 发表的关于依赖项注入设计模型开发。Spring 可以在独立应用程序或应用服务器中使用。其主要概念是使用依赖项注入和面向方面的编程来简化和平稳地进行从开发到测试再到生产的转换。

涉及 Spring 的最常用的场景之一是使用简单的 Java Bean 类配置并驱动业务逻辑。 Spring 文档应提供了使用 Spring Bean 构建应用程序的足够信息,其中没有提供任何特定于 WebSphere 的内容。以下部分将描述 Spring 在 WebSphere Application Server 上的一些使用场景。根据本文的建议开发的 Spring 应用程序应能够轻松地在 WebSphere Application Server 或 WebSphere Application Server Network Deployment 环境中执行。

除明确指出以外,本文提供的信息适用于所有平台上的 WebSphere Application Server 版本 6.0.2.x 和 6.1.x。

表示层注意事项

本部分介绍的注意事项与在基于 Web 的表示层中使用 Spring 相关。

Web MVC 框架

Spring 的 Web MVC 框架在某一时间内一直是其他框架的备选框架。直接由 WebSphere Application Server 提交、使用和支持的 Web MVC 框架包括 JavaServer Faces (JSF) 和 Struts。Spring 文档描述了如何将 Spring 与这些 Web 框架集成。尽管 WebSphere Application Server 支持使用上面的任何 MVC,但 IBM 仅为 WebSphere Application Server 附带的框架提供产品支持。

Portlet MVC 框架

Spring 还提供了一个 Portlet MVC 框架(该框架镜像 Spring Web MVC 框架),而且在 WebSphere Portal V6.0 和 WebSphere Application Server V6.1 Portlet 容器中运行。有关 Spring Portlet 的示例集,请参见Spring Portlet MVC。在 WebSphere Application Server V6.1 Portlet 容器中运行 Portlet 需要创建其他 Web 应用程序才能定义 Portlet 的布局和聚合。从 WebSphere Application Server 信息中心和文章 Portlet 容器介绍 中可以获得关于如何使用 Portlet 聚合器标记库的信息。通常的做法是结合使用 JSF 和 Portlet 进行呈现。关于如何将 Spring、Hibernate、JSF 和 WebSphere Portal 组合起来的信息,请参见 Configuring Hibernate, Spring, Portlets, and OpenInSessionViewFilter with IBM WebSphere Portal

数据访问注意事项

本部分介绍的注意事项与访问事务数据的 Spring Bean 配置相关。

Spring Framework 实际上使用一个容器管理层(在 J2EE 环境中委派给基础 J2EE 运行时)包装 Spring Bean。下面将介绍应如何配置 Spring Bean,以便 Spring Framework 可以正确委派到 WebSphere Application Server 运行时并与其集成。

访问 WebSphere Application Server 中配置的数据源

WebSphere Application Server 管理在应用服务器执行环境中使用的资源。需要访问 JDBC 数据源之类资源的 Spring 应用程序应该利用 WebSphere 管理的资源。要完成此任务,请执行以下步骤:

在开发过程中,应该使用资源引用配置 WAR 模块。例如:

<resource-ref>

    <res-ref-name>jdbc/springdb</res-ref-name>

    <res-type>javax.sql.DataSource</res-type>

    <res-auth>Container</res-auth>

    <res-sharing-scope>Shareable</res-sharing-scope>

</resource-ref>

 

 

对于 EJB JAR 文件,应该在需要访问数据源的每个 EJB 中声明同一资源引用。

然后在 Spring 应用程序配置中声明数据源代理 Bean,代理 Bean 引用 WebSphere 管理的资源提供程序:

<bean id="WASDataSource" 

    class="org.springframework.jndi.JndiObjectFactoryBean">

    <property name="jndiName" 

        value="java:comp/env/jdbc/springdb"/>

    <property name="lookupOnStartup" 

        value="false"/>

    <property name="cache" 

        value="true"/>

    <property name="proxyInterface" 

        value="javax.sql.DataSource"/>

</bean>


通过此代理 Bean 访问数据源将会使用模块配置的引用查看数据源,因此能够由 WebSphere Application Server 正确管理。注意,jndiName 属性值匹配与资源引用中声明的资源引用名称连接的模式 java:comp/env/

然后,Spring 应用程序可以在适当情况下使用数据源代理 Bean。

将应用程序部署到 WebSphere Application Server 时,必须以常规方式配置资源提供程序和资源数据源,以便由 Spring 应用程序资源引用使用。在部署过程中,在模块的部署描述符中声明的资源引用将绑定到应用服务器的配置数据源。

使用 JDBC 本机连接

当各种 JDBC 操作需要与本机 JDBC 资源交互时,Spring 可提供访问本机连接的机制。当在 JdbcTemplate 类上设置了 NativeJdbcExtractor 类时,Spring JdbcTemplate 类才可以利用此功能。设置 NativeJdbcExtractor 类后,当与 WebSphere Application Server 一起使用时,Spring 总是向下找到本机 JDBC 连接。这将忽略以下 WebSphere 服务质量功能和优点:

连接处理跟踪和再关联

连接共享

参与事务

连接池管理

这带来的另一个问题是 WebSphereNativeJdbcExtractor 类将依赖于内部 WebSphere 适配器类。这些内部类可能因 WebSphere Application Server 的版本而异,并且以后可能更改,从而中断依赖于此功能的应用程序。

在 WebSphere Application Server 上不支持使用 NativeJdbcExtractor 类实现(例如 WebSphereNativeJdbcExtractor),您应避免需要它的场景。备选方案是使用 WebSphere Application Server WSCallHelper 类来访问非标准供应商的数据源扩展。

分享到:
评论

相关推荐

    java8源码-Spring4Hibernate5Maven:基于spring4.2.4+Hibernate5.0.7final搭建的Mave

    这里将以Eclipse搭建Maven工程集成Spring4生成的项目为基础,集成Hibernate5.0.7final。 亲可以参考: 1、Maven安装以及在eclipse中的配置 2、Eclipse中使用Maven创建基本的web项目 3、Maven的Web工程集成Spring4 1...

    Java & Struts2 & Spring & Hibernate & Eclipse Tutorial

    This tutorial was written from the perspective of an Asp.Net / C# developer trying to understand the basics of creating a typical web application using Java.

    Pro Spring 5 An In-Depth Guide -Apress(2017)

    Because the Java 9 release date kept being postponed, Spring 5 was released based on Java 8. Thus, interoperability with Java 9 is covered in this book based on an early-access build. There is a multi...

    Manning.Spring.in.Action.4th.Edition.2014.11.epub

    1. Core Spring Chapter 1. Springing into action 1.1. Simplifying Java development 1.1.1. Unleashing the power of POJOs 1.1.2. Injecting dependencies 1.1.3. Applying aspects 1.1.4. Eliminating ...

    spring-tx-4.0.2.RELEASE.jar

    项目使用了spring-hibernate集成,但是报如下错的话: The project was not built since its build path is incomplete. Cannot find the class file for org.springframework.dao.support 这时有可能是还缺少一个...

    springmybatis

    mybatis实战教程mybatis in action之五与spring3集成附源码 mybatis实战教程mybatis in action之六与Spring MVC 的集成 mybatis实战教程mybatis in action之七实现mybatis分页源码下载 mybatis实战教程mybatis in ...

    基础的java web项目,完成了应用骨架的搭建,提供了底层框架的支持

    (4)集成的框架有:spring mvc + spring + hibernate + logback + junit,spring mvc、hibernate已经用注解配置方式替代 了传统的xml配置方式。 (5)logback在本项目中已经设置了一个aop切面,对所有的controller请求...

    rapid-framework 整合

    rapid-framework,hibernate+spring+springMVC 框架整合

    WebSphere部署war项目

    Struts2+Spring2+Hibernate3 集成环境配置 ---------------------------------------------------------------------------------------------------------------------- 一 WebSphere 6.1 安装及配置  版本:was...

    非常苛刻的java工作要求

    掌握主流的Java开源框架Struts2、Spring、SpringMVC、Hibernate/Mybatis、iBatis等,最好熟悉Freemaker或volecity。 2. 熟悉中间件Tomcat、jboss、Apache、Weblogic、WAS; 3. 熟悉各种Web前端技术,包括JavaScript...

    WebSphere6.1与SSH2+JPA2不兼容问题解决

    WAS6.1用的JDK是IBM jdk1.5, 而且它用的不少jar包比较旧,与最新的SSH2+JPA架构的jar包不兼容,本文是在实践过程中探索到的不兼容问题以及解决方法。基本上是网上查不到的解决方案,谨供需要在生产环境中将最新的SSH2...

    工程硕士学位论文 基于Android+HTML5的移动Web项目高效开发探究

    其中使用Struts作为系统的整体基础架构,负责MVC的分离,在Struts框架的模型部分,控制业务跳转,利用Hibernate框架对持久层提供支持,Spring做管理,管理Struts和Hibernate。 WebStorage HTML新增的本地存储解决...

Global site tag (gtag.js) - Google Analytics