【问题标题】:Can not autowire interface无法自动装配接口
【发布时间】:2015-07-17 16:07:30
【问题描述】:

当我尝试从控制器自动连接接口时,出现以下异常: 堆栈跟踪:

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

我的控制器如下所示:

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 作为基础包。

AccountDAO:

package com.projectShaun.dao;

import com.projectShaun.model.Account;

public interface AccountDao {

void persistAccount (Account account);
}

【问题讨论】:

  • 您的问题出在服务中声明的DAO中。将其设置为@Repository
  • 读取堆栈跟踪:无法自动装配字段:私有 com.projectShaun.dao.AccountDao -> 无法自动装配字段:org.hibernate.SessionFactory。没有为依赖找到类型 [org.hibernate.SessionFactory] ​​的合格 bean
  • 能否添加您的AccountDao
  • 已编辑问题,现在包含 accountDAO

标签: java spring spring-mvc


【解决方案1】:

异常说:

  • 您正确(尝试)在您 HomeController bean 中自动装配 accountService bean
  • 您正确地(尝试)在accountService 中自动连接AccountDAO

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

【讨论】:

  • 我认为 framework.orm.hibernate4.LocalSessionFactoryBean 没有实现 org.hibernate.SessionFactory
  • @JosefProcházka :它没有,但它实现了FactoryBean&lt;SessionFactory&gt;。所以如果你让 Spring 注入它,你实际上会得到一个SessionFactory。但我认为您要求AccountDaoImpl 的代码是正确的...
【解决方案2】:

只有一个spring管理的对象可以Autowire另一个组件/服务/存储库,所以AccountDao需要用@Named@Component注释为Spring bean。

【讨论】:

    【解决方案3】:

    这里的问题是Spring由于缺少依赖而无法创建Bean

    例外是 org.springframework.beans.factory.NoSuchBeanDefinitionException: 否 找到类型为 [org.hibernate.SessionFactory] ​​的合格 bean 依赖项:预计至少有 1 个符合自动装配条件的 bean 这种依赖的候选人。依赖注解: {@org.springframework.beans.factory.annotation.Autowired(required=true)

    明确指出它没有为 SessionFactory 配置 bean。 这是 AccountDaoImpl.sessionFactory 所要求的。

    【讨论】:

      猜你喜欢
      • 2023-03-16
      • 2015-05-27
      • 2017-05-23
      • 2011-01-24
      • 2018-12-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-27
      相关资源
      最近更新 更多