Ibatis的春季自动装配失败了

时间:2014-10-08 12:12:24

标签: spring autowired

我是Spring的新手,并试图从服务层(@Service)为我的DAOImpl类(@Repository)使用@Autowire注释

这是我的代码:

@Service
public class DefaultLocationService implements LocationService
{
@Autowired
@Qualifier("countryDao")
private CountryDAO countryDao; // This is an interface

public void setCountryDao(CountryDAO countryDao) {
    this.countryDao = countryDao;
}


// More code
}

然后我有CountryDaoImpl类(@Repository)实现CountryDAO

@Repository("countryDao")
public class CountryDAOImpl extends InstrumentedSqlMapDaoTemplate implements CountryDAO {

@Autowired
public CountryDAOImpl(DaoManager daoManager) {
    super(daoManager);
}
// More code
}



public class InstrumentedSqlMapDaoTemplate extends SqlMapDaoTemplate
{

public InstrumentedSqlMapDaoTemplate(DaoManager daoManager)
{
    super(daoManager);
}
// More code
}

SqlMapDaoTemplate会出现问题,因为它有一个带参数的构造函数,因此Spring无法实例化默认构造函数并失败。为此,我在我的DAOImpl类中自动装配了构造函数,但仍然没有运气,并且它因此异常而失败:

  Unsatisfied dependency expressed through constructor argument  with
 index 0 of type [com.ibatis.dao.client.DaoManager]: : No matching bean
 of type [com.ibatis.dao.client.DaoManager] found for dependency:
 expected at least 1 bean which qualifies as autowire candidate for
 this dependency. Dependency ann otations: {}; nested exception is
 org.springframework.beans.factory.NoSuchBeanDefinitionException: No
 matching bean of t ype [com.ibatis.dao.client.DaoManager] found for
 dependency: expected at least 1 bean which qualifies as autowire
 candid ate for this dependency. Dependency annotations: {}

你能告诉我这里我做错了吗?

谢谢, 特洛伊

1 个答案:

答案 0 :(得分:0)

问题正是消息所说的。你缺少DaoManager类型的bean。当Spring创建countryDay bean时,它会尝试使用构造函数public CountryDAOImpl(DaoManager daoManager)。但它也需要(根据您的配置)找到类型为DaoManager的bean。

仅使用@Repository注释注释DaoManager类。