2015-03-31 104 views
1

我是Spring中的新成員hibernate.I試圖完成一個包含下面的文件的簡單應用程序。 1.Employee.java嵌套異常是java.lang.NoClassDefFoundError:org/slf4j/impl/MessageFormatter

private int id; 
    private String name; 
    private float salary; 
//and setter and getter 

2.Employee.hbm.xml(包含所有映射的詳細信息)

3.EmployeeDao.java

public class EmployeeDao { 
      HibernateTemplate template; 

    public void setTemplate(HibernateTemplate template) { 
     this.template = template; 
    } 
    //method to save employee 
    public void saveEmployee(Employee e){ 
     template.save(e); 
    } 
    //method to update employee 
    public void updateEmployee(Employee e){ 
     template.update(e); 
    } 
    //method to delete employee 
    public void deleteEmployee(Employee e){ 
     template.delete(e); 
    } 
    //method to return one employee of given id 
    public Employee getById(int id){ 
     Employee e=(Employee)template.get(Employee.class,id); 
     return e; 
    } 
    //method to return all employees 
    public List<Employee> getEmployees(){ 
     List<Employee> list=new ArrayList<Employee>(); 
     list=template.loadAll(Employee.class); 
     return list; 
    } 
    } 

4.applicationContext.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:context="http://www.springframework.org/schema/context" 
    xmlns:lang="http://www.springframework.org/schema/lang" 
    xmlns:p="http://www.springframework.org/schema/p" 
    xmlns:task="http://www.springframework.org/schema/task" 
    xmlns:tx="http://www.springframework.org/schema/tx" 
    xmlns:util="http://www.springframework.org/schema/util" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd 
     http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-3.0.xsd 
     http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd 
     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd 
     http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd"> 

<bean id="dataSource1" class="org.apache.commons.dbcp.BasicDataSource"> 
     <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> 
     <property name="url" value="jdbc:mysql://localhost:3306/prasanta"></property> 
     <property name="username" value="root"></property> 
     <property name="password" value=""></property> 
</bean> 
<bean id="mysessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> 
     <property name="dataSource" ref="dataSource1"></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="template1" class="org.springframework.orm.hibernate3.HibernateTemplate"> 
    <property name="sessionFactory" ref="mysessionFactory"></property> 
    </bean> 
<bean id="d" class="EmployeeDao"> 
    <property name="template" ref="template1"></property> 
    </bean> 
</beans> 

5.Test.java如下:

import org.springframework.beans.factory.BeanFactory; 
import org.springframework.beans.factory.xml.XmlBeanFactory; 
import org.springframework.core.io.ClassPathResource; 
import org.springframework.core.io.Resource; 

public class Test { 
     public static void main(String[] args) { 

     Resource r=new ClassPathResource("applicationContext.xml"); 
     BeanFactory factory=new XmlBeanFactory(r); 

     EmployeeDao dao=(EmployeeDao)factory.getBean("d"); 

     Employee e=new Employee(); 
     e.setId(114); 
     e.setName("arun"); 
     e.setSalary(50000); 

     dao.saveEmployee(e); 

    } 
} 

而且我在構建路徑中添加了bellow jar文件。

spring-orm-3.0.3.RELEASE.jar 
spring-expression-3.0.2.RELEASE.jar 
spring-core-3.0.3.RELEASE.jar 
spring-context-3.0.3.RELEASE.jar 
spring-beans-3.0.3.RELEASE.jar 
spring-asm-3.0.3.RELEASE.jar 
spring.jar 
mysql-connector-java-5.1.6.jar 
commons-dbcp-1.2.jar 
com.springsource.org.hibernate.annotations.common-3.3.0.ga.jar 
commons-logging-1.1.jar 
hibernate-entitymanager-3.6.3.Final.jar 
hibernate-core-3.6.3.Final.jar 
org.apache.commons.pool.jar 
javax.transaction.jar 
hibernate-commons-annotations-4.0.3.final.jar 
hibernate-commons-annotations-3.3.0.ga.jar 
dom4j.jar 
slf4j.jar 
logback-core-0.9.6.jar 
logback-classic-0.9.jar 
slf4j.api-1.6.1.jar 

當試圖運行應用程序。它拋出異常波紋管:

