I've started trying to get my head around spring mvc framework yesterday, and have encountered a problem. I get the error:
Error creating bean with name 'userController': Injection of autowired dependencies failed;
Could not autowire field: com.springapp.mvc.service.UserService
com.springapp.mvc.controller.UserController.userService; nested exception is
java.lang.NoClassDefFoundError: [Lorg/hibernate/engine/FilterDefinition;
I've checked other solutions and most of them have to do with people providing incorrect contexts. I've provided the base context, and not .controller
directory. IntelliJ auto-generated me mvc-dispatcher-servlet.xml
and I assume this is my application context. Does this mean I need to create servlet context?
Project structure:
I've ommited imports for a 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);
}
}