Could not autowire field is Spring MVC + Hibernate -
i've started trying head around spring mvc framework yesterday, , have encountered problem. error:
error creating bean name 'usercontroller': injection of autowired dependencies failed; not autowire field: com.springapp.mvc.service.userservice com.springapp.mvc.controller.usercontroller.userservice; nested exception java.lang.noclassdeffounderror: [lorg/hibernate/engine/filterdefinition;
i've checked other solutions , of them have people providing incorrect contexts. i've provided base context, , not .controller
directory. intellij auto-generated me mvc-dispatcher-servlet.xml
, assume application context. mean need create servlet context?
project structure:
i've ommited imports little more conciseness.
mvc-dispacher-servlet.xml
<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" 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.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <context:component-scan base-package="com.springapp.mvc"/> <mvc:annotation-driven /> <bean class="org.springframework.web.servlet.view.internalresourceviewresolver"> <property name="prefix" value="/web-inf/pages/"/> <property name="suffix" value=".jsp"/> </bean> <bean id="datasource" class="org.apache.commons.dbcp.basicdatasource"> <property name="driverclassname" value="com.mysql.jdbc.driver" /> <property name="username" value="vincas" /> <property name="password" value="123456" /> <property name="url" value="jdbc:mysql://localhost:3306/tieto"/> </bean> <bean id="sessionfactory" class="org.springframework.orm.hibernate3.annotation.annotationsessionfactorybean"> <property name="datasource" ref="datasource" /> <property name="packagestoscan" value="com.springapp.mvc.model"/> <property name="hibernateproperties"> <props> <prop key="dialect">org.hibernate.dialect.mysql5dialect</prop> </props> </property> </bean> <bean id="transactionmanager" class="org.springframework.orm.hibernate4.hibernatetransactionmanager"> <property name="sessionfactory" ref="sessionfactory" /> </bean> </beans>
userserviceimpl.java:
package com.springapp.mvc.service.impl; @service @transactional public class userserviceimpl implements userservice { @autowired userdao userdao; public user getbyid(long id) { return userdao.getbyid(id); } public list<user> getbyfirstname(string firstname) { return userdao.getbyfirstname(firstname); } public list<user> getbylastname(string lastname) { return userdao.getbylastname(lastname); } public list<user> getbyfullname(string firstname, string lastname) { return userdao.getbyfullname(firstname, lastname); } public list<user> getall() { return userdao.getall(); } public void save(user user) { userdao.save(user); } public void delete(user user) { userdao.delete(user); } }
userdaoimpl.java:
package com.springapp.mvc.dao.impl; @repository public class userdaoimpl implements userdao { @autowired private sessionfactory sessionfactory; private session getsession() { return sessionfactory.getcurrentsession(); } public user getbyid(long id) { return (user)getsession().get(user.class, id); } public list<user> getbyfirstname(string firstname) { return null; } public list<user> getbylastname(string lastname) { return null; } public list<user> getbyfullname(string firstname, string lastname) { return null; } public list<user> getall() { return null; } public void save(user user) { getsession().merge(user); } public void delete(user user) { getsession().delete(user); } }
web.xml:
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <display-name>spring mvc application</display-name> <servlet> <servlet-name>mvc-dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.dispatcherservlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>mvc-dispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
usercontroller.java:
package com.springapp.mvc.controller; @controller @requestmapping("/user") public class usercontroller { @autowired userservice userservice; @requestmapping(value = "/new", method = requestmethod.get) public string adduserget(model model) { model.addattribute("user", new user()); return "user/new"; } @requestmapping(value = "/new", method = requestmethod.post) public modelandview adduserpost(model model, @modelattribute user user) { userservice.save(user); model.addattribute("user", user); return new modelandview("user/success", "user", user); } }
Comments
Post a Comment