报错:
Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled.
2018-05-10 19:59:57.321 ERROR 8692 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter :
***************************
APPLICATION FAILED TO START
***************************
Description:
Field userService in com.mooc.house.web.controller.UserController required a bean of type 'com.mooc.house.biz.service.UserService' that could not be found.
Action:
Consider defining a bean of type 'com.mooc.house.biz.service.UserService' in your configuration.
根据控制台打印的提示是在配置中找不到一个指定自动注入类型的bean,经过多方排查得出结论:
正常情况下加上@Component注解的类会自动被Spring扫描到生成Bean注册到spring容器中,既然他说没找到,也就是该注解被没有被spring识别,问题的核心关键就在application类的注解SpringBootApplication上
这个注解其实相当于下面这一堆注解的效果,其中一个注解就是@Component,在默认情况下只能扫描与控制器在同一个包下以及其子包下的@Component注解,以及能将指定注解的类自动注册为Bean的@Service@Controller和@ Repository
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(excludeFilters={@Filter(type=CUSTOM, classes={TypeExcludeFilter.class}), @Filter(type=CUSTOM, classes={AutoConfigurationExcludeFilter.class})})
@Target(value={TYPE})
@Retention(value=RUNTIME)
@Documented
@Inherited
以下解决方案:
两种解决办法:
1 .将接口与对应的实现类放在与application启动类的同一个目录或者他的子目录下,这样注解可以被扫描到,这是最省事的办法
2 .在指定的application类上加上这么一行注解,手动指定application类要扫描哪些包下的注解,如下
@SpringBootApplication @ComponentScan(basePackages = "要检索的包路径") public class HouseWebApplication { public static void main(String[] args) { SpringApplication.run(HouseWebApplication.class, args); } }