spring集成 hibernate应用后 运行测试类抱下面错误:javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file:  java.naming.factory.initial下面是我配置文件的片段!
<!-- 配置 SessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
</bean>

<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>

<!-- 配置事务的传播特性 -->
<tx:advice id="txadvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="insert*" propagation="REQUIRED"/>
<tx:method name="del*" propagation="REQUIRED"/>
<tx:method name="*" read-only="true"/>
</tx:attributes>
</tx:advice>

<!-- 哪些方法参加事务 -->
<aop:config>
<aop:pointcut id="allmethod" expression="execution(* cn.orc.jie.spring.*.*(..))"/>
<aop:advisor advice-ref="txadvice" pointcut-ref="allmethod"/>
</aop:config>
        
        <!-- 主应用事务类 -->
<bean id="userManager" class="cn.orc.jie.spring.UserManagerImp1">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>本人在线等待 有什么对问题不清楚的 提出来我在详细说明 谢谢大家了!!

解决方案 »

  1.   

    11:21:28,718  WARN SessionFactoryObjectFactory:98 - Could not bind factory to JNDI
      

  2.   

    贴下web.xml  hibernate.cfg.xml 最好加个代码框
      

  3.   

    还有包没有指定...是spring里面指定的....
      

  4.   

    楼主回复:  
      第一 我是java 项目 没有 web.xml 
      第二 我的包应该都引入了啊! 不管是spring 的还是 hibernate的
      

  5.   

    楼住回复:<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"><hibernate-configuration>
    <session-factory name="foo">
    <property name="show_sql">true</property>
    <property name="connection.username">root</property>
            <property name="connection.url">jdbc:mysql://localhost:3306/spring_hibernate_test2</property>
            <property name="connection.password">root</property>
            <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
            <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
            <property name="myeclipse.connection.profile">com.mysql.jdbc.Driver</property>
            <property name="hbm2ddl.auto">update</property> <mapping resource="cn/orc/jie/hibernate/User.hbm.xml"/>
    </session-factory>
    </hibernate-configuration>
      

  6.   

    我说的是spring中要用配置文件指定的...
      

  7.   

    既然lz是java工程,那么在类要使用spring管理的东西的时候就要实例化BeanFactory例如BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext.xml");后面的例如按ID取BEANfactory.getBean("..");
      

  8.   

    给你贴下官方reference里的内容
      

  9.   

    <session-factory name="foo">
    改为
    <session-factory name="sessionFactory">
    或者去掉<session-factory name="foo">
      

  10.   

    在web项目中,可以在web.xml中配置监听器,启动spring容器,但你的是java项目,需要手工去读取applicationContext.xml启动容器,像7楼所说的那样
      

  11.   

    建议像9楼所说的那样不要修改直接将<session-factory name="foo">去掉,
    那个只用在j2ee容器下或支持jndi动态绑定的容器。