使用 WebLogic Server 开发应用程序
|
Spring 应用程序参考
以下部分介绍开发和管理 WebLogic Server 的基于 Spring Framework 的应用程序。多数情况下,这些部分中的信息都是从创建 MedRec-Spring 的角度进行介绍的。
关于 WebLogic Server 上的 Spring
为了演示 Spring 利用 WebLogic Server 企业功能的方式,BEA 重新设计了 Avitek 医疗记录示例应用程序 (MedRec),使用 Spring 组件替代了核心 J2EE 组件。有关 MedRec 体系结构及其重新设计的其他信息,请参阅位于 http://dev2dev.bea.com/pub/a/2005/09/spring_integration_weblogic_server.html 下的文章“Spring Integration with WebLogic Server”。
以下部分介绍 BEA 在重新设计 MedRec 时执行的关键步骤。如果您想使用 Spring 组件重新设计自己的基于 J2EE 的 WebLogic Server 应用程序,可以使用此信息。如果要为 WebLogic Server 创建新的基于 Spring 组件的应用程序,也可以利用此信息。
此处假设您熟悉 J2EE 概念、WebLogic Server 和 Spring Framework。有关 WebLogic Server 的信息,请参阅 BEA WebLogic Server 9.2 文档。有关 BEA dev2dev 网站上 Spring 的信息,请参阅 Spring Resource Page。有关 Spring Framework 的常规信息,请参阅 http://www.springframework.org/。
将基于 J2EE 的应用程序重新设计为基于 Spring 的应用程序
要将基于 J2EE 的应用程序转换为基于 Spring 的应用程序,需要执行如下所述的步骤:
以下部分介绍将基于 J2EE 的应用程序重新设计为基于 Spring 的应用程序的详细信息。这些部分会在适当位置包含示例代码。多数情况下,示例代码来自 MedRec-Spring。
配置 Spring 控制反转 (Inversion of Control)
在 Spring 中,通过 Spring 配置 XML 文件 applicationContext-web.xml
来配置对其他 Bean(注入属性)的引用。
在 MedRec-Spring 中,BEA 使用 POJO 替代 Spring 配置文件 src/medrecEar/web/WEB-INF/applicationContext-web.xml
中的无状态会话 EJB,如下所示:
class="com.bea.medrec.web.patient.actions.ViewRecordAction">
<property name="medRecClientServiceFacade">
<ref bean="medRecClientServiceFacade"/>
</bean>
然后,在应用程序代码中,BEA 将为相应 Bean 定义设置器方法。例如:
protected MedRecClientServiceFacade medRecClientServiceFacade;
public void setMedRecClientServiceFacade(
MedRecClientServiceFacade pMedRecClientServiceFacade){
this.medRecClientServiceFacade = pMedRecClientServiceFacade;
}
启用 Spring Web Service 客户端服务
要使用为 Web Service 生成代理的 Spring JAX-RPC 工厂,需要通过实现如下代码来配置 Spring JaxRpcPortProxyFactoryBean
;在 MedRec-Spring 中,BEA 在 Spring 配置文件 src/physicianEar/APP-INF/classes/applicationContext-phys-service.xml
中实现此代码。
<!-- 将新医疗记录发送给 medrec 的可靠异步 Web Service -->
<bean id="reliableClientWebServicesPortType"
class="org.springframework.remoting.jaxrpc.JaxRpcPortProxyFactoryBean"
lazy-init="true">
<property name="wsdlDocumentUrl" value="http://${WS_HOST}:${WS_PORT}/ws_phys/PhysicianWebServices?WSDL"/>
<property name="portName" value="PhysicianWebServicesPort"/>
<property name="jaxRpcService">
<ref bean="generatedReliableService"/>
</property>
<property name="serviceInterface" value="com.bea.physician.webservices.client.PhysicianWebServicesPortType"/>
<property name="username" value="medrec_webservice_user"/>
<property name="password" value="weblogic"/>
<property name="customProperties"><props>
<prop key="weblogic.wsee.complex">true</prop>
</props>
</property>
</bean>
<> <!-- 允许 jaxRpcService 类执行其加载类型映射的构造方法 -->
<bean id="generatedReliableService" class="com.bea.physician.webservices.client.PhysicianWebServices_Impl">
</bean>
使 JMS 服务可用于运行时应用程序
在 Spring 中,必须配置 JMS 服务,以便将它们提供给运行时的应用程序。可以通过表示消息传递目标的 Spring Bean 来完成此操作。在 Med-Rec Spring 中,BEA 通过在 Spring 配置文件 src/medrecEar/APP-INF/classes/applicationContext-jms.xml
中实现如下代码,使 JMS 服务可用于运行时应用程序。
<bean id="uploadQueue" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName"
value="com.bea.medrec.messagging.MedicalRecordUploadQueue"/>
</bean>
<bean id="jmsConnFactory"
class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName"
value="com.bea.medrec.messagging.MedRecQueueConnectionFactory"/>
</bean>
<bean id="uploadJmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory">
<ref bean="jmsConnFactory"/>
</property>
<property name="defaultDestination">
<ref bean="uploadQueue"/>
</property>
</bean>
配置 JMX:将 WebLogic Server Runtime MBean Server 连接公开给 Spring
可以将 WebLogic Server 的 MBean 服务器公开给 Spring,方法是通过 Spring 的 MBeanServerConnectionFactoryBean
,这是一个生成 MBeanServerConnection
(在应用程序部署时建立并缓存此 MBeanServerConnection,而且以后引用 Bean 能够对其进行操作)的便捷工厂。可将 MBeanServerConnectionFactoryBean
配置为返回 WebLogic Server Runtime MBean Server,并获取到 WebLogic Server Domain Runtime MBean Server 和 WebLogic Server Edit MBean Server 的连接。
注意: | 因为 WebLogic Server Domain Runtime MBean Server 在部署时未处于活动状态,所以必须配置 MBeanServerConnectionFactoryBean 以使用 Spring 的 lazy instantiation。在 Lazy instantiation 被调用时,它会提取 Spring Bean。 |
下列代码示例演示“将 WebLogic Server Runtime MBean Server 连接公开给 Spring”的过程,在 MedRec-Spring 中,BEA 在 Spring 配置文件 medrecEar/APP-INF/classes/applicationContext-jmx.xml
中实现此过程。
<> <!-- 公开 WebLogic Server 的运行时 mbeanserver 连接 -->
<bean id="runtimeMbeanServerConnection" class="org.springframework.jmx.support.MBeanServerConnectionFactoryBean">
<property name="serviceUrl" value="service:jmx:t3://${WS_HOST}:${WS_PORT}/jndi/weblogic.management.mbeanservers.runtime"/>
<property name="environment">
<props>
<prop key="java.naming.security.principal">${WS_USERNAME}</prop>
<prop key="java.naming.security.credentials">${WS_USERNAME}</prop>
<prop key="jmx.remote.protocol.provider.pkgs">weblogic.management.remote</prop>
</props>
</property>
</bean>
将 Spring JDBC 配置为与连接缓冲池通信
在 MedRec-Spring 中,BEA 使用了引用 JDBC 连接缓冲池的数据源(此 JDBC 连接缓冲池受 WebLogic Server 管理),也使用了 JdbcDaoSupport
类。有关 JdbcDaoSupport
的信息,请参阅 Spring 文档。
有关 BEA 实现 JDBC 的方法的示例,请参阅 MedRec-Spring 类
src/medrecEar/dao/com/bea/medrec/dao/jdbc/JdbcPatientDao.java
另请参阅下列代码示例,对于 MecRec-Spring,BEA 分别在 Spring 配置文件 src/medrecEar/APP-INF/classes/applicationContext-db.xml
和 src/medrecEar/APP-INF/classes/applicationContext-jdbc.xml
中实现这些代码。
applicationContext-db.xml
代码示例:
<!-- 数据源缓冲池 -->
<bean id="dataSource"
class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="jdbc/MedRecGlobalDataSourceXA"/>
</bean>
applicationContext-jdbc.xml
代码示例:
<bean id="patientDao"
class="com.bea.medrec.dao.jdbc.JdbcPointBasePatientDao"
autowire="byType"/>
此外,在 MedRec-Spring 中,BEA 使用 POJO 替代实体 EJB,并使用 Spring JDBC 的持久性。有关示例,请参阅 MedRec-Spring 类 /src/medrecEar/core/com/bea/medrec/domain/Address.java
。
使用 Spring 事务抽象层进行事务管理
通过 WebLogic Server 的 JTA 实现,Spring 支持分布式事务。还可以配置 Spring 事务管理器,将责任委派给 WebLogic Server JTA 事务管理器。通过 Spring 的 WebLogicJtaTransactionManager
类可以达到此目的。BEA 在 MedRec-Spring 中使用此方法,是为了正确模仿初始版本 MedRec 的事务管理。
要使用 Spring 事务抽象层进行事务管理,并将责任委派给 WebLogic Server JTA 事务管理器,必须实现如下所示的代码,BEA 分别在 Spring 配置文件 src/medrecEar/APP-INF/classes/applicationContext-tx.xml
和 src/medrecEar/APP-INF/classes/applicationContext-service.xml
中实现这些代码。
applicationContext-tx.xml
代码示例:
<!-- Spring 的事务管理器对 WebLogic Server 事务管理器的委派 -->
<bean id="transactionManager" class="org.springframework.transaction.jta.WebLogicJtaTransactionManager">
<property name="transactionManagerName"
value="javax.transaction.TransactionManager"/>
</bean>
applicationContext-service.xml
代码示例:
<!-- medrec spring bean 要继承的基本事务代理 -->
< bean id="baseTransactionProxy"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
abstract="true">
<property name="transactionManager" ref="transactionManager"/>
<property name="transactionAttributes">
<props>
<prop key="activate*">PROPAGATION_REQUIRED</prop>
<prop key="create*">PROPAGATION_REQUIRED</prop>
<prop key="compose*">PROPAGATION_REQUIRED</prop>
<prop key="deny*">PROPAGATION_REQUIRED</prop>
<prop key="getRecord*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="getPatient*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="getLog*">PROPAGATION_NOT_SUPPORTED</prop>
<prop key="process*">PROPAGATION_REQUIRED</prop>
<prop key="save*">PROPAGATION_REQUIRED</prop>
<prop key="send*">PROPAGATION_REQUIRED</prop>
</props>
</property>
< /bean>
<!-- 针对所有 medrec 客户端的单点服务 -->
<bean id="medRecClientServiceFacade"
parent="baseTransactionProxy">
<property name="target">
<bean class="com.bea.medrec.service.MedRecClientServiceFacadeImpl">
<property name="adminService">
<ref bean="adminService"/>
</property>
<property name="patientService">
<ref bean="patientService"/>
</property>
<property name="recordService">
<ref bean="recordService"/>
</property>
<property name="recordXmlProcessorService">
<ref bean="recordXmlProcessorService"/>
</property>
</bean>
</property>
</bean>
您指定的 transactionAttributes
定义 Spring 开始和结束事务的方式。因为 MedRec-Spring 将事务管理委派给了 WebLogic JTA,所以诸如事务挂起和回滚的管理任务将按 WebLogic 事务管理器的指定去处理。
有关 WebLogicJtaTransactionManager
的详细信息,请参阅位于 http://dev2dev.bea.com/pub/a/2005/07/spring_transactions.html 的“Implementing Transaction Suspension in Spring”。
使用 WebLogic Server 群集
Spring 应用程序可以使用 WebLogic Server 的群集功能。因为多数 Spring 应用程序被打包为 Web 应用程序(.war 文件),所以要使用 WebLogic Server 群集,不需要您做任何特殊的工作,您需要做的只是将 Spring 应用程序部署到 WebLogic Server 群集中的服务器。
群集 Spring Remoting
WebLogic Server 上 Spring 1.2.8 的证书扩展了 Spring JndiRmiProxyFactoryBean
和其关联的服务导出器,以使其使用任意 J2EE RMI 实现支持代理。要将扩展用于 JndiRmiProxyFactoryBean
和其导出器,请执行下列操作:
通过实现如下所示代码来配置客户端支持:<bean id="proProxy" class="org.springframework.remoting.rmi.JndiRmiProxyFactoryBean">
<property name="jndiName" value="t3://${serverName}:${rmiPort}/order"/>
</property>
<property name="jndiEnvironment">
<props>
<prop key="java.naming.factory.url.pkgs">weblogic.jndi.factories</prop>
</props>
</property>
<property name="serviceInterface" value="org.springframework.samples.jpetstore.domain.logic.OrderService"/>
</bean>通过实现如下所示代码来配置服务导出器:<bean id="order-pro" class="org.springframework.remoting.rmi.JndiRmiServiceExporter">
<property name="service" ref="petStore"/>
<property name="serviceInterface" value="org.springframework.samples.jpetstore.domain.logic.OrderService"/>
<property name="jndiName" value="order"/>
</bean>
对 WebLogic 管理控制台的 Spring 扩展
可以将 Spring 扩展用于 WebLogic Server 管理控制台,用来监视和管理在您的应用程序中定义的 Spring Bean、特性和操作。
将 Spring 扩展安装到 WebLogic 管理控制台
要将 Spring 扩展安装到 WebLogic 管理控制台,请执行下列步骤:
通过 WebLogic 管理控制台公开 Spring Bean
为了能够通过 WebLogic 管理控制台访问不是 MBean 的 Spring Bean,必须在 applicationContext.xml
文件中配置 MBeanExporter,并指定将通过汇编程序公开哪些 Bean。确保 applicationName
属性是您的应用程序的部署名。
在 WebLogic Server 上支持 Spring
有关 BEA 如何支持此版本 WebLogic Server 的信息以及 Interface21 Spring Framework 的相关信息,请参阅 Supported Configurations for Products with Spring Framework。