我有一个用@Configuration
注释的DAO类。在这个DAO类中,我有一个定义为
@Bean
public MyDao myDao(){
return new MyDao(dataSource);
}
现在我想在我的服务类中使用这个DAO。所以我用@Service
注释了服务类并使用了
@Autowired private IMyDao myDao;
在运行测试时,我收到以下错误
org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [IMyDao] 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)}
答案 0 :(得分:0)
将此添加到您的应用程序上下文
<context:component-scan base-package="your top level package">
<context:include-filter type="regex"
expression="com\.your package\..*Dao.*" />
<context:include-filter type="regex"
expression="com\.your package\..*Service.*" />
<context:exclude-filter type="annotation"
expression="org.springframework.stereotype.Controller" />
</context:component-scan>