spring - No qualifying bean of type [com.sprhib.dao.UserDao] found for dependency -
i newbie spring framework. trying learn it. have time same problem ioc. cant correctly implement it. please help.
i have problem @autowiring classes. error stack:
cze 27, 2014 12:03:56 pm org.springframework.web.context.contextloader initwebapplicationcontext severe: context initialization failed org.springframework.beans.factory.beancreationexception: error creating bean name 'usercontroller': injection of autowired dependencies failed; nested exception org.springframework.beans.factory.beancreationexception: not autowire field: private com.sprhib.dao.userdao com.sprhib.controller.usercontroller.userdao; nested exception org.springframework.beans.factory.nosuchbeandefinitionexception: no qualifying bean of type [com.sprhib.dao.userdao] found dependency: expected @ least 1 bean qualifies autowire candidate dependency. dependency annotations: {@org.springframework.beans.factory.annotation.autowired(required=true)} @ org.springframework.beans.factory.annotation.autowiredannotationbeanpostprocessor.postprocesspropertyvalues(autowiredannotationbeanpostprocessor.java:288) @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.populatebean(abstractautowirecapablebeanfactory.java:1122) @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.docreatebean(abstractautowirecapablebeanfactory.java:522) @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.createbean(abstractautowirecapablebeanfactory.java:461) @ org.springframework.beans.factory.support.abstractbeanfactory$1.getobject(abstractbeanfactory.java:295) @ org.springframework.beans.factory.support.defaultsingletonbeanregistry.getsingleton(defaultsingletonbeanregistry.java:223) @ org.springframework.beans.factory.support.abstractbeanfactory.dogetbean(abstractbeanfactory.java:292) @ org.springframework.beans.factory.support.abstractbeanfactory.getbean(abstractbeanfactory.java:194) @ org.springframework.beans.factory.support.defaultlistablebeanfactory.preinstantiatesingletons(defaultlistablebeanfactory.java:626) @ org.springframework.context.support.abstractapplicationcontext.finishbeanfactoryinitialization(abstractapplicationcontext.java:932) @ org.springframework.context.support.abstractapplicationcontext.refresh(abstractapplicationcontext.java:479) @ org.springframework.web.context.contextloader.configureandrefreshwebapplicationcontext(contextloader.java:389) @ org.springframework.web.context.contextloader.initwebapplicationcontext(contextloader.java:294) @ org.springframework.web.context.contextloaderlistener.contextinitialized(contextloaderlistener.java:112) @ org.apache.catalina.core.standardcontext.listenerstart(standardcontext.java:4959) @ org.apache.catalina.core.standardcontext.startinternal(standardcontext.java:5455) @ org.apache.catalina.util.lifecyclebase.start(lifecyclebase.java:150) @ org.apache.catalina.core.containerbase.addchildinternal(containerbase.java:901) @ org.apache.catalina.core.containerbase.addchild(containerbase.java:877) @ org.apache.catalina.core.standardhost.addchild(standardhost.java:634) @ org.apache.catalina.startup.hostconfig.deploydescriptor(hostconfig.java:671) @ org.apache.catalina.startup.hostconfig$deploydescriptor.run(hostconfig.java:1840) @ java.util.concurrent.executors$runnableadapter.call(executors.java:471) @ java.util.concurrent.futuretask.run(futuretask.java:262) @ java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1145) @ java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:615) @ java.lang.thread.run(thread.java:744) caused by: org.springframework.beans.factory.beancreationexception: not autowire field: private com.sprhib.dao.userdao com.sprhib.controller.usercontroller.userdao; nested exception org.springframework.beans.factory.nosuchbeandefinitionexception: no qualifying bean of type [com.sprhib.dao.userdao] found dependency: expected @ least 1 bean qualifies autowire candidate dependency. dependency annotations: {@org.springframework.beans.factory.annotation.autowired(required=true)} @ org.springframework.beans.factory.annotation.autowiredannotationbeanpostprocessor$autowiredfieldelement.inject(autowiredannotationbeanpostprocessor.java:514) @ org.springframework.beans.factory.annotation.injectionmetadata.inject(injectionmetadata.java:87) @ org.springframework.beans.factory.annotation.autowiredannotationbeanpostprocessor.postprocesspropertyvalues(autowiredannotationbeanpostprocessor.java:285) ... 26 more caused by: org.springframework.beans.factory.nosuchbeandefinitionexception: no qualifying bean of type [com.sprhib.dao.userdao] found dependency: expected @ least 1 bean qualifies autowire candidate dependency. dependency annotations: {@org.springframework.beans.factory.annotation.autowired(required=true)} @ org.springframework.beans.factory.support.defaultlistablebeanfactory.raisenosuchbeandefinitionexception(defaultlistablebeanfactory.java:986) @ org.springframework.beans.factory.support.defaultlistablebeanfactory.doresolvedependency(defaultlistablebeanfactory.java:856) @ org.springframework.beans.factory.support.defaultlistablebeanfactory.resolvedependency(defaultlistablebeanfactory.java:768) @ org.springframework.beans.factory.annotation.autowiredannotationbeanpostprocessor$autowiredfieldelement.inject(autowiredannotationbeanpostprocessor.java:486) ... 28 more cze 27, 2014 12:03:56 pm org.apache.catalina.core.standardcontext listenerstart severe: exception sending context initialized event listener instance of class org.springframework.web.context.contextloaderlistener org.springframework.beans.factory.beancreationexception: error creating bean name 'usercontroller': injection of autowired dependencies failed; nested exception org.springframework.beans.factory.beancreationexception: not autowire field: private com.sprhib.dao.userdao com.sprhib.controller.usercontroller.userdao; nested exception org.springframework.beans.factory.nosuchbeandefinitionexception: no qualifying bean of type [com.sprhib.dao.userdao] found dependency: expected @ least 1 bean qualifies autowire candidate dependency. dependency annotations: {@org.springframework.beans.factory.annotation.autowired(required=true)} @ org.springframework.beans.factory.annotation.autowiredannotationbeanpostprocessor.postprocesspropertyvalues(autowiredannotationbeanpostprocessor.java:288) @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.populatebean(abstractautowirecapablebeanfactory.java:1122) @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.docreatebean(abstractautowirecapablebeanfactory.java:522) @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.createbean(abstractautowirecapablebeanfactory.java:461) @ org.springframework.beans.factory.support.abstractbeanfactory$1.getobject(abstractbeanfactory.java:295) @ org.springframework.beans.factory.support.defaultsingletonbeanregistry.getsingleton(defaultsingletonbeanregistry.java:223) @ org.springframework.beans.factory.support.abstractbeanfactory.dogetbean(abstractbeanfactory.java:292) @ org.springframework.beans.factory.support.abstractbeanfactory.getbean(abstractbeanfactory.java:194) @ org.springframework.beans.factory.support.defaultlistablebeanfactory.preinstantiatesingletons(defaultlistablebeanfactory.java:626) @ org.springframework.context.support.abstractapplicationcontext.finishbeanfactoryinitialization(abstractapplicationcontext.java:932) @ org.springframework.context.support.abstractapplicationcontext.refresh(abstractapplicationcontext.java:479) @ org.springframework.web.context.contextloader.configureandrefreshwebapplicationcontext(contextloader.java:389) @ org.springframework.web.context.contextloader.initwebapplicationcontext(contextloader.java:294) @ org.springframework.web.context.contextloaderlistener.contextinitialized(contextloaderlistener.java:112) @ org.apache.catalina.core.standardcontext.listenerstart(standardcontext.java:4959) @ org.apache.catalina.core.standardcontext.startinternal(standardcontext.java:5455) @ org.apache.catalina.util.lifecyclebase.start(lifecyclebase.java:150) @ org.apache.catalina.core.containerbase.addchildinternal(containerbase.java:901) @ org.apache.catalina.core.containerbase.addchild(containerbase.java:877) @ org.apache.catalina.core.standardhost.addchild(standardhost.java:634) @ org.apache.catalina.startup.hostconfig.deploydescriptor(hostconfig.java:671) @ org.apache.catalina.startup.hostconfig$deploydescriptor.run(hostconfig.java:1840) @ java.util.concurrent.executors$runnableadapter.call(executors.java:471) @ java.util.concurrent.futuretask.run(futuretask.java:262) @ java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1145) @ java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:615) @ java.lang.thread.run(thread.java:744) caused by: org.springframework.beans.factory.beancreationexception: not autowire field: private com.sprhib.dao.userdao com.sprhib.controller.usercontroller.userdao; nested exception org.springframework.beans.factory.nosuchbeandefinitionexception: no qualifying bean of type [com.sprhib.dao.userdao] found dependency: expected @ least 1 bean qualifies autowire candidate dependency. dependency annotations: {@org.springframework.beans.factory.annotation.autowired(required=true)} @ org.springframework.beans.factory.annotation.autowiredannotationbeanpostprocessor$autowiredfieldelement.inject(autowiredannotationbeanpostprocessor.java:514) @ org.springframework.beans.factory.annotation.injectionmetadata.inject(injectionmetadata.java:87) @ org.springframework.beans.factory.annotation.autowiredannotationbeanpostprocessor.postprocesspropertyvalues(autowiredannotationbeanpostprocessor.java:285) ... 26 more caused by: org.springframework.beans.factory.nosuchbeandefinitionexception: no qualifying bean of type [com.sprhib.dao.userdao] found dependency: expected @ least 1 bean qualifies autowire candidate dependency. dependency annotations: {@org.springframework.beans.factory.annotation.autowired(required=true)} @ org.springframework.beans.factory.support.defaultlistablebeanfactory.raisenosuchbeandefinitionexception(defaultlistablebeanfactory.java:986) @ org.springframework.beans.factory.support.defaultlistablebeanfactory.doresolvedependency(defaultlistablebeanfactory.java:856) @ org.springframework.beans.factory.support.defaultlistablebeanfactory.resolvedependency(defaultlistablebeanfactory.java:768) @ org.springframework.beans.factory.annotation.autowiredannotationbeanpostprocessor$autowiredfieldelement.inject(autowiredannotationbeanpostprocessor.java:486) ... 28 more cze 27, 2014 12:03:56 pm org.apache.catalina.core.standardcontext startinternal severe: error listenerstart cze 27, 2014 12:03:56 pm org.apache.catalina.core.standardcontext startinternal severe: context [/spr-mvc-hib] startup failed due previous errors cze 27, 2014 12:03:56 pm org.apache.catalina.core.applicationcontext log info: closing spring root webapplicationcontext cze 27, 2014 12:03:56 pm org.apache.catalina.loader.webappclassloader clearreferencesjdbc severe: web application [/spr-mvc-hib] registered jdbc driver [com.mysql.jdbc.driver] failed unregister when web application stopped. prevent memory leak, jdbc driver has been forcibly unregistered. cze 27, 2014 12:03:56 pm org.apache.catalina.loader.webappclassloader clearreferencesthreads severe: web application [/spr-mvc-hib] appears have started thread named [abandoned connection cleanup thread] has failed stop it. create memory leak. cze 27, 2014 12:03:56 pm org.apache.catalina.startup.hostconfig deploydirectory info: deploying web application directory c:\program files\spring_ide_64\vfabric-tc-server-developer-2.9.5.sr1\base-instance\webapps\manager cze 27, 2014 12:03:56 pm org.apache.catalina.startup.hostconfig deploydirectory info: deploying web application directory c:\program files\spring_ide_64\vfabric-tc-server-developer-2.9.5.sr1\base-instance\webapps\root cze 27, 2014 12:03:56 pm org.apache.coyote.abstractprotocol start info: starting protocolhandler ["http-bio-8080"] cze 27, 2014 12:03:56 pm org.apache.catalina.startup.catalina start info: server startup in 3776 ms
i've based application on com.sprhib application tutorial. can find here: http://fruzenshtein.com/spring-mvc-hibernate-maven-crud/
my usercontroller class
package com.sprhib.controller; import java.util.list; import org.springframework.beans.factory.annotation.autowired; import org.springframework.stereotype.controller; import org.springframework.web.bind.annotation.modelattribute; import org.springframework.web.bind.annotation.pathvariable; import org.springframework.web.bind.annotation.requestmapping; import org.springframework.web.bind.annotation.requestmethod; import org.springframework.web.servlet.modelandview; import com.sprhib.dao.userdao; import com.sprhib.model.user; @controller @requestmapping(value="/user") public class usercontroller { @autowired private userdao userdao; @requestmapping(value="/add", method=requestmethod.get) public modelandview adduserpage() { modelandview modelandview = new modelandview("add-user-form"); modelandview.addobject("user", new user()); return modelandview; } @requestmapping(value="/add", method=requestmethod.post) public modelandview addinguser(@modelattribute user user) { modelandview modelandview = new modelandview("home"); userdao.adduser(user); string message = "user added."; modelandview.addobject("message", message); return modelandview; } @requestmapping(value="/list") public modelandview listofusers() { modelandview modelandview = new modelandview("list-of-users"); list<user> users = userdao.getusers(); modelandview.addobject("users", users); return modelandview; } @requestmapping(value="/edit/{id}", method=requestmethod.get) public modelandview edituserpage(@pathvariable integer id) { modelandview modelandview = new modelandview("edit-user-form"); user user = userdao.getuser(id); modelandview.addobject("user",user); return modelandview; } @requestmapping(value="/edit/{id}", method=requestmethod.put) public modelandview edditinguser(@modelattribute user user, @pathvariable integer id) { modelandview modelandview = new modelandview("home"); userdao.updateuser(user); string message = "user edited."; modelandview.addobject("message", message); return modelandview; } @requestmapping(value="/delete/{id}", method=requestmethod.delete) public modelandview deleteuser(@pathvariable integer id) { modelandview modelandview = new modelandview("home"); userdao.deleteuser(id); string message = "user deleted."; modelandview.addobject("message", message); return modelandview; } }
my user model class:
package com.sprhib.model; import java.util.date; import java.util.list; import javax.persistence.entity; import javax.persistence.fetchtype; import javax.persistence.generatedvalue; import javax.persistence.id; import javax.persistence.onetomany; import javax.persistence.temporal; import javax.persistence.temporaltype; import javax.persistence.transient; @entity public class user { @id @generatedvalue long id; string email; string lastname; string firstname; // @onetomany(fetch=fetchtype.lazy) // list<address> address; string password; @transient string repeatpassword; @temporal(temporaltype.timestamp) date creationdate; @temporal(temporaltype.timestamp) date updatedate; boolean active; public long getid() { return id; } public void setid(long id) { this.id = id; } public string getemail() { return email; } public void setemail(string email) { this.email = email; } public string getlastname() { return lastname; } public void setlastname(string lastname) { this.lastname = lastname; } public string getfirstname() { return firstname; } public void setfirstname(string firstname) { this.firstname = firstname; } // public list<address> getaddress() { // return address; // } // public void setaddress(list<address> address) { // this.address = address; // } public string getpassword() { return password; } public void setpassword(string password) { this.password = password; } public string getrepeatpassword() { return repeatpassword; } public void setrepeatpassword(string repeatpassword) { this.repeatpassword = repeatpassword; } public date getcreationdate() { return creationdate; } public void setcreationdate(date creationdate) { this.creationdate = creationdate; } public date getupdatedate() { return updatedate; } public void setupdatedate(date updatedate) { this.updatedate = updatedate; } public boolean isactive() { return active; } public void setactive(boolean active) { this.active = active; } }
my userdao interface:
package com.sprhib.dao; import java.util.list; import com.sprhib.model.user; public interface userdao { public void adduser(user user); public void updateuser(user user); public void deleteuser(long id); public user getuser(long id); public list<user> getusers(); }
my userdao interface implemantation class
package com.sprhib.dao; import java.util.list; import org.hibernate.session; import org.hibernate.sessionfactory; import org.springframework.beans.factory.annotation.autowired; import com.sprhib.model.user; public class userdaoimpl implements userdao { @autowired private sessionfactory sessionfactory; private session getcurrentsession() { return sessionfactory.getcurrentsession(); } @override public void adduser(user user) { getcurrentsession().save(user); } @override public void updateuser(user user) { user toupdate = getuser(user.getid()); toupdate.setlastname(user.getlastname()); toupdate.setfirstname(user.getfirstname()); toupdate.setemail(user.getemail()); getcurrentsession().update(toupdate); } @override public user getuser(long id) { user user = (user) getcurrentsession().get(user.class, id); return user; } @override public void deleteuser(long id) { user user = getuser(id); if (user != null) getcurrentsession().delete(user); } @suppresswarnings("unchecked") @override public list<user> getusers() { return getcurrentsession().createquery("from user").list(); } }
web.xml file empty cause use javaconfig class approach
my configuration is:
package com.sprhib.init; import javax.servlet.servletcontext; import javax.servlet.servletexception; import javax.servlet.servletregistration.dynamic; import org.springframework.web.webapplicationinitializer; import org.springframework.web.context.contextloaderlistener; import org.springframework.web.context.support.annotationconfigwebapplicationcontext; import org.springframework.web.filter.hiddenhttpmethodfilter; import org.springframework.web.servlet.dispatcherservlet; public class initializer implements webapplicationinitializer { public void onstartup(servletcontext servletcontext) throws servletexception { annotationconfigwebapplicationcontext ctx = new annotationconfigwebapplicationcontext(); ctx.register(webappconfig.class); servletcontext.addlistener(new contextloaderlistener(ctx)); ctx.setservletcontext(servletcontext); dynamic servlet = servletcontext.addservlet("dispatcher", new dispatcherservlet(ctx)); servlet.addmapping("/"); servlet.setloadonstartup(1); // allow use put , delete method rest architecture registerhiddenfieldfilter(servletcontext); } private void registerhiddenfieldfilter(servletcontext acontext) { acontext.addfilter("hiddenhttpmethodfilter", new hiddenhttpmethodfilter()).addmappingforurlpatterns(null ,true, "/*"); } }
and:
package com.sprhib.init; import java.util.properties; import javax.annotation.resource; import javax.sql.datasource; import org.springframework.context.annotation.bean; import org.springframework.context.annotation.componentscan; import org.springframework.context.annotation.configuration; import org.springframework.context.annotation.propertysource; import org.springframework.core.env.environment; import org.springframework.jdbc.datasource.drivermanagerdatasource; import org.springframework.orm.hibernate4.hibernatetransactionmanager; import org.springframework.orm.hibernate4.localsessionfactorybean; import org.springframework.transaction.annotation.enabletransactionmanagement; import org.springframework.web.servlet.config.annotation.enablewebmvc; import org.springframework.web.servlet.view.jstlview; import org.springframework.web.servlet.view.urlbasedviewresolver; @configuration @componentscan("com.sprhib") @enablewebmvc @enabletransactionmanagement @propertysource("classpath:application.properties") public class webappconfig { private static final string property_name_database_driver = "db.driver"; private static final string property_name_database_password = "db.password"; private static final string property_name_database_url = "db.url"; private static final string property_name_database_username = "db.username"; private static final string property_name_hibernate_dialect = "hibernate.dialect"; private static final string property_name_hibernate_show_sql = "hibernate.show_sql"; private static final string property_name_hibernate_hbm2ddl_auto = "hibernate.hbm2ddl.auto"; private static final string property_name_entitymanager_packages_to_scan = "entitymanager.packages.to.scan"; @resource private environment env; @bean public datasource datasource() { drivermanagerdatasource datasource = new drivermanagerdatasource(); datasource.setdriverclassname(env.getrequiredproperty(property_name_database_driver)); datasource.seturl(env.getrequiredproperty(property_name_database_url)); datasource.setusername(env.getrequiredproperty(property_name_database_username)); datasource.setpassword(env.getrequiredproperty(property_name_database_password)); return datasource; } @bean public localsessionfactorybean sessionfactory() { localsessionfactorybean sessionfactorybean = new localsessionfactorybean(); sessionfactorybean.setdatasource(datasource()); sessionfactorybean.setpackagestoscan(env.getrequiredproperty(property_name_entitymanager_packages_to_scan)); sessionfactorybean.sethibernateproperties(hibproperties()); return sessionfactorybean; } private properties hibproperties() { properties properties = new properties(); properties.put(property_name_hibernate_dialect, env.getrequiredproperty(property_name_hibernate_dialect)); properties.put(property_name_hibernate_show_sql, env.getrequiredproperty(property_name_hibernate_show_sql)); properties.put(property_name_hibernate_hbm2ddl_auto, env.getrequiredproperty(property_name_hibernate_hbm2ddl_auto)); return properties; } @bean public hibernatetransactionmanager transactionmanager() { hibernatetransactionmanager transactionmanager = new hibernatetransactionmanager(); transactionmanager.setsessionfactory(sessionfactory().getobject()); return transactionmanager; } @bean public urlbasedviewresolver setupviewresolver() { urlbasedviewresolver resolver = new urlbasedviewresolver(); resolver.setprefix("/web-inf/pages/"); resolver.setsuffix(".jsp"); resolver.setviewclass(jstlview.class); return resolver; } }
the @repository
annotation missing in dao
Comments
Post a Comment