java - 不能 Autowiring 接口(interface)

标签 java spring spring-mvc

当我尝试从我的 Controller Autowiring 接口(interface)时,出现以下异常: 堆栈跟踪:

Could not autowire field: com.projectShaun.service.AccountService com.projectShaun.controller.HomeController.accountService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'accountService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.projectShaun.dao.AccountDao com.projectShaun.service.AccountServiceImpl.accountDao; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'accountDao': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: org.hibernate.SessionFactory com.projectShaun.dao.AccountDaoImpl.sessionFactory; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.hibernate.SessionFactory] 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)}

我的 Controller 看起来像这样:

package com.projectShaun.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.projectShaun.service.AccountService;

@Controller
public class HomeController {

    @Autowired
    AccountService accountService;

    @RequestMapping("/")
    public ModelAndView welcome() {
        ModelAndView modelAndView = new ModelAndView("welcome");
        modelAndView.addObject("greeting", "Welcome to projectShaun!");
        return modelAndView;
    }
}

还有我的应用程序上下文:

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:task="http://www.springframework.org/schema/task"
    xsi:schemaLocation="http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 
    http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.2.xsd 
    http://www.springframework.org/schema/jee 
    http://www.springframework.org/schema/jee/spring-jee-3.2.xsd 
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx-3.2.xsd 
    http://www.springframework.org/schema/task 
    http://www.springframework.org/schema/task/spring-task-3.2.xsd" >

  <context:component-scan base-package="com.projectShaun.controller" />

  <tx:annotation-driven/>

 <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="jdbc:mysql://localhost:3306/projectshaun" />
    <property name="username" value="root" />
    <property name="password" value="" />
  </bean>

  <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource"></property>
     <property name="annotatedClasses">
            <list>
                <value>com.projectShaun.model.Account</value>
            </list>
        </property>
    <property name="hibernateProperties">
      <props>
        <prop 
         key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
        <prop key="hibernate.show_sql">true</prop>
      </props>
    </property>
  </bean>

  <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager" 
    p:sessionFactory-ref="sessionFactory">
  </bean>
</beans>

我不确定它为什么抛出这个异常,我不确定它是否与上下文有关:组件扫描基础。我尝试将 com.projectShaun 作为基本包。

账户数据:

package com.projectShaun.dao;

import com.projectShaun.model.Account;

public interface AccountDao {

void persistAccount (Account account);
}

最佳答案

异常说:

  • 你正确地(尝试)在你的 HomeController bean 中 Autowiring 一个 accountService bean
  • 你正确地(尝试)在 accountService 中 Autowiring 一个 AccountDAO

但错误是在连接 AccountDaoImpl.sessionFactory 时,因为 spring 找不到任何 org.hibernate.SessionFactory bean。

关于java - 不能 Autowiring 接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30095067/

相关文章:

java - 发送对象时忽略 JSON 字段(反序列化)

java - 是否可以为 @Scheduled 分配代表 "never"的句点(在 Spring 中)?

spring - 如何在 thymeleaf 中将一个 html 页面包含到另一页面?

java - 使用 Spring 框架以原子方式维护服务层事务和数据库日志记录

java - java内存不足时写入磁盘

java - JScrollPane 内使用 <html> 标签自动换行 JLabel - 删除水平栏功能/逻辑

java - JSP 货币格式化

java - 与 Java : How to import an arbitrary . csv 文件中的文件实例函数混淆到 mysql 而不是特定文件?

spring - common.KafkaException : com. fastxml.jackson.databind.ser.std.StringSerializer 不是 org.apache.kafka.common.serialization.Serializer 的实例

java - Spring MVC 无法处理来自资源处理程序的 404?