Mar 31, 2015 7:23:48 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions 
INFO: Loading XML bean definitions from class path resource [applicationContext.xml] 
Failed to instantiate [ch.qos.logback.classic.LoggerContext] 
Reported exception: 
java.lang.NoSuchMethodError: ch.qos.logback.classic.util.ContextInitializer.<init>(Lch/qos/logback/classic/LoggerContext;)V 
    at org.slf4j.impl.StaticLoggerBinder.init(StaticLoggerBinder.java:85) 
    at org.slf4j.impl.StaticLoggerBinder.<clinit>(StaticLoggerBinder.java:56) 
    at org.slf4j.LoggerFactory.getSingleton(LoggerFactory.java:189) 
    at org.slf4j.LoggerFactory.bind(LoggerFactory.java:112) 
    at org.slf4j.LoggerFactory.performInitialization(LoggerFactory.java:105) 
    at org.slf4j.LoggerFactory.getILoggerFactory(LoggerFactory.java:235) 
    at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:208) 
    at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:221) 
    at org.hibernate.cfg.Configuration.<clinit>(Configuration.java:197) 
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) 
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57) 
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) 
    at java.lang.reflect.Constructor.newInstance(Constructor.java:526) 
    at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:126) 
    at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:104) 
    at org.springframework.orm.hibernate3.LocalSessionFactoryBean.newConfiguration(LocalSessionFactoryBean.java:813) 
    at org.springframework.orm.hibernate3.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:549) 
    at org.springframework.orm.hibernate3.AbstractSessionFactoryBean.afterPropertiesSet(AbstractSessionFactoryBean.java:211) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1469) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1409) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456) 
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291) 
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190) 
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:322) 
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:106) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1317) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1076) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456) 
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291) 
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190) 
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:322) 
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:106) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1317) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1076) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456) 
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291) 
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190) 
    at Test.main(InsertTest.java:14) 
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'd' defined in class path resource [applicationContext.xml]: Cannot resolve reference to bean 'template1' while setting bean property 'template'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'template1' defined in class path resource [applicationContext.xml]: Cannot resolve reference to bean 'mysessionFactory' while setting bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mysessionFactory' defined in class path resource [applicationContext.xml]: Invocation of init method failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.hibernate.cfg.Configuration]: Constructor threw exception; nested exception is java.lang.NoClassDefFoundError: org/slf4j/impl/MessageFormatter 
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:328) 
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:106) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1317) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1076) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456) 
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291) 
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190) 
    at Test.main(InsertTest.java:14) 
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'template1' defined in class path resource [applicationContext.xml]: Cannot resolve reference to bean 'mysessionFactory' while setting bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mysessionFactory' defined in class path resource [applicationContext.xml]: Invocation of init method failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.hibernate.cfg.Configuration]: Constructor threw exception; nested exception is java.lang.NoClassDefFoundError: org/slf4j/impl/MessageFormatter 
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:328) 
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:106) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1317) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1076) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456) 
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291) 
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190) 
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:322) 
    ... 10 more 
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mysessionFactory' defined in class path resource [applicationContext.xml]: Invocation of init method failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.hibernate.cfg.Configuration]: Constructor threw exception; nested exception is java.lang.NoClassDefFoundError: org/slf4j/impl/MessageFormatter 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1412) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456) 
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291) 
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190) 
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:322) 
    ... 20 more 
Caused by: org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.hibernate.cfg.Configuration]: Constructor threw exception; nested exception is java.lang.NoClassDefFoundError: org/slf4j/impl/MessageFormatter 
    at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:141) 
    at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:104) 
    at org.springframework.orm.hibernate3.LocalSessionFactoryBean.newConfiguration(LocalSessionFactoryBean.java:813) 
    at org.springframework.orm.hibernate3.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:549) 
    at org.springframework.orm.hibernate3.AbstractSessionFactoryBean.afterPropertiesSet(AbstractSessionFactoryBean.java:211) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1469) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1409) 
    ... 27 more 
Caused by: java.lang.NoClassDefFoundError: org/slf4j/impl/MessageFormatter 
    at ch.qos.logback.classic.spi.LoggingEvent.<init>(LoggingEvent.java:114) 
    at ch.qos.logback.classic.Logger.filterAndLog(Logger.java:421) 
    at ch.qos.logback.classic.Logger.filterAndLog(Logger.java:430) 
    at ch.qos.logback.classic.Logger.debug(Logger.java:390) 
    at org.hibernate.type.BasicTypeRegistry.register(BasicTypeRegistry.java:147) 
    at org.hibernate.type.BasicTypeRegistry.<init>(BasicTypeRegistry.java:50) 
    at org.hibernate.type.TypeResolver.<init>(TypeResolver.java:59) 
    at org.hibernate.cfg.Configuration.<init>(Configuration.java:250) 
    at org.hibernate.cfg.Configuration.<init>(Configuration.java:302) 
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) 
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57) 
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) 
    at java.lang.reflect.Constructor.newInstance(Constructor.java:526) 
    at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:126) 
    ... 33 more 
Caused by: java.lang.ClassNotFoundException: org.slf4j.impl.MessageFormatter 
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366) 
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354) 
    at java.lang.ClassLoader.loadClass(ClassLoader.java:425) 
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308) 
    at java.lang.ClassLoader.loadClass(ClassLoader.java:358) 
    ... 47 more 

我也加入三個罐子

cglib-2.2.2.jar 
log4j-1.2.15.jar 
slf4j-log4j12-1.5.6.jar 

嘗試,得到了相同的結果。

請幫我解決這個問題

回答

0

logback-classic-0.9.6.jarContextInitializer.java不支持下面的構造,這就是爲什麼你正在運行到這個問題。

缺少構造

public ContextInitializer(LoggerContext loggerContext) { 
    this.loggerContext = loggerContext; 
    } 

嘗試升級到logback-classic-1.0.0.jar版本。

也切換到slf4j-log4j12-1.0.1.jar

相關問題