搭建SSH时的思考和遇到的几个问题 (转帖)

来源:互联网 发布:我的野蛮女友 知乎 编辑:程序博客网 时间:2024/05/17 10:26
本次所用版本为:Struts 1.3.9   Spring 2.5   Hiberante 3.2
开发工具和环境为:Eclipse 3.3+MyEclipse 6.0+JBoss 4.2+Tomcat

用SSH时,我们的架构自然就会分成三层,即表现层,逻辑层和持久层,按照Martin Flower的指导思想,耦合越少越好,下层为上层提供服务,这也是Rod开发Spring的指导思想之一,所以我首先想到的就是如何减少到最低的耦合。
根据Spring 2.0官方文档中推荐的做法,Spring与Struts1.x集成,有采用代理类的方式,也有用ActionSupport的方式,但是我认为这两种方式无疑都有很强的侵入性和依赖性,这与Spring的思想有些矛盾。
我采用AutowiringRequestProcessor来做,这个类会自动为你装载你所需要的Service,根据其Java Doc的提示,其默认是byType匹配的,当然你也可以用byName的方式,我认为以Type的方式就OK了。来看看两个配置文件吧,先来看看Struts的配置文件struts-config.xml:
 1 <? xml version="1.0" encoding="UTF-8" ?>
 2 <! DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" "http://struts.apache.org/dtds/struts-config_1_3.dtd" >
 3
 4 < struts-config >
 5      < form-beans >
 6          < form-bean  name ="loginForm"  type ="com.***.ssh.view.login.LoginForm"   />
 7      </ form-beans >
 8
 9      < global-exceptions  />
10      < global-forwards  />
11      < action-mappings >
12          < action  name ="loginForm"  path ="/login"  scope ="request"
13             type ="com.***.ssh.view.login.LoginAction"  validate ="false"   />
14      </ action-mappings >
15     
16      < controller
17          processorClass ="org.springframework.web.struts.AutowiringRequestProcessor"   />
18
19      < message-resources  parameter ="ApplicationResources"   />
20 </ struts-config >
21
22

这个文件中可以发现,跟没有与Spring集成时就一点不一样,多了一行:
 <controller  processorClass="org.springframework.web.struts.AutowiringRequestProcessor" />

再来看看Spring的配置文件applicationContext.xml:

 1 <? xml version="1.0" encoding="UTF-8" ?>
 2 <! DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd" >
 3
 4 < beans >
 5
 6      < bean  id ="SSHSessionFactory"
 7         class ="org.springframework.orm.hibernate3.LocalSessionFactoryBean" >
 8          < property  name ="configLocation" >
 9              < value > classpath:hibernate.cfg.xml </ value >
10          </ property >
11      </ bean >
12     
13      < bean  name ="LoginService"
14         class ="com.***.ssh.biz.login.LoginService"  singleton ="false" >
15          < property  name ="dao" >
16              < ref  bean ="UsersDao"   />
17          </ property >
18      </ bean >
19
20      < bean  id ="UsersDao"  class ="com.***.ssh.persistence.UserDao" >
21          < property  name ="sessionFactory" >
22              < ref  bean ="SSHSessionFactory"   />
23          </ property >
24      </ bean >
25 </ beans >
26

这里面也没有多余的内容,不会出现代理方式时,多份XML同时配置的问题,减少了维护量。
同时,你会发现,我并没有把Hibernate的具体配置放在这里面,而是用Hibernate自身的配置来管理。

这样的话,我们三层之间的依赖性会降到较低,两头可以任意换到其中的某一层。

顺便要说的是,常见的书籍上面发现往往不会有如此深入的探讨,难道是怕初学者看不懂吗?
另外,对于一个架构来说,要解决的问题决不是指这些,通常我们可以采用RUP的4+1视图的方法去考虑架构的方方面面。我们也可以从以下一些方面来各个击破:安全性,数据输入输出的校验与转换,国际化,LOG,异常处理,异构系统整合,后台运行程序等等。如果是多个数据库,我们还需要更多的考虑事务控制。

我想我后面会去完善这个整合,并实现一个Demo,作为小的简单的项目快速开发的基础。



