没有加载spring的配置文件

解决方案 »

  1.   

    我的hibernate的配置文件hibernate.cfg.xml配置正确,单独测试通过,内容如下:
    <?xml version='1.0' encoding='UTF-8'?>
    <!DOCTYPE hibernate-configuration PUBLIC
              "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
              "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"><!-- Generated by MyEclipse Hibernate Tools.                   -->
    <hibernate-configuration> <session-factory>
    <property name="hibernate.connection.driver_class">
    com.mysql.jdbc.Driver
    </property>
    <property name="hibernate.connection.url">
    jdbc:mysql://localhost/SAMPLEDB
    </property>
    <property name="hibernate.connection.username">root</property>
    <property name="hibernate.connection.password">111111</property>
    <property name="hibernate.connection.pool.size">20</property>
    <property name="hibernate.show_sql">true</property>
    <property name="jdbc.fetch_size">50</property>
    <property name="jdbc.batch_size">25</property>
    <property name="hibernate.dialect">
    org.hibernate.dialect.MySQLDialect
    </property>
    <property name="jdbc.use_scrollable_resultset">false</property> <!-- Mapping files -->

    <mapping resource="com/jandar/model/User.hbm.xml" /> </session-factory></hibernate-configuration>
    1请问大家spring的配置文件还应该怎样配置?
      

  2.   

    <beans>
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="driverClassName">
    <value>org.gjt.mm.mysql.Driver</value>
    </property>
    <property name="url">
    <value>jdbc:mysql://localhost/example</value>
    </property>
    <property name="username">
    <value>root</value>
    </property>
    <property name="password">
    <value></value>
    </property>
    </bean>
    <bean id="sessionFactory" class="org.springframework.orm.hibernate.LocalSessionFactoryBean">
      <property name="dataSource">
        <ref local="dataSource"/>
      </property>
      <property name="mappingResources">
        <list>
          <value>eqzhou/AAA.hbm.xml</value>
        </list>
      </property>
      <property name="hibernateProperties">
        <props>
          <prop key="hibernate.dialect">net.sf.hibernate.dialect.MySQLDialect</prop>
          <prop key="hibernate.show_sql">true</prop>
        </props>
      </property>
    </bean>
    然后如果你的其他的类要用sessionFactory,则需要将sessionFactory注入其中。
    然后在应用的时候,初始化你的Spring 容器。你的程序好象没有这个步骤。如果是web应用,则可以在web.xml中进行配置,在web容器初始化的时候就加载spring容器。
      

  3.   

    zeq258(近朱者赤) ,我把你的代码加到我的appconext.xml,并修改我的BusinessService中的代码:
    package mypack;import java.util.Iterator;
    import java.util.List;import org.apache.commons.logging.Log;
    import org.apache.commons.logging.LogFactory;
    import org.hibernate.Session;
    import org.hibernate.SessionFactory;
    import org.hibernate.Transaction;
    import org.springframework.beans.factory.BeanFactory;
    import org.springframework.beans.factory.xml.XmlBeanFactory;
    import org.springframework.core.io.ClassPathResource;
    import org.springframework.core.io.Resource;
    import org.springframework.orm.hibernate.LocalSessionFactoryBean;
    import org.springframework.orm.hibernate.support.HibernateDaoSupport;BusinessService{
      public BusinessService(){
             Resource resource=new ClassPathResource("appcontext.xml");
          BeanFactory factory= new XmlBeanFactory(resource);
          LocalSessionFactoryBean sessionFactory2=(LocalSessionFactoryBean)factory.getBean("sessionFactory");

     }
     public static void main(String args[]) throws Exception {
    BusinessService t=new BusinessService();
    t.getCustomers();
    //sessionFactory2.close();
     }
    }

     LocalSessionFactoryBean sessionFactory2=(LocalSessionFactoryBean)factory.getBean("sessionFactory");
    这句在运行时出现以下错误:
    java.lang.ClassCastException
    请指教!
      

  4.   

    java.lang.ClassCastException
    造型错误,请仔细检查!