Springboot初始化启动报错Error creating bean with name 'dataSource' defined in class path resource
作者:黄金原野
Springboot启动项目
Springboot新手,第一次搭建环境,启动项目,报错如下。
看起来是某一个Class没有找到。
百度的答案只有一条,在启动类上添加@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
事实证明无果。
后期怀疑是不是mysql驱动的版本问题,https://mvnrepository.com/ 上搜索替换了N个版本,报错相同。
走投无路之际怀疑是不是配置文件写错了,盯着看了几分钟,driver-class-name: com.mysql.jdbc.driver
怎么看怎么奇怪。
试着改成driver-class-name: com.mysql.jdbc.Driver
运行,成功!
如果遇到类似问题,在添加注解信息之外,可以考虑是不是配置文件写错等问题。毕竟这种问题很难百度,网上搜索到的类似报错也不会提供正确的写法。遂记录。
正确版本如下
spring: datasource: type: com.alibaba.druid.pool.DruidDataSource druid: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://localhost:3306/emos?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC username: root password: root initial-size: 2 max-active: 4 min-idle: 2 max-wait: 60000 test-while-idle: true test-on-borrow: false test-on-return: false
报错信息
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [com/alibaba/druid/spring/boot/autoconfigure/DruidDataSourceAutoConfigure.class]: Invocation of init method failed; nested exception is java.sql.SQLException: com.mysql.jdbc.driver
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1786) ~[spring-beans-5.3.9.jar:5.3.9]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:602) ~[spring-beans-5.3.9.jar:5.3.9]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:524) ~[spring-beans-5.3.9.jar:5.3.9]
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335) ~[spring-beans-5.3.9.jar:5.3.9]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-5.3.9.jar:5.3.9]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) ~[spring-beans-5.3.9.jar:5.3.9]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[spring-beans-5.3.9.jar:5.3.9]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:944) ~[spring-beans-5.3.9.jar:5.3.9]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:918) ~[spring-context-5.3.9.jar:5.3.9]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:583) ~[spring-context-5.3.9.jar:5.3.9]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:145) ~[spring-boot-2.5.4.jar:2.5.4]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:754) [spring-boot-2.5.4.jar:2.5.4]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:434) [spring-boot-2.5.4.jar:2.5.4]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:338) [spring-boot-2.5.4.jar:2.5.4]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1343) [spring-boot-2.5.4.jar:2.5.4]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1332) [spring-boot-2.5.4.jar:2.5.4]
at com.example.emos.EmosApplication.main(EmosApplication.java:13) [classes/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_281]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_281]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_281]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_281]
at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) [spring-boot-devtools-2.5.4.jar:2.5.4]
Caused by: java.sql.SQLException: com.mysql.jdbc.driver
at com.alibaba.druid.util.JdbcUtils.createDriver(JdbcUtils.java:604) ~[druid-1.1.13.jar:1.1.13]
at com.alibaba.druid.pool.DruidDataSource.init(DruidDataSource.java:854) ~[druid-1.1.13.jar:1.1.13]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_281]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_281]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_281]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_281]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeCustomInitMethod(AbstractAutowireCapableBeanFactory.java:1912) ~[spring-beans-5.3.9.jar:5.3.9]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1854) ~[spring-beans-5.3.9.jar:5.3.9]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1782) ~[spring-beans-5.3.9.jar:5.3.9]
... 21 common frames omitted
Caused by: java.lang.ClassNotFoundException: com.mysql.jdbc.driver
at java.net.URLClassLoader.findClass(URLClassLoader.java:382) ~[na:1.8.0_281]
at java.lang.ClassLoader.loadClass(ClassLoader.java:418) ~[na:1.8.0_281]
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:355) ~[na:1.8.0_281]
at java.lang.ClassLoader.loadClass(ClassLoader.java:351) ~[na:1.8.0_281]
at java.lang.Class.forName0(Native Method) ~[na:1.8.0_281]
at java.lang.Class.forName(Class.java:264) ~[na:1.8.0_281]
at com.alibaba.druid.util.JdbcUtils.createDriver(JdbcUtils.java:602) ~[druid-1.1.13.jar:1.1.13]
... 29 common frames omitted
以上就是Springboot初始化启动报错Error creating bean with name 'dataSource' defined in class path resource的详细内容,更多关于Springboot初始化启动报错的资料请关注脚本之家其它相关文章!