【问题标题】:Hibernate - Spring MVC Error creating beanHibernate - Spring MVC 错误创建 bean
【发布时间】:2016-01-06 21:11:48
【问题描述】:

我是 Spring 新手,我尝试根据本教程制作一个应用程序:http://websystique.com/spring/spring4-hibernate4-mysql-maven-integration-example-using-annotations。我见过一些类似的问题,但我想不通,我仍然得到这个错误:

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)}

这里是配置类:

@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;
  }
}

控制器:

@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);
}

这是服务:

@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();
}

还有 servlet-servlet.xml 配置文件:

<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 />

非常感谢您的帮助!

【问题讨论】:

  • 你确定UserServiceImp在一个被spring的组件扫描扫描的包里吗?因为异常表明没有任何具有该名称的bean,尽管您正在正确定义它
  • 尝试使用 *Service *Qualifier("userService") *Transactional public class UserServiceImp implements UserService{ 并确保组件扫描包含该包 // 我不能使用 @ 所以我使用 *

标签: spring hibernate spring-mvc


【解决方案1】:

@Controller 需要 UserService 来执行它的工作。它是@Autowired,但也带有@Qualifier(“userService”)。在此示例中,我不会使用 @Qualifier,因为没有其他 UserService 接口的冲突实现(只是 UserServiceImp)。也就是说,@Service 已用“userService”定义,因此带有@Qualifier 的@Autowired 应该可以工作。我无法解释为什么它不适合你,但怀疑它与 @Transactional 的代理方式有关。
简而言之,删除 @Qualifier("userService") 行,@Autowired 应该可以正常工作(Autowired 应该与 UserService 匹配。 如果这不起作用,请尝试将 @Transactional 注释从类中向下移动到类中的两个方法

【讨论】:

  • 这应该会让他更接近事业
猜你喜欢
  • 1970-01-01
  • 2013-04-19
  • 1970-01-01
  • 1970-01-01
  • 2016-12-15
  • 1970-01-01
  • 1970-01-01
  • 2011-01-05
  • 2015-03-10
相关资源
最近更新 更多