【问题标题】:Error creating bean with name 'countriesDao': Injection of autowired dependencies failed;创建名为“countriesDao”的 bean 时出错:注入自动装配的依赖项失败;
【发布时间】:2012-04-25 22:21:29
【问题描述】:

我正在尝试启动 Tomcat,但是当我尝试启动它时出现以下错误

SEVERE: Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'countriesDao': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire method: public void com.fexco.helloworld.web.util.CustomHibernateDaoSupport.anyMethodName(org.hibernate.SessionFactory); nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [org.hibernate.SessionFactory] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}

国道

package com.fexco.helloworld.web.dao;

import com.fexco.helloworld.web.model.Countries;

public interface CountriesDao {


void save(Countries countries);
void update(Countries countries);
void delete(Countries countries);
Countries findByCountry(String country);
}

CountriesDaoImpl 的开始

package com.fexco.helloworld.web.dao;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.fexco.helloworld.web.model.Countries;
import com.fexco.helloworld.web.util.CustomHibernateDaoSupport;

@Repository("countriesDao")
public class CountriesDaoImpl extends CustomHibernateDaoSupport implements CountriesDao{

public void save(Countries countries){
    getHibernateTemplate().save(countries);
}
......
}

一些Application-config.xml

<bean id="countriesDao" class="com.fexco.helloworld.web.dao.CountriesDaoImpl">
    <property name="sessionFactory" ref="sessionFactory"/>
</bean>
.... 
<context:annotation-config />
<context:component-scan base-package="com.fexco.helloworld.web" />

<bean 
  class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>

CustomHibernateDaoSupport 类

package com.fexco.helloworld.web.util;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

public abstract class CustomHibernateDaoSupport extends HibernateDaoSupport
{    
@Autowired
public void anyMethodName(SessionFactory sessionFactory)
{
    setSessionFactory(sessionFactory);
}
}

这个错误是因为 CountryDaoImpl 没有真正实现 CountryDao 吗? 有谁知道如何解决这个错误?

谢谢

【问题讨论】:

  • 这个问题有点令人困惑。您说的是 CountryDAO,但 DAOImpl 的代码是 CustomerDAOImpl。此外,异常清楚地表明您从 Hibernate 使用的 SessionFactory 无法自动装配。如果我看到您正在混合注释和 xml 配置的代码。试着坚持一个。检查您是否在 DAO 中定义了正确的设置器以供 sessionFactory 注入。还请编辑并放置 CountryDAOImpl 的相应代码。错误似乎发生在 CustomHibernateDaoSupport 中,请同时粘贴该代码。
  • 很抱歉,我也有一个 customerDao 和 customerDaoImpl...我已经更改了它并设置了 CustomHibernateDaoSupport 类。抱歉,我是新手,所以我想尽可能多地学习,所以带走 xml 配置会不会更好,因为注释无论如何都会这样做?
  • 请查看此文档以正确配置和自动装配休眠会话工厂static.springsource.org/spring/docs/3.0.x/…
  • 我已经解决了这个问题。我的 web.xml 没有指向我的 application-context.xml,所以它找不到我的 hibernate.xml 和 DataSource.xml 文件

标签: java spring hibernate tomcat spring-mvc


【解决方案1】:

您的 dao 实现类中似乎没有定义会话工厂。如果尚未定义,您应该定义一个并避免同时使用这两种配置它会搞砸事情,即

            @Autowired
        @Qualifier("sessionFactory")
        public void seSessionFactory(SessionFactory sessionFactory) {

            this.setSessionFactory(sessionFactory);
        }

【讨论】:

    猜你喜欢
    • 2017-09-24
    • 1970-01-01
    • 1970-01-01
    • 2015-12-06
    • 2019-06-10
    • 2014-06-09
    • 2018-06-22
    • 2014-11-24
    • 2015-06-12
    相关资源
    最近更新 更多