idea使用备忘,出现Could not autowire. No beans of 'BoyRepository' type found

idea使用备忘,出现Could not autowire. No beans of 'BoyRepository' type found

你好! 这是你第一次使用 Markdown编辑器 所展示的欢迎页。如果你想学习如何使用Markdown编辑器, 可以仔细阅读这篇文章,了解一下Markdown的基本语法知识。

方法一:加@Repository

转自stackoverflow
https://stackoverflow.com/questions/16241946/intellij-idea-could-not-autowire-no-beans-of-applicationrepository-type-fou
I followed the same tutorial and ran into the same code inspection warning (even if the application was working fine, the IDE was complaining). To make fix it, I added @Repository to my JpaRepository:

package com.example.demo;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository  //在定义Repository接口的位置加上这个注解,声明为Spring的Bean即可
public interface BoyRepository extends JpaRepository<Boy, Integer> {
}

方法二:降低idea的Autowired检测级别

在Editor >> Inspections >> Spring >> Spring Core >> Code >>Autowiring for Bean Class 把Serverity级别由Error改编为Warning >> Apply >> OK 查看效果 无异常提示 搞定

在这里插入图片描述

方法三:在Autowired后加(required = false)

在注解上加上:@Autowired(required = false)

package com.example.demo;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class BoyController {

    @Autowired(required = false)
    private BoyRepository boyRepository;

    @GetMapping(value = "/boy")
    public List<Boy> findAll() {
        return boyRepository.findAll();
    }

}

PS

还有一个原因,这个博主没有遇到,友情粘贴!
便是我们导入 @Service 包的时候导入包错误造成的。

spring auto scan 配置,在编辑情况下,无法找不到对应的bean,于是提示找不到对应 bean 的错误。常见于 mybatis 的 mapper,如下:

<!-- mapper scanner configurer -->
<bean id="mapperScannerConfig" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
  <property name="basePackage" value="com.adu.spring_test.mybatis.dao" />
  <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
</bean>

解决办法:

错误导包 import com.alibaba.dubbo.config.annotation.Service;
正确的包应该是下面这个:

import org.springframework.stereotype.Service;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值