2

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!

2
  • 1
    are you sure that the UserServiceImp is in a package that is scanned by spring's component scan? Because the exception indicates that there is not any bean with that name , although you are defining it properly Commented Jan 7, 2016 at 9:00
  • Try using *Service *Qualifier("userService") *Transactional public class UserServiceImp implements UserService{ and also make sure component scan contains that package // I could not use @ so I used * Commented Jan 8, 2016 at 16:08

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

That should lead him closer to the cause

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.