Field * in * required a bean of type ‘*‘ that could not be

在这里插入图片描述

Field testDao in com.service.TestService required a bean of type
‘com.dao.TestDao’ that could not be found.

注入bean失败!!

重点一:
注入失败bean的包是否是启动类的同包目录或是子包目录;
说明:spring boot 默认的扫描路径是启动类当前的包和子包,所以我们在创建项目的时候需要注意,启动类Application需要在最底层
如果已经不能修改路径:
一.添加@ComponentScan(“com.dao”)注解
项目会扫描com.dao同包和子包下的所有@@Component标记过的bean;

重点二:
在多模块项目的情况下,如果Controller层注入失败bean是接口需要注意是在这个Controller所在的模块中是否能够访问接口实现类
说明:spring 无法注入没有实现类的接口

        <dependency>
            <groupId>com.example</groupId><!--  模块的共同包名 -->
            <artifactId>domain</artifactId><!--  接口实现类所在的模块 -->
            <version>0.0.1-SNAPSHOT</version>
        </dependency>

重点三:
@Autowired注入的对象是否是有参构造
在这里插入图片描述
说明:

如果以上都能做到即使这样写,也可以启动:
接口:Spring 无法注入有参构造对象

public interface HelloInterface {
    String hello(String parameter);
}

实现接口的实现类

@Service
public class HelloInterfaceImp implements HelloInterface {
    @Override
    public String hello(String parameter) {
        return "hello"+parameter;
    }

调用接口的Controller层

@RestController
public class HelloServer {
    @Autowired
    private HelloInterface helloInterface;

    @GetMapping("/hello")
    public String helloMethod(@RequestParam("parameter") String parameter) {
        return helloInterface.hello(parameter);
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值