有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

创建名为“countriesDao”的bean时发生java错误:自动连线依赖项的注入失败;

我试图启动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);
}
......
}

一些应用程序配置。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"/>

自定义HibernatedAOSupport类

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

错误是因为CountriesDaoImpl没有真正实现CountriesDao吗? 有人知道如何解决这个错误吗

谢谢


共 (1) 个答案

  1. # 1 楼答案

    似乎在dao实现类中没有定义会话工厂。如果尚未定义,则应定义一个,并避免使用这两种配置,因为这会使事情变得一团糟,例如

                @Autowired
            @Qualifier("sessionFactory")
            public void seSessionFactory(SessionFactory sessionFactory) {
    
                this.setSessionFactory(sessionFactory);
            }