dydjjs9934
dydjjs9934

Reputation: 21

Hibernate - Spring MVC Error creating bean

I am new to Spring and I tried to make an application based on this tutorial: http://websystique.com/spring/spring4-hibernate4-mysql-maven-integration-example-using-annotations. I've seen some similar questions but I couldn't figure it out, I still get this error:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'RESTController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: public service.UserService controller.RESTController.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), @org.springframework.beans.factory.annotation.Qualifier(value=userService)}
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: public service.UserService controller.RESTController.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), @org.springframework.beans.factory.annotation.Qualifier(value=userService)}
Caused by: 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), @org.springframework.beans.factory.annotation.Qualifier(value=userService)}

Here is the configuration class:

@Configuration
@EnableTransactionManagement
@ComponentScan({ "configuration" })
@PropertySource(value = { "classpath:database.properties" })
public class HibernateConfiguration {

@Autowired
private Environment environment;

@Bean
public LocalSessionFactoryBean sessionFactory() {
    LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
    sessionFactory.setDataSource(dataSource());
    sessionFactory.setPackagesToScan(new String[] { "model" });
    sessionFactory.setHibernateProperties(hibernateProperties());
    return sessionFactory;
 }

@Bean
public DataSource dataSource() {
    DriverManagerDataSource dataSource = new DriverManagerDataSource();
    dataSource.setDriverClassName(environment.getRequiredProperty("jdbc.driverClassName"));
    dataSource.setUrl(environment.getRequiredProperty("jdbc.url"));
    dataSource.setUsername(environment.getRequiredProperty("jdbc.username"));
    dataSource.setPassword(environment.getRequiredProperty("jdbc.password"));
    return dataSource;
}

private Properties hibernateProperties() {
    Properties properties = new Properties();
    properties.put("hibernate.dialect", environment.getRequiredProperty("hibernate.dialect"));
    properties.put("hibernate.show_sql", environment.getRequiredProperty("hibernate.show_sql"));
    properties.put("hibernate.format_sql", environment.getRequiredProperty("hibernate.format_sql"));
    return properties;        
}

@Bean
@Autowired
public HibernateTransactionManager transactionManager(SessionFactory s) {
   HibernateTransactionManager txManager = new HibernateTransactionManager();
   txManager.setSessionFactory(s);
   return txManager;
  }
}

The controller:

@RestController
public class RESTController {
@Autowired
@Qualifier("userService")
public UserService userService;

@RequestMapping(value = "/allGrades", method = RequestMethod.GET)
public ResponseEntity<List<Grade>> listAllGrades() {
    List<Grade> grades = userService.getAllGrades();
    if(grades.isEmpty()){
        return new ResponseEntity<List<Grade>>(HttpStatus.NO_CONTENT);
    }
    return new ResponseEntity<List<Grade>>(grades, HttpStatus.OK);
}

This is the service:

@Service("userService")
@Transactional
public class UserServiceImp implements UserService{

private static final AtomicLong counterGrades = new AtomicLong();
private static final AtomicLong counterStudents = new AtomicLong(); 

private static List<Grade> grades;
private static List<Student> students; 

@Autowired
private StudentDAO studentDAO;
@Autowired
private GradeDAO gradeDAO; 

public UserServiceImp() {

}

public void saveGrade(Grade grade) {

    gradeDAO.saveGrade(grade);
}

public List<Grade> getAllGrades() {
    return gradeDAO.getAllGrades();
}

and the servlet-servlet.xml configuration file:

<context:annotation-config></context:annotation-config>
<context:component-scan
base-package="controller, dao, model, service, main, configuration">
</context:component-scan>
<mvc:annotation-driven></mvc:annotation-driven>
<mvc:default-servlet-handler />
<tx:annotation-driven />

Thanks a lot for help!

Upvotes: 2

Views: 269

Answers (1)

Ian Mc
Ian Mc

Reputation: 5829

The @Controller requires the UserService to perform it's work. It is being @Autowired, but also with a @Qualifier("userService"). In this example I would not have used the @Qualifier because there are no other conflicting implementations of the UserService interface (just UserServiceImp). That said, the @Service has been defined with "userService" so the @Autowired with the @Qualifier should work. I can't explain why it is not for you, but suspect it has to do with how @Transactional is proxied.
In short, remove the @Qualifier("userService") line and @Autowired should work fine (Autowiring should be matched by UserService. If that doesn't work, try moving the @Transactional annotation down from the class to the two methods in the class

Upvotes: 1

Related Questions