spring - Injection of autowired dependencies failed; nested exception is java.lang.NoClassDefFoundError: org/hibernate/cfg/Configuration -
i new @ java spring. following tutorial, faced problem couldn't solve. getting "org.springframework.beans.factory.beancreationexception: error creating bean name 'homecontroller': injection of autowired dependencies failed; nested exception java.lang.noclassdeffounderror: org/hibernate/cfg/configuration" error when tried run application.
before sharing code snippets, tell have found similar questions mine (for example[caused by: java.lang.noclassdeffounderror: org/hibernate/cfg/configuration), couldn't find answer own question. have found answers says need configure build path, every answer eclipse. using intellij idea , couldn't find correct solution on intellij. hope question can more clear guys tell me root cause of problem.
pom.xml
<?xml version="1.0" encoding="utf-8"?> <project xmlns="http://maven.apache.org/pom/4.0.0" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="http://maven.apache.org/pom/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelversion>4.0.0</modelversion> <groupid>com.mywebsite</groupid> <artifactid>emusicstore</artifactid> <version>1.0-snapshot</version> <dependencies> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-webmvc</artifactid> <version>4.3.3.release</version> </dependency> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-core</artifactid> <version>4.3.3.release</version> </dependency> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-orm</artifactid> <version>4.3.3.release</version> </dependency> <dependency> <groupid>jstl</groupid> <artifactid>jstl</artifactid> <version>1.2</version> </dependency> <dependency> <groupid>taglibs</groupid> <artifactid>standard</artifactid> <version>1.1.2</version> </dependency> <dependency> <groupid>org.hibernate</groupid> <artifactid>hibernate-core</artifactid> <version>5.2.4.final</version> </dependency> <dependency> <groupid>org.hibernate.javax.persistance</groupid> <artifactid>hibernate-jpa-2.0-api</artifactid> <version>1.0.1.final</version> </dependency> <dependency> <groupid>com.h2database</groupid> <artifactid>h2</artifactid> <version>1.4.193</version> </dependency> </dependencies>
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" xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- creating database connection here --> <bean id="datasource" class="org.springframework.jdbc.datasource.drivermanagerdatasource"> <property name="driverclassname" value="org.h2.driver"/> <property name="url" value=" jdbc:h2:~/test" /> <property name="username" value="sa" /> <property name="password" value="" /> </bean> <bean id="sessionfactory" class="org.springframework.orm.hibernate4.localsessionfactorybean"> <property name="datasource" ref="datasource"></property> <property name="hibernateproperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.h2dialect</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.format_sql">true</prop> </props> </property> <property name="packagestoscan"> <list> <value>com.emusicstore</value> </list> </property> </bean> <bean id="transactionmanager" class="org.springframework.orm.hibernate4.hibernatetransactionmanager"> <property name="sessionfactory" ref="sessionfactory"/> </bean>
web.xml
<?xml version="1.0" encoding="utf-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <listener> <listener-class>org.springframework.web.context.contextloaderlistener</listener-class> </listener> <context-param> <param-name>contextconfiglocation</param-name> <param-value> /web-inf/dispatcher-servlet.xml, /web-inf/applicationcontext.xml </param-value> </context-param> <servlet> <servlet-name>dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.dispatcherservlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>
dispatcherservlet.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:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemalocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <context:component-scan base-package="com.emusicstore" /> <mvc:annotation-driven /> <bean id="viewresolver" class="org.springframework.web.servlet.view.internalresourceviewresolver"> <property name="prefix" value="/web-inf/views/" /> <property name="suffix" value=".jsp" /> </bean> <mvc:resources mapping="/resources/**" location="/web-inf/resources/" /> <tx:annotation-driven/>
homecontroller
package com.emusicstore.controller; import com.emusicstore.dao.productdao; import com.emusicstore.model.product; import org.springframework.beans.factory.annotation.autowired; import org.springframework.stereotype.controller; import org.springframework.ui.model; import org.springframework.web.bind.annotation.pathvariable; import org.springframework.web.bind.annotation.requestmapping; import java.io.ioexception; import java.util.list; @controller public class homecontroller { @autowired private productdao productdao; @requestmapping("/") public string home() { return "home"; } @requestmapping("/productlist") public string getproducts(model model) { list<product> products = productdao.getallproducts(); model.addattribute("products", products); return "productlist"; } @requestmapping("/productlist/viewproduct/{productid}") public string viewproduct(@pathvariable string productid, model model) throws ioexception{ product product = productdao.getproductbyid(productid); model.addattribute(product); return "viewproduct"; } }
productdaoimpl
package com.emusicstore.dao.impl; import com.emusicstore.dao.productdao; import com.emusicstore.model.product; import org.hibernate.session; import org.hibernate.sessionfactory; import org.springframework.beans.factory.annotation.autowired; import org.springframework.stereotype.repository; import org.hibernate.query; import org.springframework.transaction.annotation.transactional; import java.util.list; @repository @transactional public class productdaoimpl implements productdao { @autowired private sessionfactory sessionfactory; public void addproduct(product product) { session session = sessionfactory.getcurrentsession(); session.saveorupdate(product); session.flush(); } public product getproductbyid(string id){ session session = sessionfactory.getcurrentsession(); product product = (product) session.get(product.class, id); session.flush(); return product; } public list<product> getallproducts(){ session session = sessionfactory.getcurrentsession(); query query = session.createquery("from product"); list<product> products = query.list(); session.flush(); return products; } public void deleteproduct(string id) { session session = sessionfactory.getcurrentsession(); session.delete(getproductbyid(id)); session.flush(); } }
productdao
package com.emusicstore.dao; import com.emusicstore.model.product; import java.util.list; public interface productdao { void addproduct(product product); product getproductbyid(string id); list<product> getallproducts(); void deleteproduct(string id); }
the error got below:
org.springframework.beans.factory.beancreationexception: error creating bean name 'homecontroller': injection of autowired dependencies failed; nested exception java.lang.noclassdeffounderror: org/hibernate/cfg/configuration @ org.springframework.beans.factory.annotation.autowiredannotationbeanpostprocessor.postprocesspropertyvalues(autowiredannotationbeanpostprocessor.java:355) @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.populatebean(abstractautowirecapablebeanfactory.java:1219) @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.docreatebean(abstractautowirecapablebeanfactory.java:543) @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.createbean(abstractautowirecapablebeanfactory.java:482) @ org.springframework.beans.factory.support.abstractbeanfactory$1.getobject(abstractbeanfactory.java:306) @ org.springframework.beans.factory.support.defaultsingletonbeanregistry.getsingleton(defaultsingletonbeanregistry.java:230) @ org.springframework.beans.factory.support.abstractbeanfactory.dogetbean(abstractbeanfactory.java:302) @ org.springframework.beans.factory.support.abstractbeanfactory.getbean(abstractbeanfactory.java:197) @ org.springframework.beans.factory.support.defaultlistablebeanfactory.preinstantiatesingletons(defaultlistablebeanfactory.java:751) @ org.springframework.context.support.abstractapplicationcontext.finishbeanfactoryinitialization(abstractapplicationcontext.java:861) @ org.springframework.context.support.abstractapplicationcontext.refresh(abstractapplicationcontext.java:541) @ org.springframework.web.context.contextloader.configureandrefreshwebapplicationcontext(contextloader.java:444) @ org.springframework.web.context.contextloader.initwebapplicationcontext(contextloader.java:326) @ org.springframework.web.context.contextloaderlistener.contextinitialized(contextloaderlistener.java:107) @ org.apache.catalina.core.standardcontext.listenerstart(standardcontext.java:4715) @ org.apache.catalina.core.standardcontext.startinternal(standardcontext.java:5177) @ org.apache.catalina.util.lifecyclebase.start(lifecyclebase.java:152) @ org.apache.catalina.core.containerbase.addchildinternal(containerbase.java:724) @ org.apache.catalina.core.containerbase.addchild(containerbase.java:700) @ org.apache.catalina.core.standardhost.addchild(standardhost.java:734) @ org.apache.catalina.startup.hostconfig.manageapp(hostconfig.java:1702) @ sun.reflect.nativemethodaccessorimpl.invoke0(native method) @ sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:62) @ sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:43) @ java.lang.reflect.method.invoke(method.java:497) @ org.apache.tomcat.util.modeler.basemodelmbean.invoke(basemodelmbean.java:300) @ com.sun.jmx.interceptor.defaultmbeanserverinterceptor.invoke(defaultmbeanserverinterceptor.java:819) @ com.sun.jmx.mbeanserver.jmxmbeanserver.invoke(jmxmbeanserver.java:801) @ org.apache.catalina.mbeans.mbeanfactory.createstandardcontext(mbeanfactory.java:482) @ org.apache.catalina.mbeans.mbeanfactory.createstandardcontext(mbeanfactory.java:431) @ sun.reflect.nativemethodaccessorimpl.invoke0(native method) @ sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:62) @ sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:43) @ java.lang.reflect.method.invoke(method.java:497) @ org.apache.tomcat.util.modeler.basemodelmbean.invoke(basemodelmbean.java:300) @ com.sun.jmx.interceptor.defaultmbeanserverinterceptor.invoke(defaultmbeanserverinterceptor.java:819) @ com.sun.jmx.mbeanserver.jmxmbeanserver.invoke(jmxmbeanserver.java:801) @ javax.management.remote.rmi.rmiconnectionimpl.dooperation(rmiconnectionimpl.java:1471) @ javax.management.remote.rmi.rmiconnectionimpl.access$300(rmiconnectionimpl.java:76) @ javax.management.remote.rmi.rmiconnectionimpl$privilegedoperation.run(rmiconnectionimpl.java:1312) @ javax.management.remote.rmi.rmiconnectionimpl.doprivilegedoperation(rmiconnectionimpl.java:1404) @ javax.management.remote.rmi.rmiconnectionimpl.invoke(rmiconnectionimpl.java:832) @ sun.reflect.nativemethodaccessorimpl.invoke0(native method) @ sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:62) @ sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:43) @ java.lang.reflect.method.invoke(method.java:497) @ sun.rmi.server.unicastserverref.dispatch(unicastserverref.java:323) @ sun.rmi.transport.transport$1.run(transport.java:200) @ sun.rmi.transport.transport$1.run(transport.java:197) @ java.security.accesscontroller.doprivileged(native method) @ sun.rmi.transport.transport.servicecall(transport.java:196) @ sun.rmi.transport.tcp.tcptransport.handlemessages(tcptransport.java:568) @ sun.rmi.transport.tcp.tcptransport$connectionhandler.run0(tcptransport.java:826) @ sun.rmi.transport.tcp.tcptransport$connectionhandler.lambda$run$256(tcptransport.java:683) @ java.security.accesscontroller.doprivileged(native method) @ sun.rmi.transport.tcp.tcptransport$connectionhandler.run(tcptransport.java:682) @ java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1142) @ java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:617) @ java.lang.thread.run(thread.java:745) caused by: java.lang.noclassdeffounderror: org/hibernate/cfg/configuration @ java.lang.class.getdeclaredmethods0(native method) @ java.lang.class.privategetdeclaredmethods(class.java:2701) @ java.lang.class.getdeclaredmethods(class.java:1975) @ org.springframework.util.reflectionutils.getdeclaredmethods(reflectionutils.java:612) @ org.springframework.util.reflectionutils.dowithmethods(reflectionutils.java:524) @ org.springframework.util.reflectionutils.dowithmethods(reflectionutils.java:510) @ org.springframework.beans.factory.annotation.autowiredannotationbeanpostprocessor.determinecandidateconstructors(autowiredannotationbeanpostprocessor.java:243) @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.determineconstructorsfrombeanpostprocessors(abstractautowirecapablebeanfactory.java:1074) @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.createbeaninstance(abstractautowirecapablebeanfactory.java:1047) @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.getsingletonfactorybeanfortypecheck(abstractautowirecapablebeanfactory.java:870) @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.gettypeforfactorybean(abstractautowirecapablebeanfactory.java:797) @ org.springframework.beans.factory.support.abstractbeanfactory.istypematch(abstractbeanfactory.java:544) @ org.springframework.beans.factory.support.defaultlistablebeanfactory.dogetbeannamesfortype(defaultlistablebeanfactory.java:422) @ org.springframework.beans.factory.support.defaultlistablebeanfactory.getbeannamesfortype(defaultlistablebeanfactory.java:398) @ org.springframework.beans.factory.beanfactoryutils.beannamesfortypeincludingancestors(beanfactoryutils.java:220) @ org.springframework.beans.factory.support.defaultlistablebeanfactory.findautowirecandidates(defaultlistablebeanfactory.java:1240) @ org.springframework.beans.factory.support.defaultlistablebeanfactory.doresolvedependency(defaultlistablebeanfactory.java:1091) @ org.springframework.beans.factory.support.defaultlistablebeanfactory.resolvedependency(defaultlistablebeanfactory.java:1056) @ org.springframework.beans.factory.annotation.autowiredannotationbeanpostprocessor$autowiredfieldelement.inject(autowiredannotationbeanpostprocessor.java:566) @ org.springframework.beans.factory.annotation.injectionmetadata.inject(injectionmetadata.java:88) @ org.springframework.beans.factory.annotation.autowiredannotationbeanpostprocessor.postprocesspropertyvalues(autowiredannotationbeanpostprocessor.java:349) ... 58 more caused by: java.lang.classnotfoundexception: org.hibernate.cfg.configuration @ org.apache.catalina.loader.webappclassloaderbase.loadclass(webappclassloaderbase.java:1280) @ org.apache.catalina.loader.webappclassloaderbase.loadclass(webappclassloaderbase.java:1114) ... 79 more
any appreciated.
i think it's hibernate-core jar. can use this:
<dependency> <groupid>org.hibernate</groupid> <artifactid>hibernate-core</artifactid> <version>4.0.0.final</version> </dependency>
Comments
Post a Comment