整合时遇到过如下问题:
问题1:启动时出现 “严重: Error listenerStart ”
这个问题,网上有很多解决办法:
有一种最简单的解决办法是把用Listener初始化Spring改为用Servlet初始化Spring,但这样的方法不太好,一是没有找到根源,二是可能会带来新的问题。
比较好的一种解决办法是,加上Log4J的相关配置,然后再启动时,就会出现各类详细信息,这样可根据具体信息再来解决,一般可能是DataSource配置,或环境配置有问题。
参考网址:http://hi.baidu.com/xht314/blog/item/808ecf13c1dd1820dd5401af.html

内容:

********************************

在启动tomcat时总是出一个错误:

      2007-5-31 14:27:13 org.apache.catalina.core.StandardContext start
    严重: Error listenerStart
      2007-5-31 14:27:13 org.apache.catalina.core.StandardContext start
    严重: Context [/testWSH] startup failed due to previous errors
    
    教程的作者在录制教程时也遇到了此问题,但是他删了一个jar包后就没事了,可是我的一直无法正常启动,从昨天到现在一天的时间都在研究这个问题,也“百 度”到了很多有关此问题的信息,但是都没有很明确的解决方案。现在此问题已经解决,而且基本肯定问题所在,所以将解决方案写出来以供参考。

    有一种解决方案是把web.xml文件中的
<listener>       <listener-class>org.springframework.web.context.ContextLoaderListener<!--</span-->listener-class>
<!--</span-->listener>
改为
   <servlet>
<servlet-name>SpringContextServlet<!--</span-->servlet-name>
<servlet-class>
    <!--</span-->servlet-class>
<load-on-startup>1<!--</span-->load-on-startup>
<!--</span-->servlet>
    但这种方法可能会出现其他问题(网上又说会导致其他文件无法打开)。
    
     我的最终解决方案如下:
    我用的是tomcat5.5,配置了日志之后打印出下列信息:
       ERROR main org.springframework.web.context.ContextLoader - Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Instantiation of bean failed; nested exception is java.lang.NoClassDefFoundError: org/apache/commons/pool/impl/GenericObjectPool
Caused by:
java.lang.NoClassDefFoundError: org/apache/commons/pool/impl/GenericObjectPool
     at java.lang.Class.getDeclaredConstructors0(Native Method)
     at java.lang.Class.privateGetDeclaredConstructors(Class.java:2328)
     at java.lang.Class.getConstructor0(Class.java:2640)
     at java.lang.Class.getDeclaredConstructor(Class.java:1953)

     ……

    从日志信息看问题已经很明显了,是applicationContext.xmldataSource 问题。

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
改为
<bean id="dataSource"class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<!--[if !supportEmptyParas]-->之后问题得到解决。 <!--[endif]-->
       出现"Error listenerStart"一般是applicationContext.xml中的bean加载有问题。在用eclipse做webwork + spring + hibernate 的项目时一般都是用Myeclipse自动生成那些配置文件,而有些相关的jar<!--[if !supportEmptyParas]-->或者文件并没有加载在至项目中,以至引出奇怪的问题,又因为是自动生成的东西所以往往会忽略一些文件,而问题确恰恰是这些生成的文件所致,所以自动化的东西也未必一定是正确的,呵呵……

    org.springframework.jdbc.datasource.DriverManagerDataSource 不可以使用连接池。org.apache.commons.dbcp.BasicDataSource作为注入的DataSource源,为了使用 DBCP的功能,必须要将commons-dbcp.jar加入CLASSPATH中,另外还需要commons-pool.jar和commons- collections.jar,这些都可以在Spring的lib目录下找到。

org.springframework.jdbc.datasource.DriverManagerDataSource并没有提供连接池的功能,只能作作简单的单机连接测试。
使用org.apache.commons.dbcp.BasicDataSource时缺少commons-pool.jar所以会出现如题的问题。

********************************

问题2:遇到“Required extension qdox not found”这样的提示
网上也有解答,我用的方法是直接把commons-attributes-compiler.jar这个包去掉。当然如果你要用到这个包的话,可以参考下面这个地址:
http://hi.baidu.com/sky_lei/blog/item/77ee17085543b232e8248824.html

内容:

*********************************

问题的表现形式是,当把项目部署到Tomcat服务器时,出现如下错误:

