简体   繁体   English

无法@Autowire @Bean属性

[英]Cannot @Autowire a @Bean property

I have a DAO class which is annotated with @Configuration . 我有一个用@Configuration注释的DAO类。 In this DAO class I have a bean defined as 在这个DAO类中,我有一个定义为

@Bean
public MyDao myDao(){
    return new MyDao(dataSource);
}

Now I want to use this DAO in my service class. 现在,我想在服务类中使用此DAO。 So I annotated the service class with @Service and used 所以我用@Service注释了服务类并使用了

@Autowired private IMyDao myDao;

While running the tests I get the below error 在运行测试时,出现以下错误

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

Add this to your application context 将此添加到您的应用程序上下文

 <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>  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM