java - Exception encountered during context initialization - cancelling refresh attempt -
warning: exception encountered during context initialization - cancelling refresh attempt org.springframework.beans.factory.beancreationexception: error creating bean name 'registrationcontroller': injection of autowired dependencies failed; nested exception org.springframework.beans.factory.beancreationexception: not autowire field: com.concept.userservice com.concept.registrationcontroller.userservice; nested exception org.springframework.beans.factory.cannotloadbeanclassexception: cannot find class [com.concept] bean name 'userservice' defined in servletcontext resource [/web-inf/spring-servlet.xml]; nested exception java.lang.classnotfoundexception: com.concept
can please might cause .i new spring framework
warning: exception encountered during context initialization - cancelling refresh attempt org.springframework.beans.factory.beancreationexception: error creating bean name 'registrationcontroller': injection of autowired dependencies failed; nested exception org.springframework.beans.factory.beancreationexception: not autowire field: com.concept.userservice com.concept.registrationcontroller.userservice; nested exception org.springframework.beans.factory.cannotloadbeanclassexception: cannot find class [com.concept] bean name 'userservice' defined in servletcontext resource [/web-inf/spring-servlet.xml]; nested exception java.lang.classnotfoundexception: com.concept @ org.springframework.beans.factory.annotation.autowiredannotationbeanpostprocessor.postprocesspropertyvalues(autowiredannotationbeanpostprocessor.java:334) @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.populatebean(abstractautowirecapablebeanfactory.java:1202) @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.docreatebean(abstractautowirecapablebeanfactory.java:537) @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.createbean(abstractautowirecapablebeanfactory.java:476) @ org.springframework.beans.factory.support.abstractbeanfactory$1.getobject(abstractbeanfactory.java:303) @ org.springframework.beans.factory.support.defaultsingletonbeanregistry.getsingleton(defaultsingletonbeanregistry.java:230) @ org.springframework.beans.factory.support.abstractbeanfactory.dogetbean(abstractbeanfactory.java:299) @ org.springframework.beans.factory.support.abstractbeanfactory.getbean(abstractbeanfactory.java:194) @ org.springframework.beans.factory.support.defaultlistablebeanfactory.preinstantiatesingletons(defaultlistablebeanfactory.java:762) @ org.springframework.context.support.abstractapplicationcontext.finishbeanfactoryinitialization(abstractapplicationcontext.java:757) @ org.springframework.context.support.abstractapplicationcontext.refresh(abstractapplicationcontext.java:480) @ org.springframework.web.servlet.frameworkservlet.configureandrefreshwebapplicationcontext(frameworkservlet.java:663) @ org.springframework.web.servlet.frameworkservlet.createwebapplicationcontext(frameworkservlet.java:629) @ org.springframework.web.servlet.frameworkservlet.createwebapplicationcontext(frameworkservlet.java:677) @ org.springframework.web.servlet.frameworkservlet.initwebapplicationcontext(frameworkservlet.java:548) @ org.springframework.web.servlet.frameworkservlet.initservletbean(frameworkservlet.java:489) @ org.springframework.web.servlet.httpservletbean.init(httpservletbean.java:136) @ javax.servlet.genericservlet.init(genericservlet.java:158) @ org.apache.catalina.core.standardwrapper.initservlet(standardwrapper.java:1227) @ org.apache.catalina.core.standardwrapper.loadservlet(standardwrapper.java:1140) @ org.apache.catalina.core.standardwrapper.load(standardwrapper.java:1027) @ org.apache.catalina.core.standardcontext.loadonstartup(standardcontext.java:5038) @ org.apache.catalina.core.standardcontext.startinternal(standardcontext.java:5348) @ org.apache.catalina.util.lifecyclebase.start(lifecyclebase.java:145) @ org.apache.catalina.core.containerbase$startchild.call(containerbase.java:1408) @ org.apache.catalina.core.containerbase$startchild.call(containerbase.java:1398) @ java.util.concurrent.futuretask.run(futuretask.java:266) @ java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1149) @ java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:624) @ java.lang.thread.run(thread.java:748)
and here controller
package com.concept; import org.springframework.beans.factory.annotation.autowired; import org.springframework.stereotype.controller; import org.springframework.web.bind.annotation.*; import com.concept.userservice; import com.concept.user; @controller public class registrationcontroller { @autowired userservice userservice; @requestmapping("/insert") public string inserdata(@modelattribute user user) { if (user != null) userservice.insertdata(user); return "result" ; } }
and here user.java
package com.concept; public class user { private string firstname; private string lastname; private integer age; private string email; 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 integer getage() { return age; } public void setage(integer age) { this.age = age; } public string getemail() { return email; } public void setemail(string email) { this.email = email; } }
and here userdao.java
package com.concept; import javax.sql.datasource; import org.springframework.beans.factory.annotation.autowired; import org.springframework.jdbc.core.jdbctemplate; public class userdao implements userservice { @autowired datasource datasource; public void insertdata(user user) { string sql = "insert enroll(first_name,last_name, age, email) values (?, ?, ?,?)"; jdbctemplate jdbctemplate = new jdbctemplate(datasource); jdbctemplate.update(sql, new object[] { user.getfirstname(), user.getlastname(), user.getage(),user.getemail() }); } }
and here userservice.java
package com.concept; public interface userservice { public void insertdata(user user); }
and here spring-servlet.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" xmlns:context="http://www.springframework.org/schema/context" xsi:schemalocation="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"> <context:annotation-config /> <context:component-scan base-package="com.concept" /> <!-- adding view resolver show jsp's on browser --> <bean id="viewresolver" class="org.springframework.web.servlet.view.internalresourceviewresolver"> <property name="prefix" value="/web-inf/jsp/" /> <property name="suffix" value=".jsp" /> </bean> <bean id="userservice" class="com.concept"/> <!-- declare datasource bean --> <bean id="datasource" class="org.springframework.jdbc.datasource.drivermanagerdatasource"> <property name="driverclassname" value="oracle.jdbc.oracledriver" /> <property name="url" value="jdbc:oracle:thin:@localhost:1521:xe" /> <property name="username" value="employee" /> <property name="password" value="pass1234" /> </bean> </beans>
and here 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_2_5.xsd" id="webapp_id" version="2.5"> <display-name>spring mvc application</display-name> <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> </web-app>
see root cause: nested exception java.lang.classnotfoundexception: com.concept
remove line <bean id="userservice" class="com.concept"/>
because com.concept
package not class. should not mix xml , annotation based configuration.
and add @repository
public class userdao implements userservice {
but not understand why dao implements interface called service
Comments
Post a Comment