Foros del Web » Programación para mayores de 30 ;) » Java »

org.springframework.beans.factory.BeanCreationExce ption

Estas en el tema de org.springframework.beans.factory.BeanCreationExce ption en el foro de Java en Foros del Web. 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 ...
  #1 (permalink)  
Antiguo 24/04/2013, 05:11
 
Fecha de Ingreso: febrero-2013
Mensajes: 50
Antigüedad: 11 años, 2 meses
Puntos: 0
org.springframework.beans.factory.BeanCreationExce ption

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
  1. <beans xmlns="http://www.springframework.org/schema/beans"
  2.     xmlns:context="http://www.springframework.org/schema/context"
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.     xsi:schemaLocation="http://www.springframework.org/schema/beans
  5.                         http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  6.                         http://www.springframework.org/schema/context
  7.                         http://www.springframework.org/schema/context/spring-context-3.0.xsd">
  8.  
  9.     <context:component-scan base-package="app.view" />
  10.     <bean
  11.         class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  12.         <property name="prefix" value="/WEB-INF/jsp/" />
  13.         <property name="suffix" value=".jsp" />
  14.     </bean>
  15.  
  16. </beans>



Siendo la clase Controller UsuarioController:


Código JAVA:
Ver original
  1. package app.view;
  2.  
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.stereotype.Controller;
  5. import org.springframework.web.bind.annotation.ModelAttribute;
  6. import org.springframework.web.bind.annotation.RequestMapping;
  7. import org.springframework.web.bind.annotation.RequestMethod;
  8. import org.springframework.web.servlet.ModelAndView;
  9. import org.springframework.ui.ModelMap;
  10. import app.entity.Usuario;
  11. import app.service.UsuarioService;
  12.  
  13.     @Controller
  14.     public class UsuarioController {
  15.        
  16.         @Autowired
  17.         private UsuarioService usuarioService;
  18.        
  19.        
  20.         @RequestMapping(value = "/usuario", method = RequestMethod.GET)
  21.         public ModelAndView usuario() {
  22.             return new ModelAndView("usuario", "command", new Usuario());
  23.         }
  24.        
  25.         @RequestMapping(value = "/insertaUsuario", method = RequestMethod.POST)
  26.         public String insertaUsuario(@ModelAttribute("SpringWeb")Usuario usuario, ModelMap model) {
  27.             usuarioService.addUsuario(usuario);
  28.             return "resultado";
  29.         }
  30. }


Código JAVA:
Ver original
  1. package app.service;
  2.  
  3. import app.entity.Usuario;
  4.  
  5. public interface UsuarioService {
  6.      public void addUsuario(Usuario usuario);
  7. }


Código JAVA:
Ver original
  1. package app.service;
  2.  
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.transaction.annotation.Transactional;
  5. import app.dao.UsuarioDAO;
  6. import app.entity.Usuario;
  7.  
  8.  
  9. public class UsuarioServiceImpl implements UsuarioService{
  10.    
  11.      @Autowired
  12.      private UsuarioDAO usuarioDAO;
  13.  
  14.      @Transactional
  15.      public void addUsuario(Usuario usuario) {
  16.          usuarioDAO.addUsuario(usuario);
  17.      }
  18.  
  19. }


Código JAVA:
Ver original
  1. package app.dao;
  2.  
  3. import app.entity.Usuario;
  4.  
  5. public interface UsuarioDAO {
  6.     public void addUsuario(Usuario usuario);
  7. }


Código JAVA:
Ver original
  1. package app.dao;
  2.  
  3. import org.hibernate.SessionFactory;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.stereotype.Repository;
  6. import app.entity.Usuario;
  7.  
  8. public class UsuarioDAOImpl implements UsuarioDAO {
  9.    
  10.     @Autowired
  11.     private SessionFactory sessionFactory;
  12.  
  13.     public void addUsuario(Usuario usuario) {
  14.         sessionFactory.getCurrentSession().save(usuario);
  15.     }
  16. }

Código JAVA:
Ver original
  1. ackage app.entity;
  2.  
  3. public class Usuario {
  4.     private Integer id;
  5.     private String nombre;
  6.     private String email;
  7.  
  8.     public Integer getId() {
  9.         return id;
  10.     }
  11.  
  12.     public void setId(Integer id) {
  13.         this.id = id;
  14.     }
  15.  
  16.     public String getNombre() {
  17.         return nombre;
  18.     }
  19.  
  20.     public void setNombre(String nombre) {
  21.         this.nombre = nombre;
  22.     }
  23.  
  24.     public String getEmail() {
  25.         return email;
  26.     }
  27.  
  28.     public void setEmail(String email) {
  29.         this.email = email;
  30.     }
  31. }



