在搭建一个简单的关于springboot的AOP编程时,遇到一个Consider defining a bean of type ‘com.springboot.chapter04.service.impl.UserServiceImpl’ in your configuration的问题。

Description:

Field helloService in com.example.demo.service.TestController required a bean of type 'com.springboot.chapter04.service.impl.UserServiceImpl' that could not be found.


Action:

Consider defining a bean of type 'com.springboot.chapter04.service.impl.UserServiceImpl' in your configuration.

最后找到问题,就是springboot启动类包扫描的路径和我接口实现类的路径不是在同一个目录中
解决办法是将启动类包扫描的路径改成与实现类相同的路径
实现类的路径是

package com.springboot.chapter04.service.impl;
@SpringBootApplication(scanBasePackages = "com.springboot.chapter04")

这样做的一个缺点是将一些不需要注入的类也一起注入了
另一个方法是将实现类移到启动类的同一个路径下或者子路径下

更多推荐

spring boot自动注入出现Consider defining a bean of type 'xxx' in your config