spring-data-r2dbc icon indicating copy to clipboard operation
spring-data-r2dbc copied to clipboard

Unable to inject DAO interface,expected at least 1 bean which qualifies as autowire candidate.

Open xiaobo1997 opened this issue 3 years ago • 1 comments

1.illustrate springboot 2.3.7 springboot-data-r2dbc 1.1.6 2.I used Spring-Data-R2DBC to operate mysql, but I have been reporting an errororg.springframework.beans.factory.UnsatisfiedDependencyException

3.Complete error description

 cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'webFluxController': Unsatisfied dependency expressed through field 'umsAddressService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'umsAddressServiceImpl': Unsatisfied dependency expressed through field 'umsAddressDao'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.xxx.inner.UmsAddressDao' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}


Field umsAddressDao in com.xxx.impl.UmsAddressServiceImpl required a bean of type 'com.xxx.inner.UmsAddressDao' that could not be found.

3.My code snippet


@RestController
public class WebFluxController {

    private Logger log = LoggerFactory.getLogger(WebFluxController.class);

    @Autowired
    private UmsAddressService umsAddressService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private QuestionService questionService;
    // do something
}

public interface UmsAddressService {
    Mono<UmsAddress> findById(Long id);


}


@Service
public class UmsAddressServiceImpl implements UmsAddressService {

    @Autowired
    private UmsAddressDao umsAddressDao;

    @Override
    public Mono<UmsAddress> findById(Long id) {
        return umsAddressDao.findById(id);
    }
}


@Repository
public interface UmsAddressDao extends ReactiveCrudRepository<UmsAddress,Long> {

}


@Data
@Accessors(chain = true)
@ToString
@Table("ums_address")
public class UmsAddress {


    @Id
    @Column("id")
    private Long id;
  
    @Column("member_id")
    private Long memberId;

  // other field

}



@EnableWebFlux
@SpringBootApplication( exclude = { RedisRepositoriesAutoConfiguration.class })
@OpenAPIDefinition(info = @Info(title = "APIs", version = "1.0", description = "Documentation APIs v1.0"))
public class RollkingWebApplication {

    public static void main(String[] args) {
        new SpringApplicationBuilder(RollkingWebApplication.class).web(WebApplicationType.REACTIVE).run(args);
    }
}

xiaobo1997 avatar Apr 24 '22 07:04 xiaobo1997

I'm assuming you created the project via start.spring.io , your example is totally counter-intuitive and should head over to stactoverflow. Pointless @EnableWebFlux, @Repository and redundant @Column. And you are using an out-of-maintenance SpringBoot OSS version, which causes a certain psychological burden to cut this issue, because @Autowired has long been deprecated, and instead, constructor injection is recommended to expose more details.

linghengqian avatar May 21 '22 03:05 linghengqian

Please rebase your application to a more recent and supported version and report back if there are still issues.

gregturn avatar Mar 20 '23 15:03 gregturn