when add autowire controller following exception:
no qualifying bean of type [org.hibernate.sessionfactory] found dependency: expected @ least 1 bean qualifies autowire candidate dependency. dependency annotations: {@org.springframework.beans.factory.annotation.autowired(required=true)}
i not sure how solve suggestions please? wondering whether or not component scan in application context? controller:
package com.projectshaun.controller; import org.springframework.beans.factory.annotation.autowired; import org.springframework.stereotype.controller; import org.springframework.web.bind.annotation.requestmapping; import org.springframework.web.servlet.modelandview; import com.projectshaun.service.accountservice; @controller public class homecontroller { @autowired accountservice accountservice; @requestmapping("/") public modelandview welcome() { modelandview modelandview = new modelandview("welcome"); modelandview.addobject("greeting", "welcome projectshaun!"); return modelandview; } }
applicationcontext.xml:
<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:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:task="http://www.springframework.org/schema/task" xsi:schemalocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd" > <context:component-scan base-package="com.projectshaun" /> <tx:annotation-driven/> <bean id="datasource" class="org.apache.commons.dbcp.basicdatasource" destroy-method="close"> <property name="driverclassname" value="com.mysql.jdbc.driver" /> <property name="url" value="jdbc:mysql://localhost:3306/projectshaun" /> <property name="username" value="root" /> <property name="password" value="" /> </bean> <bean id="sessionfactory" class="org.springframework.orm.hibernate4.localsessionfactorybean"> <property name="datasource" ref="datasource"></property> <property name="annotatedclasses"> <list> <value>com.projectshaun.model.account</value> </list> </property> <property name="hibernateproperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.mysql5dialect</prop> <prop key="hibernate.show_sql">true</prop> </props> </property> </bean> <bean id="transactionmanager" class="org.springframework.orm.hibernate4.hibernatetransactionmanager" p:sessionfactory-ref="sessionfactory"> </bean> </beans>
defaultservlet-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:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" 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-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> <mvc:annotation-driven /> <context:component-scan base-package="com.projectshaun" /> <bean class="org.springframework.web.servlet.view.internalresourceviewresolver"> <property name="prefix" value="/web-inf/jsp/" /> <property name="suffix" value=".jsp" /> </bean> </beans>
accountservice:
package com.projectshaun.service; import com.projectshaun.model.account; public interface accountservice { void persistaccount(account account); }
homecontroller:
package com.projectshaun.controller; import org.springframework.beans.factory.annotation.autowired; import org.springframework.stereotype.controller; import org.springframework.web.bind.annotation.requestmapping; import org.springframework.web.servlet.modelandview; import com.projectshaun.service.accountservice; @controller public class homecontroller { @autowired accountservice accountservice; @requestmapping("/") public modelandview welcome() { modelandview modelandview = new modelandview("welcome"); modelandview.addobject("greeting", "welcome projectshaun!"); return modelandview; } }
accountserviceimpl:
package com.projectshaun.service; import org.springframework.beans.factory.annotation.autowired; import org.springframework.stereotype.service; import org.springframework.transaction.annotation.transactional; import com.projectshaun.dao.accountdao; import com.projectshaun.model.account; @service("accountservice") @transactional public class accountserviceimpl implements accountservice { @autowired private accountdao accountdao; public void persistaccount(account account) { accountdao.persistaccount(account); } }
web.xml:
<?xml version="1.0" encoding="utf-8"?> <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> <servlet> <servlet-name>defaultservlet</servlet-name> <servlet-class> org.springframework.web.servlet.dispatcherservlet </servlet-class> </servlet> <servlet-mapping> <servlet-name>defaultservlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
you have lots of xml file creating lots of confusion.first configure simple mvc add security , filter .so simple mvc web.xml looks below
<context-param> <param-name>contextconfiglocation</param-name> <param-value>web-inf/applicationcontext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.contextloaderlistener</listener-class> </listener> <servlet> <servlet-name>dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.dispatcherservlet</servlet-class> <load-on-startup>2</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>
applicationcontext.xml
<context:component-scan base-package="com.projectshaun" /> <mvc:annotation-driven />
dispatcher-servlet.xml
<bean class="org.springframework.web.servlet.mvc.support.controllerclassnamehandlermapping"/> <bean id="viewresolver" class="org.springframework.web.servlet.view.internalresourceviewresolver" p:prefix="/web-inf/jsp/" p:suffix=".jsp" />
Comments
Post a Comment