web.xml
Código XML:
Ver original
  1. <?xml version="1.0" encoding="UTF-8"?>
  2.  
  3.  
  4. <web-app id="WebApp_ID" version="2.4"
  5.      xmlns="http://java.sun.com/xml/ns/j2ee"
  6.      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  7.      xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
  8.      http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
  9.    
  10.       <display-name>Ejemplo Spring MVC</display-name>
  11.    
  12.       <servlet>
  13.           <servlet-name>SpringEjemplo</servlet-name>
  14.           <servlet-class>
  15.              org.springframework.web.servlet.DispatcherServlet
  16.           </servlet-class>
  17.           <load-on-startup>1</load-on-startup>
  18.       </servlet>
  19.     <servlet-mapping>
  20.           <servlet-name>SpringEjemplo</servlet-name>
  21.           <url-pattern>/</url-pattern>
  22.       </servlet-mapping>
  23.    
  24.   </web-app>


usuario.jsp
Código JSP:
Ver original
  1. <%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>  
  2. <html>
  3.   <head>
  4.   <title>Ejemplo Spring MVC</title>
  5.   </head>
  6.   <body>    <h2>Información del Usuario</h2>
  7.       <form:form method="POST" action="/SpringExample/insertaUsuario">
  8.           <table>
  9.               <tr>
  10.                   <td><form:label path="id">Id</form:label></td>
  11.                   <td><form:input path="id" /></td>
  12.               </tr>
  13.               <tr>
  14.                   <td><form:label path="nombre">Nombre</form:label></td>
  15.                   <td><form:input path="nombre" /></td>
  16.               </tr>
  17.               <tr>
  18.                   <td><form:label path="email">Email</form:label></td>
  19.                   <td><form:input path="email" /></td>
  20.               </tr>            <tr>
  21.                   <td colspan="2"><input type="submit" value="Insertar" /></td>
  22.               </tr>
  23.           </table>
  24.       </form:form>
  25.   </body>
  26. </html>
¿Qué sucede?, ¿alguien ve por qué arroja ese fallo?


Gracias.

Última edición por gt_int; 24/04/2013 a las 05:17
  #2 (permalink)  
Antiguo 24/04/2013, 05:33
Avatar de Fuzzylog  
Fecha de Ingreso: agosto-2008
Ubicación: En internet
Mensajes: 2.511
Antigüedad: 15 años, 8 meses
Puntos: 188
Respuesta: org.springframework.beans.factory.BeanCreationExce ption

@Service("usuarioService")
public class UsuarioServiceImpl implements UsuarioService{

@Autowired
private UsuarioDAO usuarioDAO;

@Transactional
public void addUsuario(Usuario usuario) {
usuarioDAO.addUsuario(usuario);
}

}
__________________
if (fuzzy && smooth) {
fuzzylog = "c00l";
return true;
}
  #3 (permalink)  
Antiguo 24/04/2013, 05:46
 
Fecha de Ingreso: febrero-2013
Mensajes: 50
Antigüedad: 11 años, 2 meses
Puntos: 0
Respuesta: org.springframework.beans.factory.BeanCreationExce ption

Cita:
Iniciado por Fuzzylog Ver Mensaje
@Service("usuarioService")
public class UsuarioServiceImpl implements UsuarioService{

@Autowired
private UsuarioDAO usuarioDAO;

@Transactional
public void addUsuario(Usuario usuario) {
usuarioDAO.addUsuario(usuario);
}

}

En primer lugar darte las gracias por tu ayuda, pero aún con lo que me has puesto me sigue dando fallo:


SEVERE: StandardWrapper.Throwable
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





Y también este:

SEVERE: El Servlet /SpringExample lanzó excepción de load()
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
  #4 (permalink)  
Antiguo 24/04/2013, 08:20
Avatar de Fuzzylog  
Fecha de Ingreso: agosto-2008
Ubicación: En internet
Mensajes: 2.511
Antigüedad: 15 años, 8 meses
Puntos: 188
Respuesta: org.springframework.beans.factory.BeanCreationExce ption

Deberias tener un archivo tipo spring-model.xml con esto
<!-- Activa el uso de @Autowired. -->
<context:annotation-config />
<!-- Indica el package de referencia a buscar las anotaciones-->
<context:component-scan base-package="app" />

Para que lo localice en el web.xml deberias indicarlo asi

<!-- Ficheros de configuracion del contexto principal de spring -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:spring/spring-model.xml
</param-value>
</context-param>
__________________
if (fuzzy && smooth) {
fuzzylog = "c00l";
return true;
}
  #5 (permalink)  
Antiguo 25/04/2013, 11:05
 
Fecha de Ingreso: febrero-2013
Mensajes: 50
Antigüedad: 11 años, 2 meses
Puntos: 0
Respuesta: org.springframework.beans.factory.BeanCreationExce ption

Cita:
Iniciado por Fuzzylog Ver Mensaje
Deberias tener un archivo tipo spring-model.xml con esto
<!-- Activa el uso de @Autowired. -->
<context:annotation-config />
<!-- Indica el package de referencia a buscar las anotaciones-->
<context:component-scan base-package="app" />

Para que lo localice en el web.xml deberias indicarlo asi

<!-- Ficheros de configuracion del contexto principal de spring -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:spring/spring-model.xml
</param-value>
</context-param>
Muchas gracias, por ahí estaba el fallo.

Ahora también me vi un ejemplo que me aclaró muchas dudas: http://www.youtube.com/watch?v=rdYQOqxq9F0

Etiquetas: clase, jsp, servlet, string
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 18:27.