传智播客spring2.5教程跟他写的一样怎么我的就不行啊 。。  就试了第一个save()方法就不行 火死了测试类里 打印时1能打印  2 打印不了    save()  里的personService 打印 null;
 显示空指针异常  谢谢大哥    帮我解决下    第一次整合框架   给点思路也行   该配的好像都配了   自已研究不出来了   谢谢大哥们。
public class PersonServiceTest {
private static PersonService personService;
@BeforeClass
public static void setUpBeforeClass() throws Exception {
try {System.out.println("----1-----");
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
System.out.println("----2-----");
System.out.println("applicationContext");
personService = (PersonService)applicationContext.getBean("personService");
System.out.println(personService);
} catch (Exception e) {
e.printStackTrace();
}
} @Test
public void testSave() {
System.out.println("personService:"+personService);
personService.save(new Person("小李"));
}
}
-------------------------------------------------------------------------------------------------------------
package cn.itcast.service.impl;import java.util.List;import javax.annotation.Resource;import org.hibernate.SessionFactory;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;import cn.itcast.bean.Person;
import cn.itcast.service.PersonService;
@Transactional
public class PersonServiceBean implements PersonService {

@Resource private SessionFactory sessionFactory;

public void save(Person person){
sessionFactory.getCurrentSession().persist(person);
}
}
--------------------------------------------------------------------------------------------------------------
<?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:context="http://www.springframework.org/schema/context"
       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.5.xsd
           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
 <context:annotation-config/>
 <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="driverClassName" value="org.gjt.mm.mysql.Driver"/>
    <property name="url" value="jdbc:mysql://localhost:3306/itcast?useUnicode=true&amp;characterEncoding=UTF-8"/>
    <property name="username" value="root"/>
    <property name="password" value="123"/>
     <!-- 连接池启动时的初始值 -->
 <property name="initialSize" value="1"/>
 <!-- 连接池的最大值 -->
 <property name="maxActive" value="500"/>
 <!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 -->
 <property name="maxIdle" value="2"/>
 <!--  最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 -->
 <property name="minIdle" value="1"/>
  </bean>  <bean id="sessionFactory"
  class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
  <property name="dataSource" ref="dataSource" />
  <property name="mappingResources">
  <list>
  <value>cn/itcast/bean/Person.hbm.xml</value>
  </list>
  </property>
  <property name="hibernateProperties">
  <value>
  hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
  hibernate.hbm2ddl.auto=update 
  hibernate.show_sql=false
  hibernate.format_sql=false
  </value>
  </property>
 </bean>
 
 <bean id="txManager"
  class="org.springframework.orm.hibernate3.HibernateTransactionManager">
  <property name="sessionFactory" ref="sessionFactory" />
 </bean>
 
 <tx:annotation-driven transaction-manager="txManager"/>
 
 <bean id="personService" class="cn.itcast.service.impl.PersonServiceBean"/>
</beans>

解决方案 »

  1.   

    提示说sessionFactory在bean里定义错误     帮我找找看  谢谢了。。
      

  2.   

    一夜没睡   但是得到了回报   我找出来什么毛病了   配上hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider
    就可以了      这段代码是什么意思啊    ??  能否跟我解释下?   为什么必须配?
      

  3.   

    hibernate.cache.provider_class 定制的CacheProvider的类名
    也就相当于hibernate二级缓存的驱动啦!
      

  4.   

    <property name="hibernateProperties"> 
    <value> 
    hibernate.dialect=org.hibernate.dialect.MySQL5Dialect 
    hibernate.hbm2ddl.auto=update 
    hibernate.show_sql=false 
    hibernate.format_sql=false 
    </value> 
    </property> 这错了。