0

我正在尝试启动 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);
}

CountryDaoImpl 的开始

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 吗?有谁知道如何解决这个错误?

谢谢

4

1 回答 1

0

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

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

            this.setSessionFactory(sessionFactory);
        }
于 2012-04-13T14:47:59.013 回答