最近用eclipse开发一个项目,用的是swing + spring + hibernate,在eclipse在测试一切正常,但打包后就无法使用,测试后发现是无法连接到数据库,应该是spring中的路径不对,sping中是读取的外部的properties文件,谁知道该如何配置啊

解决方案 »

  1.   

    spring配置文件如下:
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"><beans>

    <bean id="placeholderConfig"
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
    <list>
    <value>/dataSource1.properties</value>
    <value>/dataSource2.properties</value>
    </list>
    </property>
    </bean> <!-- 集成hibernate  --> 
    <bean id="dataSource"
    class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName">
    <value>${datasource.driverClassName}</value>
    </property>
    <property name="url">
    <value>jdbc:mysql://${datasource.host}:3306/${datasource.database}</value>
    </property>
    <property name="username">
    <value>${datasource.username}</value>
    </property>
    <property name="password">
    <value>${datasource.password}</value>
    </property>
    <property name="maxActive">
    <value>${datasource.maxActive}</value>
    </property>
    <property name="maxIdle">
    <value>${datasource.maxIdle}</value>
    </property>
    <property name="maxWait">
    <value>${datasource.maxWait}</value>
    </property>
    <property name="defaultAutoCommit">
    <value>${datasource.defaultAutoCommit}</value>
    </property>
    </bean> <bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource">
    <ref local="dataSource" />
    </property>
    <property name="mappingResources">
    <list>
    <value>com/customer/bean/Customerinfo.hbm.xml</value>
    <value>com/customer/bean/Messageinfo.hbm.xml</value>
    <value>com/customer/bean/Userinfo.hbm.xml</value>
    </list>
    </property>
    <property name="hibernateProperties">
    <props>
    <prop key="hibernate.dialect">
    ${hibernate.dialect}
    </prop>
    <prop key="hibernate.show_sql">
    ${hibernate.show_sql}
    </prop>
    <prop key="hibernate.jdbc.fetch_size">
    ${hibernate.jdbc.fetch_size}
    </prop>
    <prop key="hibernate.jdbc.batch_size">
    ${hibernate.jdbc.batch_size}
    </prop>
    </props>
    </property>
    </bean> <!--  
    <bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="configLocation">
    <value>classpath:com/customer/common/hibernate.cfg.xml</value>
    </property>
    </bean>
    --> <!-- 声明式事务 -->
    <bean id="transactionManager"
    class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory">
    <ref bean="sessionFactory" />
    </property>
    </bean> <!-- 事务规则 -->
    <bean id="transactionInterceptor"
    class="org.springframework.transaction.interceptor.TransactionInterceptor">
    <property name="transactionManager" ref="transactionManager" />
    <property name="transactionAttributes">
    <props>
    <prop key="add*">PROPAGATION_REQUIRED</prop>
    <prop key="del*">PROPAGATION_REQUIRED</prop>
    <prop key="update*">PROPAGATION_REQUIRED</prop>
    <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
    </props>
    </property>
    </bean> <!-- 拦截器 -->
    <bean
    class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
    <property name="beanNames">
    <value>*Service,*dao</value>
    </property>
    <property name="interceptorNames">
    <list>
    <value>transactionInterceptor</value>
    <!--  
    增加新的 Interceptor  
    -->
    </list>
    </property>
    </bean> <bean
    class="org.springframework.transaction.interceptor.TransactionAttributeSourceAdvisor">
    <property name="transactionInterceptor"
    ref="transactionInterceptor" />
    </bean>
    <!-- dao -->
    <bean id="userdao" class="com.customer.dao.impl.UserInfoDaoimpl"
    autowire="byName" /> <bean id="customerdao" class="com.customer.dao.impl.CustomerDaoImpl"
    autowire="byName" /> <bean id="messagedao"
    class="com.customer.dao.impl.MessageInfoDaoImpl" autowire="byName" /> <!-- service -->
    <bean id="userService"
    class="com.customer.service.impl.UserServiceImpl">
    <property name="userdao">
    <ref local="userdao" />
    </property>
    </bean> <bean id="customerService"
    class="com.customer.service.impl.CustomerServiceImpl">
    <property name="customerdao">
    <ref local="customerdao" />
    </property>
    </bean> <bean id="messageService"
    class="com.customer.service.impl.MessageServiceImpl">
    <property name="messageInfoDao">
    <ref local="messagedao" />
    </property>
    </bean>
    </beans>
      

  2.   

    如何读取的文件?
    property文件是放在哪里的
    使用类路径方式读取应该不会有问题
      

  3.   

    property文件放在jar包同一个目录下
    读取bean是用ApplicationContext apc = new ClassPathXmlApplicationContext("applicationContext.xml");