歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Spring2.5.6+Hibernate3+RMI整合

Spring2.5.6+Hibernate3+RMI整合

日期:2017/3/1 9:53:38   编辑:Linux編程

今天做spring2.5.6+Hibernate3+RMI整合的程序

昨天的問題: Unable to create Java 1.5 dependent parser: org.springframework.context.annotation.ComponentScanBeanDefinitionParser
編譯程序遇到的問題,應該是spring版本和配置文件之間的問題。 見 http://www.linuxidc.com/Linux/2013-08/89162.htm

解決方法:
要將編譯工程的jre改為1.6.0.3+spring2.5.6+hibernate3.3.2這樣才匹配
另外,要將applicationContext.xml放在src目錄下才能直接通過
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");配置文件導入成功
還有,對應類的****.hbm.xml文件最好一定和類文件放在一起,否則也容易找不到!!
並且要在配置文件中加相對路徑

錯誤spring+Hibernate整合普通java工程,
網上很多都是web工程
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
少包conmons—logging.jar包

j今天的 問題:在正確整合spring2.5.6+hibernate3之後配置文件加入RMI服務配置,即報錯:
Exception in thread "main" org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'logPersonService' defined in class path resource [server.xml]:
Cannot resolve reference to bean 'logPerson' while setting bean property 'service';
nested exception is org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'logPerson' defined in class path resource [server.xml]:
Invocation of init method failed; nested exception is java.lang.IllegalArgumentException:
'sessionFactory' or 'hibernateTemplate' is required

at com.test.ServicTest.main(ServicTest.java:15)
Caused by: org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'logPerson' defined in class path resource [server.xml]:
Invocation of init method failed; nested exception is java.lang.IllegalArgumentException:
'sessionFactory' or 'hibernateTemplate' is required

解決方法:去掉重復定義的bean id原來有問題的配置文件如下(注意黑體字的地方):

<!-- Pass the session factory to our UserDAO -->
<bean id="PersonTarget" class="com.hibernate.LogPerson">
<property name="sessionFactory"><ref local="SessionFactory"/></property>
</bean>

<!-- RMI service -->

<bean id="logPerson" class="com.hibernate.LogPerson"/>
<bean id="logPersonService"
class="org.springframework.remoting.rmi.RmiServiceExporter">
<!-- RmiServiceExporter 對服務名沒有特殊要求 -->
<property name="serviceName"> <value>LogPerson</value> </property>
<property name="service"> <ref bean="logPerson"/> </property>
<property name="serviceInterface"> <value>com.rmi.ILogPerson</value> </property>
<!-- 避免與默認的RMI注冊端口沖突,因此修改為1200 -->
<property name="registryPort"> <value>1200</value> </property>

</bean>

修改過正確的配置文件如下:

<!-- Pass the session factory to our UserDAO -->
<bean id="PersonTarget" class="com.hibernate.LogPerson">
<property name="sessionFactory"><ref local="SessionFactory"/></property>
</bean>

<!-- RMI service -->

<bean id="logPersonService"
class="org.springframework.remoting.rmi.RmiServiceExporter">
<!-- RmiServiceExporter 對服務名沒有特殊要求 -->
<property name="serviceName"> <value>LogPerson</value> </property>
<property name="service"> <ref bean="logPerson"/> </property>
<property name="serviceInterface"> <value>com.rmi.ILogPerson</value> </property>
<!-- 避免與默認的RMI注冊端口沖突,因此修改為1200 -->
<property name="registryPort"> <value>1200</value> </property>

</bean>

總結: 對於spring+hibernate+RMI合並的配置文件,所有的類都不要進行bean id的重復定義。
例如在<!-- Pass the session factory to our UserDAO -->部分已經定義了com.hibernate.LogPerson類
<bean id="PersonTarget" class="com.hibernate.LogPerson">
<property name="sessionFactory"><ref local="SessionFactory"/></property>
</bean>
後面的部分就不能再重復定義,否則就會報錯
<!-- RMI service -->重復定義
<bean id="logPerson" class="com.hibernate.LogPerson"/>

Copyright © Linux教程網 All Rights Reserved