Could not autowire. No beans of ‘ILoginService‘ type found. SpringBoot中Controller调用接口出错记录

Could not autowire. No beans of ‘ILoginService’ type found.
iLoginService显示错误,错误信息:Could not autowire. No beans of ‘ILoginService’ type found.

@Controller
@RequestMapping("user")
public class TestController {

    @Autowired
    private ILoginService iLoginService;

    @PostMapping("login")
    @ResponseBody
    public LoginResponse test(@RequestParam("username")String userName, @RequestParam("password")String pwd) {
        return iLoginService.loginResponse(userName, pwd);
    }
}

主要原因没有吧所有对象交给Spring管理,类比springmvc中通过applicationContext.xml注入整个所有包的对象:

<context:component-scan base-package="com.mysutdy.springservlet">
    </context:component-scan>

springboot也应该在:DemoApplication中注入整个包对象,可以用:

@ComponentScan("com.example.demo")

但是由于已经有了:

```java
@SpringBootApplication

注解,而且该注解里边包含注入包对象的东西,base-package,所以最后形式是:

@SpringBootApplication(scanBasePackages = "com.example.demo")
//@EnableAutoConfiguration(exclude ={ DataSourceAutoConfiguration.class})//排除自动配置
public class DemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

如此一开,就把所有包注入进去,但是要主意应该在 controller service model层加上@controller @service @Repository注解这样代表他们吧自己的对象都发放到spring的IOC中,让它进行管理与分配。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值