您的位置:首页 > 编程语言 > Java开发

spring boot下非web应用,mysql数据源报错问题

2017-10-26 16:21 375 查看
在非web应用下,spring boot启动时提示如下错误:

org.springframework.context.annotation.AnnotationConfigApplicationContext - 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).


spring boot启动时默认会自动配置数据源为org.apache.tomcat.jdbc.pool.DataSource。即使在配置文件或代码中指定了其他的数据源(如druid)。

解决方法是:在@SpringBootApplication注解中取消自动配置,如下:

@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
public class ThisApplication
{
public static void main( String[] args )
{
SpringApplication.run(ThisApplication.class);
}
}


另外,pom.xml中加入依赖:

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>


有时会出现ClassNotFoundException: com.mysql.jdbc.Driver。并且在IDEA Project Structure ->Modules ->Dependencies中有Maven:mysql:mysql-connector-java:unknown项。

解决在pom.xml指定mysql-connector-java的版本即可
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  spring spring-boot