今天上午从svn上checkout一个项目,里面用到了ZK和Spring框架,本来这个项目是部署在Jboss上的,现在我需要在Tomcat中部署它。下面把我部署时出现的问题及解决方法记录下来:
1.checkout之后,我首先改了Spring的配置文件:applicationContext.xml
将数据源的配置:
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean"> |
替换为:
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> |
并在项目同目录下增加application.config.properties文件,内容为:
# db server config db.url=jdbc:oracle:thin:@ip:1521:serverName db.password=loginPassword |
其中粗体代表需要替换你的实际参数的地方。
本以为这样就可以了,发布,启动Tomcat,报错:
NoClassDefFoundError: javax/transaction/Synchronization
......
Caused by:
java.lang.NoClassDefFoundError: javax/transaction/Synchronization
at java.lang.Class.getDeclaredConstructors0(Native Method)
这个是因为缺少jta.jar包的原因,到网上下了一个jta.jar包,放到lib目录下。
启动又报错:
IllegalStateException: Either 'userTransaction' or 'userTransactionName' or 'transactionManager' or 'transactionManagerName' must be specified
到网上查查看,关于这个错误有这样的说法:
You use JTA so re-introducte the jta.jar when running on Tomcat. As you correctly mentioned, JTA is provided by JBoss out-of-the-box (which is has to because it is a a full J2EE server), tomcat otoh doesn't provide that because it is merely a JSP/Servlet container. (This is the part you already fixed).
Tomcat also doesn't provide anything for distributed transaction, you will need to introduce something yourself, the are standalone JTA providers/manager available wich work perfectly well with Tomcat).
我到配置文件(applicationContext.xml)里看看,发现有这样的配置:
<bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager"></bean> |
把它换成:
<bean id="transactionManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> |
重新发布,启动,又报错:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'txAdvice': Cannot resolve reference to bean 'transactionManager' while setting bean property 'transactionManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionManager' defined in class path resource [META-INF/commonContext.xml]: Invocation of init method failed; nested exception is java.lang.IllegalStateException: Either 'userTransaction' or 'userTransactionName' or 'transactionManager' or 'transactionManagerName' must be specified
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionManager' defined in class path resource [META-INF/commonContext.xml]: Invocation of init method failed; nested exception is java.lang.IllegalStateException: Either 'userTransaction' or 'userTransactionName' or 'transactionManager' or 'transactionManagerName' must be specified
要解决这个问题,请按照下面的方法进行(转自:http://zkgale.javaeye.com/blog/210421)
java.lang.NoClassDefFoundError: org/objectweb/asm/CodeVisito
错误:java.lang.NoClassDefFoundError: org/objectweb/asm/CodeVisitor的解决方法
在使用Spring的AOP编程时,会用到这几个lib:
asm-2.2.2.jar
asm-commons-2.2.2.jar
asm-util-2.2.2.jar
Hibernate使用如下lib:
asm.jar
asm-attrs.jar
其中asm-2.2.2.jar与asm.jar存在类上的冲突!!!
使用其中之一或两者都使用,可能会出现如下错误:
java.lang.NoClassDefFoundError: org/objectweb/asm/CodeVisitor
java.lang.NoClassDefFoundError: org/objectweb/asm/commons/EmptyVisitor
java.lang.NoSuchMethodError: org.objectweb.asm.ClassVisitor.visit。。。。。。
解决办法是:
1.去掉类路径上的关于Hibernate的3个lib
asm.jar
asm-attrs.jar
cglib-2.1.3.jar
2.加入Spring中的以下4个lib
asm-2.2.2.jar
asm-commons-2.2.2.jar
asm-util-2.2.2.jar
cglib-nodep-2.1_3.jar
好了,到这里,问题就解决完了,发布、启动Tomcat,成功。