2017-10-28 214 views
0

您好我已經實現了spring和hibernate整合我有一個例外,同時集成的例外是:異常運行春季休眠應用

請幫我解決這個異常:

Exception in thread "main" java.lang.ExceptionInInitializerError 
at org.springframework.context.support.AbstractRefreshableApplicationContext.createBeanFactory(AbstractRefreshableApplicationContext.java:194) 
at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:127) 
at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:465) 
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:395) 
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139) 
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83) 
at test.InsertTest.main(InsertTest.java:14) 

這裏是xml文件:

<?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:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> 


    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> 
     <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> 
     <property name="url" value="jdbc:mysql://localhost:3306/test"></property> 
     <property name="username" value="root"></property> 
     <property name="password" value="root"></property> 
    </bean> 

    <bean id="mysessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> 
     <property name="dataSource" ref="dataSource"></property> 

     <property name="mappingResources"> 
     <list> 
     <value>employee.hbm.xml</value> 
     </list> 
     </property> 

     <property name="hibernateProperties"> 
      <props> 
       <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> 
       <prop key="hibernate.hbm2ddl.auto">update</prop> 
       <prop key="hibernate.show_sql">true</prop> 

      </props> 
     </property> 
    </bean> 

    <bean id="template" class="org.springframework.orm.hibernate3.HibernateTemplate"> 
    <property name="sessionFactory" ref="mysessionFactory"></property> 
    </bean> 

    <bean id="d" class="test.EmployeeDao"> 
    <property name="template" ref="template"></property> 
    </bean> 


    </beans> 

這裏的示例程序:

> 
package test; 

import org.springframework.beans.factory.BeanFactory; 

import org.springframework.beans.factory.xml.XmlBeanFactory; 

import org.springframework.context.ApplicationContext; 

import org.springframework.context.support.ClassPathXmlApplicationContext; 

import org.springframework.core.io.ClassPathResource; 

import org.springframework.core.io.Resource; 

public class InsertTest { 

    public static void main(String[] args) { 
     // TODO Auto-generated method stub 
     // Resource r=new ClassPathResource("test.xml"); 
      // BeanFactory factory=new XmlBeanFactory(r);  
     ApplicationContext context =new ClassPathXmlApplicationContext("applicationContext.xml");  
     EmployeeDao dao=(EmployeeDao)context.getBean("d"); 
      Employee e=new Employee(); 
      e.setId(114); 
      e.setName("varun"); 
      e.setSalary(50000);  
      dao.saveEmployee(e); 
    } 

} 
+0

這是完整的異常堆棧跟蹤?在這部分跟蹤下面應該有一個「由...引起」。 – Vasan

+0

沒有顯示它只顯示完整的堆棧跟蹤這裏是堆棧跟蹤的屏幕截圖:http://i.prntscr.com/tMMIezsXSDunb8cSVXmybg.png –

+0

你可以檢查'applicationContext.xml'是否在類路徑和可讀?或者嘗試使用'FileSystemXmlApplicationContext'並提供完整路徑,而不是'ClassPathXmlApplicationContext'。如果這不能解決它,請將您的java/spring版本和JAR發佈到classpath中。這可能是JAR衝突或老春天的一個已知問題。 – Vasan

回答

0

只是刪除所有已添加的JAR文件,然後嘗試在每個jar文件仔細不會添加已經添加jar文件,這將工作我已經測試和工作礦山