运行,结果报错
java.lang.NullPointerException
at com.erp.DAOImpole.BaseDAODaoImpl.getsession(BaseDAODaoImpl.java:62)
at com.erp.DAOImpole.BaseDAODaoImpl.findByPage(BaseDAODaoImpl.java:156)
at com.test.Test.test2(Test.java:53)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)打印的结果Class Name+++++++++class com.erp.Entity.OrderInfo
session ====null这就说明没有获取到sessionFactory,请问我错在哪儿?我该怎么配置和调用呢?

解决方案 »

  1.   


    你在test2中不应new吧,应该通过spring容器取得OrderInfODAO 实例吧!
      

  2.   

    你BaseDao extends HibernateDaoSupport,在BaseDAODaoImpl中就可以取getSesson了。不用去拿sessionFactory
      

  3.   

    你测试的时候应该通过@Resource来注入orderInfODAO ,而不是去new它。
      

  4.   

    你的测试类不能通过new关键字创建实例对象,这样创建的不是spring管理的实例,当然获取不到sessionFactory. 应该这样测试: //applicationContext.xml:src目录下
    ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
    @Test
    public void testSessionFactory() {
    SessionFactory sessionFactory = (SessionFactory) context.getBean("sessionFactory");
    System.out.println("sessionFactory>>" + sessionFactory);
    }

    @Test
    public void testUserService() {
    OrderInfODAO orderInfODAO = (OrderInfODAO) context.getBean("orderInfODAO");
    List<OrderInfo> list = orderInfODAO.findByPage(orderInfo, 1, 10);
    for (OrderInfo s:list){
    System.out.println("OrderNO"+s.getOrderNo()+" "+"Buyername:"+s.getBuyerName());
    }
    }
      

  5.   

    怎么是bean.xml
    交给spring 不是applicationContext.xml吗
    DaoImpl文件继承的类不是这个
      

  6.   

    感谢各位大哥,但是我越弄越糊涂了。我也看了很多视频,以及源码,还是没弄明白。(我用的是Hibernate 4,不能用ibernateDaoSuppor)
    有几个疑问。
    第一。我将bean.xml改了一下
    <!-- 自动扫描与装配 -->
        <context:component-scan base-package="com.erp.Base,com.erp.DAO,com.erp.DAOImpole">
            <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
        
        </context:component-scan> 
    这样写是不是能保证这几个包里的注入都能扫描到?在类前都加了@Service
    第二。
    @Test
    public void testUserService() {
    OrderInfODAO orderInfODAO = (OrderInfODAO) context.getBean("orderInfODAO");
    List<OrderInfo> list = orderInfODAO.findByPage(orderInfo, 1, 10);
    for (OrderInfo s:list){
    System.out.println("OrderNO"+s.getOrderNo()+" "+"Buyername:"+s.getBuyerName());
    }
    这个方法运行后报错,报org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'orderInfoDAO' is defined,应该是找不到orderInfoDAO这个bean
    但是我用这个方法直接拿sessionFactory又能拿到。如果我要用OrderInfoDAOImple的FindByPage又该怎么使用?OrderInfoDAO和OrderInfoDAOImpl前面都加上了@Service
    第三。看一些源码里都没有用ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");,那又是怎么拿到sessionFactory的呢?源码里是整合SSH,有Service层,在Action里直接注入Service就行了吗?