0

I want to integrate Spring with Hibernate. I am new to both technologies. At the moment I get an error like this:

 org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'homeController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: public service.UserService controller.HomeController.userService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [service.UserService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)} 

Can anybody help me, please?

package controller;

@Controller  
public class HomeController {  

   @Autowired  
    private UserService userService;  


   @RequestMapping("/register")  
    public ModelAndView getRegisterForm(@ModelAttribute("user") User user,  
            BindingResult result) {  

        ArrayList<String> gender = new ArrayList<String>();  
        gender.add("Male");  
        gender.add("Female");  

        ArrayList<String> city = new ArrayList<String>();  
        city.add("Delhi");  
        city.add("Kolkata");  
        city.add("Chennai");  
        city.add("Bangalore");  

        Map<String, Object> model = new HashMap<String, Object>();  
        model.put("gender", gender);  
        model.put("city", city);  

        System.out.println("Register Form");  
        return new ModelAndView("Register", "model", model);  

    }  


    @RequestMapping("/saveUser")  
    public ModelAndView saveUserData(@ModelAttribute("user") User user,  
            BindingResult result) {  

        userService.addUser(user);  
        System.out.println("Save User Data");  
        return new ModelAndView("redirect:/userList.html");  

    }  


    @RequestMapping("/userList")  
    public ModelAndView getUserList() {  

        Map<String, Object> model = new HashMap<String, Object>();  
        model.put("user", userService.getUser());  
        return new ModelAndView("UserDetails", model);  

    }  

}  

package service;


@Service  

public class UserServiceImpl implements UserService {  


    @Autowired  
    UserDao userDao;  
    @Override  
    public void addUser(User user) {  
        userDao.saveUser(user);  

    }  


    @Override  
    public List<User> getUser() {  
        return userDao.getUser();  

    }  
}  

spring-servlet Code: Hi need a Spring ,Hibernate integration example for my new project.so i have copied this from a website.and i did some modifications in that ..It's not working please help me

   <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"
   xmlns:tx="http://www.springframework.org/schema/tx"
   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
   http://www.springframework.org/schema/tx  
   http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
    <context:property-placeholder location="classpath:jdbc.properties" /> 
    <context:component-scan base-package="controller" />
    <tx:annotation-driven/>
    <bean id="jspViewResolver"  
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
        <property name="viewClass"  
            value="org.springframework.web.servlet.view.JstlView" />  
        <property name="prefix" value="/WEB-INF/view/" />  
        <property name="suffix" value=".jsp" />  
    </bean>  

    <bean id="dataSource"  
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">  
        <property name="driverClassName" value="${database.driver}" />  
        <property name="url" value="${database.url}" />  
        <property name="username" value="${database.user}" />  
        <property name="password" value="${database.password}" />  
    </bean>  


  <bean id="myydao" class="dao.UserDaoImpl">
       <property name="sessionFactory" ref="sessionFactory"></property>
  </bean>
  <bean name="/user/*.htm" class="controller.HomeController" >
  <property name="UserDao" ref="mydao" />
  </bean>

    <bean id="hibernateTransactionManager"  
        class="org.springframework.orm.hibernate3.HibernateTransactionManager">  
        <property name="sessionFactory" ref="sessionFactory" />  
    </bean> 
   </beans>
  • 2
    Please add the code of `UserService` and `homeController` – Jens Jul 11 '14 at 06:46
  • It's mu UserService interface package service; /** * @author Durga * */ import java.util.List; import com.bean.User; public interface UserService { public void addUser(User user); public List getUser(); } – user3828255 Jul 11 '14 at 06:49
  • possible duplicate http://stackoverflow.com/questions/11273070/spring-autowiring-not-working-for-abstract-classes http://stackoverflow.com/questions/16770955/nullpointerexception-on-autowired-attribute-with-jersey-and-spring-for-rest-ser http://stackoverflow.com/questions/15951286/abstract-classes-interfaces-and-autowired – pezetem Jul 11 '14 at 06:51
  • @user3828255 Can you add the implementation of UserService? – Jens Jul 11 '14 at 06:52
  • Hi Jens thanks for your quick reply Just now i had posted the controller as well as UserService implemetation class Please check it – user3828255 Jul 11 '14 at 07:24

2 Answers2

1

I assume that you haven't added the bean to your Spring Application Context

If you are using xml, you need to do this by adding something like the following to the xml file:

  <bean class="service.UserServiceImpl" id="userService"/>

Or, if you are using a Java based configuration class, you need to add a bean like so:

@Configuration
public class AppConfig{

   @Bean
   public UserService userService() {
       return new UserServiceImpl();
   }
}

Or, if you add the @Component annotation to UserServiceImpl and @ComponentScan("service") annotation to the Java App config, it should pick up the User service

Ben Green
  • 3,953
  • 3
  • 29
  • 49
0

Make sure you have enabled the component scanning to automatically detect classes and register respective beans. You should add below entry to your bean definition XML file:

<context:component-scan base-package="org.example"/>

More about automatic bean detection

tmarwen
  • 15,750
  • 5
  • 43
  • 62