java - Component annotation is not working properly -
i new in spring , hibernate, thing working fine component annotation not working. each time error coming -
caused by: org.springframework.beans.factory.nosuchbeandefinitionexception: no qualifying bean of type [com.vc.teacher.db.dao.userdao] found dependency: expected @ least 1 bean qualifies autowire candidate dependency. dependency annotations: {@org.springframework.beans.factory.annotation.autowired(required=true)}
my code -
web.xml
<?xml version="1.0" encoding="utf-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemalocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> <display-name>teacher</display-name> <welcome-file-list> <welcome-file>jsp/signin.jsp</welcome-file> </welcome-file-list> <!-- spring configuration --> <servlet> <servlet-name>spring</servlet-name> <servlet-class>org.springframework.web.servlet.dispatcherservlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>spring</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.css</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.js</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.jpg</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.woff</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.ttf</url-pattern> </servlet-mapping> <context-param> <param-name>log4jxmlpath</param-name> <param-value>/web-inf/log4j.xml</param-value> </context-param> <context-param> <param-name>contextconfiglocation</param-name> <param-value>/web-inf/spring-servlet.xml</param-value> </context-param> <listener> <listener-class> org.springframework.web.context.contextloaderlistener </listener-class> </listener> </web-app>
application-context.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:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemalocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> <tx:annotation-driven /> <context:component-scan base-package="com.vc.teacher" /> <bean id="transactionmanager" class="org.springframework.orm.hibernate4.hibernatetransactionmanager"> <property name="sessionfactory" ref="sessionfactory"></property> </bean> <bean id="sessionfactory" class="org.springframework.orm.hibernate4.localsessionfactorybean" lazy-init="false"> <property name="datasource" ref="datasource" /> <property name="annotatedclasses"> <list> <value>com.vc.teacher.entities.user</value> </list> </property> <property name="hibernateproperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.hsqldialect</prop> <!-- <prop key="hibernate.current_session_context_class">thread</prop> --> <prop key="show_sql">true</prop> </props> </property> </bean> <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/teacher"></property> <property name="username" value="root"></property> <property name="password" value="root"></property> </bean> </beans>
**spring
-servlet.xml** <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemalocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd"> <context:component-scan base-package="com.vc.teacher.controller" /> <!-- configuration defining views files --> <bean class="org.springframework.web.servlet.view.internalresourceviewresolver"> <property name="prefix"> <value>/</value> </property> <property name="suffix"> <value>.jsp</value> </property> </bean> </beans>
userdao class
package com.vc.teacher.db.dao; import java.util.list; import org.hibernate.query; import org.hibernate.session; import org.hibernate.sessionfactory; import org.hibernate.transaction; import org.springframework.stereotype.component; import org.springframework.beans.factory.annotation.autowired; import org.springframework.transaction.annotation.transactional; import com.vc.teacher.entities.user; @component public class userdao { @autowired sessionfactory sessionfactory; public sessionfactory getsessionfactory() { return sessionfactory; } public void setsessionfactory(sessionfactory sessionfactory) { this.sessionfactory = sessionfactory; } @transactional public user checkcreditionals(string email, string password){ user user = null; session session = sessionfactory.getcurrentsession(); query query = session.createquery("from user email = '"+email+"' , password = '"+password+"'"); list list = query.list(); if(list.size()>0) user = (user)list.get(0); return user; } @transactional public boolean registeruser(user user){ boolean result = false; session session = sessionfactory.getcurrentsession(); try{ user.setusertypeid(2); session.save(user); result = true; } catch (exception e){ result = false; e.printstacktrace(); } return result; } }
entity class - user
package com.vc.teacher.entities; import javax.persistence.column; import javax.persistence.entity; import javax.persistence.generatedvalue; import javax.persistence.id; import javax.persistence.table; @entity @table(name="user") public class user { @id @generatedvalue @column(name="user_id") private int id; @column(name="age") private int age; @column(name="user_type_id") private int usertypeid; @column(name="first_name") private string firstname; @column(name="last_name") private string lastname; @column(name="profession") private string profession; @column(name="phone") private string phone; @column(name="email") private string email; @column(name="password") private string password; @column(name="address") private string address; @column(name="status") private string status; public int getid() { return id; } public void setid(int id) { this.id = id; } public int getage() { return age; } public void setage(int age) { this.age = age; } public int getusertypeid() { return usertypeid; } public void setusertypeid(int usertypeid) { this.usertypeid = usertypeid; } public string getfirstname() { return firstname; } public void setfirstname(string firstname) { this.firstname = firstname; } public string getlastname() { return lastname; } public void setlastname(string lastname) { this.lastname = lastname; } public string getprofession() { return profession; } public void setprofession(string profession) { this.profession = profession; } public string getphone() { return phone; } public void setphone(string phone) { this.phone = phone; } public string getemail() { return email; } public void setemail(string email) { this.email = email; } public string getpassword() { return password; } public void setpassword(string password) { this.password = password; } public string getaddress() { return address; } public void setaddress(string address) { this.address = address; } public string getstatus() { return status; } public void setstatus(string status) { this.status = status; } }
controller class - accountcontroller
package com.vc.teacher.controller; import org.springframework.beans.factory.annotation.autowired; import org.springframework.stereotype.controller; import org.springframework.ui.model; import org.springframework.web.bind.annotation.requestmapping; import org.springframework.web.bind.annotation.requestmethod; import org.springframework.web.bind.annotation.requestparam; import org.springframework.web.servlet.modelandview; import org.springframework.web.servlet.mvc.support.redirectattributes; import com.vc.teacher.db.dao.userdao; import com.vc.teacher.entities.user; @controller public class accountcontroller { @autowired userdao userdao; @requestmapping("/login") public string loginuser(@requestparam("email") string email, @requestparam("password") string password, model model) { user user = userdao.checkcreditionals( email, password); if (user != null) { model.addattribute("user", user); return "jsp/profile"; } else { model.addattribute("error", "wrong creditionals"); return "jsp/signin"; } } @requestmapping("/signup") public string initilize(model model) { model.addattribute(new user()); return "jsp/signup"; } @requestmapping(method = requestmethod.post, value = "/register") public string signupuser(user user, redirectattributes attributes) { boolean result = false; user.setstatus("deactive"); result = userdao.registeruser(user); if (result == true) { attributes.addflashattribute("message", "you ready go !"); return "redirect:/signup"; } else { attributes.addflashattribute("message", "something went wrong"); return "redirect:/signup"; } } }
your spring-servlet.xml says
<context:component-scan base-package="com.vc.teacher.controller" />
while userdao
component not in controller package.
Comments
Post a Comment