Buenos días, estoy intentando hacer una aplicación web con Hibernate y Spring, se puede decir que tengo la aplicación montada, pero me da fallo, en concreto el siguiente:
org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'usuarioController': Autowiring of fields failed; nested exception is org.springframework.beans.factory.BeanCreationExce ption: Could not autowire field: private app.service.UsuarioService app.view.UsuarioController.usuarioService; nested exception is org.springframework.beans.factory.NoSuchBeanDefini tionException: No unique bean of type [app.service.UsuarioService] is defined: Unsatisfied dependency of type [interface app.service.UsuarioService]: expected at least 1 matching bean
Creo que el fallo viene del archivo
SpringEjemplo-servlet.xml:
Código XML:
Ver original<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"
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:component-scan base-package="app.view" />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
Siendo la clase Controller
UsuarioController:
Código JAVA:
Ver originalpackage app.view;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.ui.ModelMap;
import app.entity.Usuario;
import app.service.UsuarioService;
@Controller
public class UsuarioController {
@Autowired
private UsuarioService usuarioService;
@RequestMapping(value = "/usuario", method = RequestMethod.GET)
public ModelAndView usuario() {
return new ModelAndView("usuario", "command", new Usuario());
}
@RequestMapping(value = "/insertaUsuario", method = RequestMethod.POST)
public String insertaUsuario
(@ModelAttribute
("SpringWeb")Usuario usuario, ModelMap model
) { usuarioService.addUsuario(usuario);
return "resultado";
}
}
Código JAVA:
Ver originalpackage app.service;
import app.entity.Usuario;
public interface UsuarioService {
public void addUsuario(Usuario usuario);
}
Código JAVA:
Ver originalpackage app.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import app.dao.UsuarioDAO;
import app.entity.Usuario;
public class UsuarioServiceImpl implements UsuarioService{
@Autowired
private UsuarioDAO usuarioDAO;
@Transactional
public void addUsuario(Usuario usuario) {
usuarioDAO.addUsuario(usuario);
}
}
Código JAVA:
Ver originalpackage app.dao;
import app.entity.Usuario;
public interface UsuarioDAO {
public void addUsuario(Usuario usuario);
}
Código JAVA:
Ver originalpackage app.dao;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import app.entity.Usuario;
public class UsuarioDAOImpl implements UsuarioDAO {
@Autowired
private SessionFactory sessionFactory;
public void addUsuario(Usuario usuario) {
sessionFactory.getCurrentSession().save(usuario);
}
}
Código JAVA:
Ver originalackage app.entity;
public class Usuario {
return id;
}
this.id = id;
}
return nombre;
}
public void setNombre
(String nombre
) { this.nombre = nombre;
}
return email;
}
public void setEmail
(String email
) { this.email = email;
}
}
web.xml
Código XML:
Ver original<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" 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>Ejemplo Spring MVC</display-name>
<servlet>
<servlet-name>SpringEjemplo</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>SpringEjemplo</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
usuario.jsp
Código JSP:
Ver original<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<html>
<head>
<title>Ejemplo Spring MVC</title>
</head>
<body> <h2>Información del Usuario</h2>
<form:form method="POST" action="/SpringExample/insertaUsuario">
<table>
<tr>
<td><form:label path="id">Id</form:label></td>
<td><form:input path="id" /></td>
</tr>
<tr>
<td><form:label path="nombre">Nombre</form:label></td>
<td><form:input path="nombre" /></td>
</tr>
<tr>
<td><form:label path="email">Email</form:label></td>
<td><form:input path="email" /></td>
</tr> <tr>
<td colspan="2"><input type="submit" value="Insertar" /></td>
</tr>
</table>
</form:form>
</body>
</html>
¿Qué sucede?, ¿alguien ve por qué arroja ese fallo?
Gracias.