解决方案 --[restartedMain] o.s.b.d.LoggingFailureAnalysisReporter :

问题描述:

Error starting ApplicationContext. To display the conditions report re-run your application with ‘debug’ enabled.
ERROR 9680 — [ restartedMain] o.s.b.d.LoggingFailureAnalysisReporter

问题分析

这个问题在网上找到很多方法,多半都是删除某某依赖,或者更改依赖版本,或者添加:(exclude={DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class})

但是笔者使用上述方法后仍然不管用,依然存在这个问题。

在这上面debug弄了一个小时,这时候,终于想起来看看IDEA的错误提示
给的提示是:

Description:

Field userRepository in com.example.usermanag.service.UserServiceImp required a bean of type
‘com.example.usermanag.repository.UserRepository’ that could not be found.

The injection point has the following annotations:

  • @org.springframework.beans.factory.annotation.Autowired(required=true)

Action:

Consider defining a bean of type
‘com.example.usermanag.repository.UserRepository’ in your configuration.

仔细阅读,可以发现关键在The injection point has the following annotations: @org.springframework.beans.factory.annotation.Autowired(required=true)这句话。

笔者回溯代码时才发现有个地方用了@Autowired 进行自动注入,查阅官方文档和其他博客,发现错误的原因在于:
spring framerwork 4.0以后就不推荐使用属性注入,改为推荐构造器注入和setter注入,因为属性注入方式容易出现循环依赖问题,即A注入B,B注入C,C注入A,这种情况很容易会报异常。

解决方案

法一:将@Autowired 改为 @Qualifier 进行注入

法二:将@Autowired 改为setter方式注入

法三:老老实实new对象,不使用自动注入

法四:应该还是可以继续以另外的方式使用@Autowired(这点暂时笔者也不会,等待各位补充)

类似错误但是是其他问题:请一定要看错误信息

猜你喜欢

转载自blog.csdn.net/air__Heaven/article/details/123365536