Cannot determine embedded database driver class for database type NONE

这个问题要从“ 

java.io.IOException: Server returned HTTP response code: 403 for URL: http://start.spring.io

”错误说起, eclipse  创建  spring boot  项目  遇到错误:java.io.IOException: Server returned HTTP response code: 403 for URL: http://start.spring.io

然后就在网页上创建项目下载,新建Maven工程,修改Pom.xml 文件……


完整错误信息如下:

2017-12-29 17:28:51.149  INFO 4429 --- [           main] com.wanyu.fams.App                       : Starting App on avenMac.local with PID 4429 (/Users/aven/Documents/workspace/fams/fams/target/classes started by aven in /Users/aven/Documents/workspace/fams/fams)
2017-12-29 17:28:51.153  INFO 4429 --- [           main] com.wanyu.fams.App                       : No active profile set, falling back to default profiles: default
2017-12-29 17:28:51.228  INFO 4429 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@4e7912d8: startup date [Fri Dec 29 17:28:51 CST 2017]; root of context hierarchy
2017-12-29 17:28:52.062  WARN 4429 --- [           main] o.m.s.mapper.ClassPathMapperScanner      : No MyBatis mapper was found in '[com.wanyu.fams]' package. Please check your configuration.
2017-12-29 17:28:53.358  INFO 4429 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2017-12-29 17:28:53.384  INFO 4429 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2017-12-29 17:28:53.386  INFO 4429 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.5.23
2017-12-29 17:28:53.564  INFO 4429 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2017-12-29 17:28:53.564  INFO 4429 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 2341 ms
2017-12-29 17:28:53.904  INFO 4429 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean  : Mapping servlet: 'dispatcherServlet' to [/]
2017-12-29 17:28:53.913  INFO 4429 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'characterEncodingFilter' to: [/*]
2017-12-29 17:28:53.914  INFO 4429 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2017-12-29 17:28:53.915  INFO 4429 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2017-12-29 17:28:53.915  INFO 4429 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'requestContextFilter' to: [/*]
2017-12-29 17:28:54.580  INFO 4429 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@4e7912d8: startup date [Fri Dec 29 17:28:51 CST 2017]; root of context hierarchy
2017-12-29 17:28:54.725  INFO 4429 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2017-12-29 17:28:54.727  INFO 4429 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2017-12-29 17:28:54.805  INFO 4429 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-12-29 17:28:54.805  INFO 4429 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-12-29 17:28:54.929  INFO 4429 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-12-29 17:28:55.030  WARN 4429 --- [           main] ationConfigEmbeddedWebApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Tomcat.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.apache.tomcat.jdbc.pool.DataSource]: Factory method 'dataSource' threw exception; nested exception is org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException: Cannot determine embedded database driver class for database type NONE. If you want an embedded database please put a supported one on the classpath. If you have database settings to be loaded from a particular profile you may need to active it (no profiles are currently active).
2017-12-29 17:28:55.034  INFO 4429 --- [           main] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
2017-12-29 17:28:55.064  INFO 4429 --- [           main] utoConfigurationReportLoggingInitializer : 

Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled.
2017-12-29 17:28:55.072 ERROR 4429 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

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

Description:

Cannot determine embedded database driver class for database type NONE

Action:

If you want an embedded database please put a supported one on the classpath. If you have database settings to be loaded from a particular profile you may need to active it (no profiles are currently active).
我认为错误产生的原因是:创建项目的时候添加了 MyBatis 依赖,但却没有添加对应的数据库配置。

分析原因

这是因为spring boot 会默认加载org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration这个类,

DataSourceAutoConfiguration类使用了@Configuration注解向spring注入了dataSource bean。因为工程中没有关于dataSource相关的配置信息,当spring创建dataSource bean因缺少相关的信息就会报错。

解决方案一

在类定义上增加@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})的注解。

解决方案二

在 application.properties 文件中添加数据库配置,例如:

#spring.datasource.type=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://your DataBase Server IP:3306/DatabaseName?useUnicode=true&zeroDateTimeBehavior=convertToNull&autoReconnect=true
spring.datasource.username=username
spring.datasource.password=password

注意,如果使用:

spring.datasource.type=com.mysql.jdbc.Driver

还需要在 pom.xml 中添加依赖:

		<!-- 导入Mysql数据库链接jar包 -->
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>5.1.41</version>
		</dependency>
注意注意,这里的一个坑:

spring.datasource.type 需要指定的是 dataSource 实现类,不是 Driver。

Driver 是这样写的:

spring.datasource.driver-class-name=com.mysql.jdbc.Driver

那么这个怎么填写呢?

1、不写!Spring Boot 默认的数据源是 org.apache.tomcat.jdbc.pool.DataSource

2、在实际开发中我们可能需要使用我们比较熟悉的数据源或者性能比比较高的数据源,比如Druid,那么很自然的我们就不能使用默认的数据源,这时候spring.datasource.type就诞生了。spring.datasource.type属性的存在能够使我们快速的指定我们需要的数据源,但是在使用的过程当中还是需要有些需要注意的!

需要注意的是:spring.datasource.type旧的spring boot版本是不能识别的。

(更新于2016年8月20日):很抱歉的是新版本也废弃了这个属性,配置完之后启动就会报错,所以现在知道的情况是1.2版本不支持,1.3版本支持,1.4.0版本不支持。具体信息可以看链接:http://stackoverflow.com/questions/39032368/not-able-to-set-spring-datasource-type-in-spring-boot-1-4 )

(更新于2016年9月24日):spring boot 1.4.1重新支持了spring.datasource.type属性,从1.4.0的启动报异常到1.4.1支持,个人觉得这个应该是官方在升级的时候,影响到了spring.datasource.type属性的使用。不管怎么样,希望spring boot做的越来越好。


你还可以使用 application.yml 文件

写法更直观哦,类似 Python 的强缩进:

#application.yml
spring:
    datasource:
        name: test
        url: jdbc:mysql://192.168.1.168:3306/test
        username: root
        password:
        # 使用druid数据源
        type: com.alibaba.druid.pool.DruidDataSource
        driver-class-name: com.mysql.jdbc.Driver
        filters: stat
        maxActive: 20
        initialSize: 1
        maxWait: 60000
        minIdle: 1
        timeBetweenEvictionRunsMillis: 60000
        minEvictableIdleTimeMillis: 300000
        validationQuery: select 'x'
        testWhileIdle: true
        testOnBorrow: false
        testOnReturn: false
        poolPreparedStatements: true
        maxOpenPreparedStatements: 20
但,但是,你是不是和我一样,有了一个新疑问:application.properties  文件和 application.yml 文件有什么区别呢?

参考链接:

91.Spring Boot属性spring.datasource.type特别篇

Spring Boot – Cannot determine embedded database driver class for database type NONE



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值