解决Error starting ApplicationContext.To display the conditions report re-run your application xxx的问题

目录

  • 1. 配置文件的错误
  • 2. 编译的错误
  • 3. 定义请求接口重复的错误
  • 4. 没加@Mapper注解的错误
  • 5. 端口重复错误
  • 6. 包冲突的错误
  • 7. 总结
  • 8. 参看文档

解决Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.的问题。

1. 配置文件的错误

今天在项目启动类中,加上这行代码@MapperScan("com.**.mapper")后,报出了如下错误:

com.superjson.superjsonmanager.SuperJsonManagerApplication
Connected to the target VM, address: '127.0.0.1:49879', transport: 'socket'

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v2.7.1)

2022-08-05 16:37:27.699  INFO 152900 --- [           main] c.s.s.SuperJsonManagerApplication        : Starting SuperJsonManagerApplication using Java 1.8.0_102 on DESKTOP-UVTEHFR with PID 152900 (D:\project\1myProject\superjson\superjsonmanager\target\classes started by zby in D:\project\1myProject\superjson\superjsonmanager)
2022-08-05 16:37:27.702  INFO 152900 --- [           main] c.s.s.SuperJsonManagerApplication        : No active profile set, falling back to 1 default profile: "default"
2022-08-05 16:37:28.166  WARN 152900 --- [           main] o.m.s.mapper.ClassPathMapperScanner      : No MyBatis mapper was found in '[com.**.mapper]' package. Please check your configuration.
2022-08-05 16:37:28.408  INFO 152900 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8088 (http)
2022-08-05 16:37:28.414  INFO 152900 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2022-08-05 16:37:28.414  INFO 152900 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.64]
2022-08-05 16:37:28.504  INFO 152900 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2022-08-05 16:37:28.505  INFO 152900 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 772 ms
2022-08-05 16:37:28.719  WARN 152900 --- [           main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration': Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.boot.context.properties.ConfigurationPropertiesBindException: Error creating bean with name 'mybatis-org.mybatis.spring.boot.autoconfigure.MybatisProperties': Could not bind properties to 'MybatisProperties' : prefix=mybatis, ignoreInvalidFields=false, ignoreUnknownFields=true; nested exception is org.springframework.boot.context.properties.bind.BindException: Failed to bind properties under 'mybatis.configuration' to org.apache.ibatis.session.Configuration
2022-08-05 16:37:28.721  INFO 152900 --- [           main] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
2022-08-05 16:37:28.729  INFO 152900 --- [           main] ConditionEvaluationReportLoggingListener : 

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2022-08-05 16:37:28.740 ERROR 152900 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************

Description:

Failed to bind properties under 'mybatis.configuration' to org.apache.ibatis.session.Configuration:

    Reason: org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [java.lang.String] to type [@org.springframework.boot.context.properties.NestedConfigurationProperty org.apache.ibatis.session.Configuration]

Action:

Update your application's configuration

Disconnected from the target VM, address: '127.0.0.1:49879', transport: 'socket'

Process finished with exit code 1

通过分析代码后,发现配置文件出现了问题,如下图所示:

解决Error starting ApplicationContext.To display the conditions report re-run your application xxx的问题_第1张图片
字段configuration给到了空值,导致spring boot无法获取到对应的值,报出上面的错误。只要把该字段删除即可启动spring boot。

这个错误存在多种情况,如果以上解决方案无法解决你的问题,可以继续查看如下的解决方案,大概能解决你的实际问题。

2. 编译的错误

仔细看报出的问题,有没有具体报错信息,如上图所示,这种情况进行如下操作:

  1. clean清理一下

  2. 然后install重新编译一下

解决Error starting ApplicationContext.To display the conditions report re-run your application xxx的问题_第2张图片

  1. 也可以用这个命令,重新编译一下

解决Error starting ApplicationContext.To display the conditions report re-run your application xxx的问题_第3张图片

此时,清除缓存后重新启动,如下图所示:

解决Error starting ApplicationContext.To display the conditions report re-run your application xxx的问题_第4张图片

解决Error starting ApplicationContext.To display the conditions report re-run your application xxx的问题_第5张图片

如果还报同样的错误,打开如下图位置,看是否有编译的文件,如mapper.xml文件,application.yml或者application.properties文件,以及一些配置文件等

解决Error starting ApplicationContext.To display the conditions report re-run your application xxx的问题_第6张图片
这里发现真的没有mapper和配置文件,只有main下面的.jar文件,该怎么办呢?

pom.xml文件中给加入要编译的文件位置依赖,如下:(这是我的文件位置,你根据你的进行调整)


<resources>


 <resource>
  <directory>libdirectory>
   <targetPath>BOOT-INF/lib/targetPath>
   <includes>
       <include>**/*.jarinclude>
   includes>
 resource>
 
 
 <resource>
  <directory>src/main/javadirectory>
   <includes>
       <include>**/*.propertiesinclude>
       <include>**/*.xmlinclude>
   includes>
 resource>
 
 
 <resource>
  <directory>src/main/resourcesdirectory>
   <includes>
       <include>**/*.propertiesinclude>
       <include>**/*.xmlinclude>
   includes>
   <filtering>falsefiltering>
 resource>
resources>

如是,清理 ->打包 -> 启动 -> 运行即可。

3. 定义请求接口重复的错误

日志说明是路径问题,你可以思考下:是不是写的接口名字重复了,或者只有一个斜杠或没斜杠的情况:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'requestMappingHandlerMapping' defined in class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]: Invocation of init method failed; nested exception is java.lang.IllegalStateException: Ambiguous mapping. Cannot map 'indexController' method 
com.superjson.superjsonmanager.controller.IndexController#heart()
to {GET [/heart]}: There is already 'indexController' bean method
com.superjson.superjsonmanager.controller.IndexController#heartbeat() mapped.
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1804) ~[spring-beans-5.3.21.jar:5.3.21]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:620) ~[spring-beans-5.3.21.jar:5.3.21]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542) ~[spring-beans-5.3.21.jar:5.3.21]
	at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335) ~[spring-beans-5.3.21.jar:5.3.21]
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-5.3.21.jar:5.3.21]
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) ~[spring-beans-5.3.21.jar:5.3.21]
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[spring-beans-5.3.21.jar:5.3.21]
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:955) ~[spring-beans-5.3.21.jar:5.3.21]
	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:918) ~[spring-context-5.3.21.jar:5.3.21]
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:583) ~[spring-context-5.3.21.jar:5.3.21]
	at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:147) ~[spring-boot-2.7.1.jar:2.7.1]
	at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:734) [spring-boot-2.7.1.jar:2.7.1]
	at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:408) [spring-boot-2.7.1.jar:2.7.1]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:308) [spring-boot-2.7.1.jar:2.7.1]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1306) [spring-boot-2.7.1.jar:2.7.1]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1295) [spring-boot-2.7.1.jar:2.7.1]
	at com.superjson.superjsonmanager.SuperJsonManagerApplication.main(SuperJsonManagerApplication.java:43) [classes/:na]
Caused by: java.lang.IllegalStateException: Ambiguous mapping. Cannot map 'indexController' method 
com.superjson.superjsonmanager.controller.IndexController#heart()
to {GET [/heart]}: There is already 'indexController' bean method
com.superjson.superjsonmanager.controller.IndexController#heartbeat() mapped.
	at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping$MappingRegistry.validateMethodMapping(AbstractHandlerMethodMapping.java:669) ~[spring-webmvc-5.3.21.jar:5.3.21]
	at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping$MappingRegistry.register(AbstractHandlerMethodMapping.java:635) ~[spring-webmvc-5.3.21.jar:5.3.21]
	at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.registerHandlerMethod(AbstractHandlerMethodMapping.java:332) ~[spring-webmvc-5.3.21.jar:5.3.21]
	at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping.registerHandlerMethod(RequestMappingHandlerMapping.java:420) ~[spring-webmvc-5.3.21.jar:5.3.21]
	at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping.registerHandlerMethod(RequestMappingHandlerMapping.java:76) ~[spring-webmvc-5.3.21.jar:5.3.21]
	at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.lambda$detectHandlerMethods$2(AbstractHandlerMethodMapping.java:299) ~[spring-webmvc-5.3.21.jar:5.3.21]
	at java.util.LinkedHashMap.forEach(LinkedHashMap.java:684) ~[na:1.8.0_102]
	at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.detectHandlerMethods(AbstractHandlerMethodMapping.java:297) ~[spring-webmvc-5.3.21.jar:5.3.21]
	at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.processCandidateBean(AbstractHandlerMethodMapping.java:266) ~[spring-webmvc-5.3.21.jar:5.3.21]
	at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.initHandlerMethods(AbstractHandlerMethodMapping.java:225) ~[spring-webmvc-5.3.21.jar:5.3.21]
	at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.afterPropertiesSet(AbstractHandlerMethodMapping.java:213) ~[spring-webmvc-5.3.21.jar:5.3.21]
	at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping.afterPropertiesSet(RequestMappingHandlerMapping.java:205) ~[spring-webmvc-5.3.21.jar:5.3.21]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1863) ~[spring-beans-5.3.21.jar:5.3.21]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1800) ~[spring-beans-5.3.21.jar:5.3.21]
	... 16 common frames omitted

如下截图:

解决Error starting ApplicationContext.To display the conditions report re-run your application xxx的问题_第7张图片
修改如下:

解决Error starting ApplicationContext.To display the conditions report re-run your application xxx的问题_第8张图片

重新启动即可。

4. 没加@Mapper注解的错误

这些情况就是看描述的问题,如图所示:

解决Error starting ApplicationContext.To display the conditions report re-run your application xxx的问题_第9张图片

这种情况,看是不是没有扫描到它需要的东西,给加上@Mapper注解(扫描dao层的),或者加上@Service注解(扫描service层)即可。

5. 端口重复错误

查看是否自己的运行端口冲突,将其kill杀死,重新启动,或者跟换端口。

以下是查看端口冲突的方式:

  1. window键+R,输入netstat -aon|findstr 8080找到占用8080端口的进程号:

解决Error starting ApplicationContext.To display the conditions report re-run your application xxx的问题_第10张图片

  1. 输入tasklist|findstr "3412",查看进程号为3412的进程:

解决Error starting ApplicationContext.To display the conditions report re-run your application xxx的问题_第11张图片

  1. 输入taskkill /f /t /im QQ.exe:(/f:强制性的结束进程),结束这个进程:

解决Error starting ApplicationContext.To display the conditions report re-run your application xxx的问题_第12张图片
现在,可以重新启动一下,看是否运行成功。

6. 包冲突的错误

如果是包冲突,可以将maven仓库清理一下自己的包,重新下载一下:

解决Error starting ApplicationContext.To display the conditions report re-run your application xxx的问题_第13张图片

7. 总结

如果以上解决方案人,仍旧无法解决你的问题,而你其他方式解决了,可以在评论区评论,大家一起快乐地成长。

8. 参看文档

https://blog.csdn.net/m0_50762431/article/details/122143601

你可能感兴趣的:(java,spring框架,mybatis,spring,boot,java,spring,boot常见错误,mapper)