Swing写的窗口程序,采用了Spring和hibernate。在myeclipse中可以正常运行,导出成可运行的jar之后就无发显示从数据库中查询数据才显示的jtable。Spring配置文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xmlns:aop="http://www.springframework.org/schema/aop"
     xmlns:tx="http://www.springframework.org/schema/tx"
     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://10.XXX.XXX.XXX:(发帖改的,这里没有问题)3306/POS?characterEncoding=gbk"></property>
<!-- 
<property name="url" value="jdbc:mysql://127.0.0.1:3306/POS?characterEncoding=gbk"></property>
 -->
<property name="username" value="root"></property>
<property name="password" value="nbsp"></property>
</bean>

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="mappingDirectoryLocations">
<list>
<value>classpath:com/pos/po</value>
</list>
</property>

<property name="hibernateProperties">
<props>
<prop key="dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean> <bean id="ht" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>

<bean id="htm" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 事务属性 -->
<tx:advice id="txAdvice" transaction-manager="htm">
<tx:attributes>
<tx:method name="*" propagation="REQUIRED" isolation="READ_COMMITTED"/>
</tx:attributes>
</tx:advice>

<!-- 事务通知织入 -->
<aop:config>
<aop:advisor pointcut="execution(* com.pos.dao.*.*(..))" advice-ref="txAdvice" />
</aop:config>


<!-- 系统对象 -->
<bean id="baseDao" class="com.pos.dao.impl.BaseDaoImpl" abstract="true">
<property name="hibernateTemplate" ref="ht"></property>
</bean>
<bean id="dao" class="com.pos.dao.impl.DaoImpl" parent="baseDao">
</bean>

<!-- 实体对象 -->
<bean id="campaignInfo" class="com.pos.po.CampaignInfo" parent="baseDao">
</bean>
<bean id="cardInfo" class="com.pos.po.CardInfo" parent="baseDao">
</bean>
<bean id="posSerial" class="com.pos.po.PosSerial" parent="baseDao">
</bean>
<bean id="primaryKey" class="com.pos.po.PrimaryKey" parent="baseDao">
</bean>
<bean id="tidInfo" class="com.pos.po.TidInfo" parent="baseDao">
</bean>
<bean id="trans" class="com.pos.po.Trans" parent="baseDao">
</bean>
<bean id="transInfo" class="com.pos.po.TransInfo" parent="baseDao">
</bean>
</beans> 启动spring的类如下:
package com.pos.util;import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;public class Beans {
public static String configFile = "applicationContext.xml";

//单例模式
private static Beans b = null;
private BeanFactory factory = null;
private Beans(){
System.out.println("对象被创建了");
factory = new XmlBeanFactory(new ClassPathResource("applicationContext.xml")); 
}



//获取对象工厂
private static BeanFactory getBeanFactory(){
if(b == null){
b = new Beans();
}
return b.factory;
}

//获取对象
public static Object getBean(String beanName){
return getBeanFactory().getBean(beanName);
}}日志里面记录的报的错误如下:
2012-06-18 17:07:24,953ERROR [com.pos.gui.TransMgr.initGUI(TransMgr.java:228)] org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dao' defined in class path resource [applicationContext.xml]: Cannot resolve reference to bean 'ht' while setting bean property 'hibernateTemplate'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'ht' defined in class path resource [applicationContext.xml]: Cannot resolve reference to bean 'sessionFactory' while setting bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [applicationContext.xml]: Invocation of init method failed; nested exception is java.io.FileNotFoundException: class path resource [com/pos/po] cannot be resolved to URL because it does not existapplicationContext.xml在src目录下面,com/pos/po包下方的是实体类和对应的xx.HBM.XML配置文件。
在myeclipse中启动可以正常运行,导出成可运行的jar包就报上面的错。我解压了jar包,配置文件,和依赖的jar包都引入了的,也在相应的位置。