用AnnotationSessionFactoryBean生成sessionFactory时报错不能创建sessionFactoyBean

报错信息:

[junit] Testcase: testCreateFileInfo(springmp3.service.FileDaoImplTests):Caused an ERROR
    [junit]
Error creating bean with name 'sessionFactory' defined in URL [applicationContext.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean]: Constructor threw exception; nested exception is java.lang.NoClassDefFoundError: org/dom4j/DocumentException
    [junit] org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in URL [applicationContext.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean]: Constructor threw exception; nested exception is java.lang.NoClassDefFoundError: org/dom4j/DocumentException
    [junit] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:813)
    [junit] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:769)
    [junit] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:412)
    [junit] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:383)
    [junit] at java.security.AccessController.doPrivileged(Native Method)

spring配置信息:

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="annotatedClasses">
<list>
<value>domain.FileInfo</value>
</list>
</property>

DAO方法:

public class FileDaoImplTests extends AbstractTransactionalDataSourceSpringContextTests {


   public void testCreateFileInfo() throws Exception{
       
   FileInfo inf = new FileInfo();
   // Arbitrary test information for this "file"
   inf.setFilename("FILE NAME");
   inf.setTitle("TITLE");
   inf.setComment("TEST COMMENT");
  
   assertNull("ID shall not be zero",inf.getId());
  
  
   FileDaoImpl dao = new FileDaoImpl();
   // making the fileinfo persistent in database:
   Integer res = dao.SaveFileInfo(inf);
   assertTrue("wrong result by FileDaoImpl",res.intValue()>0);
   assertNotNull("ID must be non-zero",inf.getId());
   }
  
    @Override
    protected String[] getConfigLocations() {
        return new String[]{"applicationContext.xml"};
    }
}

原因是我没有导入spring_home\lib\dom4j\下的dom4j.jar和jta.jar,导入即可!

你可能感兴趣的:(java,DAO,spring,bean,JUnit)