解决方案 --Spring framerwork+Mysql+Tomcat遇见的各种报错与踩坑避坑记录

343 阅读2分钟

持续创作,加速成长!这是我参与「掘金日新计划 · 10 月更文挑战」的第29天,点击查看活动详情

本篇主要记录一些:Spring framerwork+mysql+Tomcat遇见的各种报错与踩坑避坑记录

一、ERROR 12028 --- [ restartedMain] com.zaxxer.hikari.pool.HikariPool : HikariPool-1 - Exception during pool initialization.

问题描述

ERROR 12028 --- [ restartedMain] com.zaxxer.hikari.pool.HikariPool : HikariPool-1 - Exception during pool initialization.

ERROR 12028 --- [ restartedMain] j.LocalContainerEntityManagerFactoryBean : Failed to initialize JPA EntityManagerFactory: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]

在这里插入图片描述

问题原因1:

因为mysql服务没打开 在这里插入图片描述

问题解决1:

1、打开服务窗口 在这里插入图片描述 2、打开mysql服务,等待后显示正在运行就是成功了 在这里插入图片描述 在这里插入图片描述

问题原因2:

因为mysql用户名或者密码错误,或者mysql数据库的url无法正常访问

在这里插入图片描述

问题解决2:

打开mysql核对自己的用户名、密码、端口号 在这里插入图片描述 并核对自己的数据库 在这里插入图片描述

然后填入application.properties文件中 在这里插入图片描述

二、Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled

问题描述:

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(这点暂时笔者也不会,等待各位补充)

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

三、Tomcat警告 [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property

问题描述:

Eclipse启动服务器提示[SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:WebProject' did not find a matching property.

解决方案:

步骤一:首先右击服务器,选择停止Tomcat在这里插入图片描述 在这里插入图片描述

步骤二:双击eclipse下tomcat服务器,如下图,找到server(服务器)选项,选中其中的选项”Publish modual contexts to separat XML files“,ctr+s保存,然后再次启动tomcat。 在这里插入图片描述