INFO: Deploying web application archive TestSpringMVC.war
Aug
18, 2006 10:21:13 AM org.apache.catalina.util.ExtensionValidator validateManifestResources
INFO: ExtensionValidator[
/TestSpringMVC][commons-attributes-api.jar]: Required extension "ant" not found.
Aug
18, 2006 10:21:13 AM org.apache.catalina.util.ExtensionValidator validateManifestResources
INFO: ExtensionValidator[
/TestSpringMVC][commons-attributes-compiler.jar]: Required extension "ant" not found.
Aug
18, 2006 10:21:13 AM org.apache.catalina.util.ExtensionValidator validateManifestResources
INFO: ExtensionValidator[
/TestSpringMVC][commons-attributes-compiler.jar]: Required extension "javadoc" not found.
Aug
18, 2006 10:21:13 AM org.apache.catalina.util.ExtensionValidator validateManifestResources
INFO: ExtensionValidator[
/TestSpringMVC]: Failure to find 3 required extension(s).
Aug
18, 2006 10:21:13 AM org.apache.catalina.core.StandardContext start
SEVERE: Error getConfigured
Aug
18, 2006 10:21:13 AM org.apache.catalina.core.StandardContext start
SEVERE: Context [
/TestSpringMVC] startup failed due to previous errors
Aug
18, 2006 10:21:13 AM org.apache.catalina.core.StandardContext stop
INFO: Container org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[
/TestSpringMVC] has not been started

这个问题导致项目无法启动,也就无法去访问。
这个问题出现的原因是commons-attributes-compiler.jar包的MANIFEST.MF文件出现问题,如果对这个包没有依赖,直接删除这个包的引用,项目就可以正常启动,如果对这个包有依赖,那么可以通过以下两种方式来解决。
1、修改MANIFEST.MF文件
原compiler.jar包中的MF文件结构

Manifest-Version: 1.0
Ant
-Version: Apache Ant 1.5.3
Created
-By: Apache Maven
Built
-By: hen
Package: org.apache.commons.attributes
Build
-Jdk: 1.4.2_05
Extension
-Name: commons-attributes-api
Specification
-Title: Client API for Jakarta Commons Attributes.
Specification
-Vendor: The Apache Software Foundation
Implementation
-Title: org.apache.commons.attributes
Implementation
-Vendor: The Apache Software Foundation
Implementation
-Version: 2.2
Extension
-List: ant qdox
ant
-Extension-Name: ant
ant
-Implementation-Version: 1.5
ant
-Implementation-URL: http://www.ibiblio.org/maven/ant/jars/ant-1.5.
jar
qdox
-Extension-Name: qdox
qdox
-Implementation-Version: 1.5
qdox
-Implementation-URL: http://www.ibiblio.org/maven/qdox/jars/qdox-1
.5.jar
Implementation
-Vendor-Id: org.apache
X
-Compile-Source-JDK: 1.4
X
-Compile-Target-JDK: 1.4

我们可以看到URL后面的地址中含有一些不必要的字符,将MF文件修改如下

Manifest-Version: 1.0
Ant
-Version: Apache Ant 1.5.3
Created
-By: Apache Maven
Built
-By: hen
Package: org.apache.commons.attributes
Build
-Jdk: 1.4.2_05
Extension
-Name: commons-attributes-api
Specification
-Title: Client API for Jakarta Commons Attributes.
Specification
-Vendor: The Apache Software Foundation
Implementation
-Title: org.apache.commons.attributes
Implementation
-Vendor: The Apache Software Foundation
Implementation
-Version: 2.2
Extension
-List: ant qdox
ant
-Extension-Name: ant
ant
-Implementation-Version: 1.5
ant
-Implementation-URL: http://www.ibiblio.org/maven/ant/jars/ant-1.5.jar
qdox-Extension-Name: qdox
qdox
-Implementation-Version: 1.5
qdox
-Implementation-URL: http://www.ibiblio.org/maven/qdox/jars/qdox-1.5.jar
Implementation-Vendor-Id: org.apache
X
-Compile-Source-JDK: 1.4
X
-Compile-Target-JDK: 1.4

这个问题就可以解决。
2、替换compiler.jar包
下载,将我提供的jar包下载之后替换也可以解决这个问题

*********************************

问题3:Struts包与Spring包冲突的问题
我遇到包有冲突,换成Struts需要的优先就OK了,现在的JAR档越来越麻烦了,很多项目的JAR档都被开发人员搞得乱七八糟,看来Maven是个好东西,至少思想是好的,可能下一步需要研究一下。 

原创粉丝点击