首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Spring引导(使用Scala)无法实例化DataSource [未找到受支持的DataSource类型]

问Spring引导(使用Scala)无法实例化DataSource [未找到受支持的DataSource类型]
EN

Stack Overflow用户
提问于 2019-02-16 02:26:51
回答 1查看 1.5K关注 0票数 3

当我运行独立的web应用程序时,spring无法实例化数据源bean。请注意,我不想在这个项目中使用JPA或hibernate。在这一点上我不知道为什么。我的最佳猜测是依赖关系或语法问题,但我一直未能找到解决问题的方法。

代码语言:javascript
复制
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'gameoo' defined in class path resource [com/hf/database/Datasource.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.sql.DataSource]: Factory method 'get' threw exception; nested exception is java.lang.IllegalStateException: No supported DataSource type found

如果有人有洞察力或者能帮我解决这个问题,那就太好了。

下面是我的"spring.properties“文件

代码语言:javascript
复制
# Datasource properties
spring.datasource.driver-class-name = com.mysql.jdbc.Driver
spring.datasource.url = jdbc:mysql://localhost:3306/gameoo?autoReconnect=true&useSSL=true
spring.datasource.username = user
spring.datasource.password = pass
spring.datasource.initialSize = 1
spring.datasource.maxActive = 5

# HikariCP settings (spring.datasource.hikari.*)
spring.datasource.hikari.connection-timeout = 60000 #60 sec
spring.datasource.hikari.maximum-pool-size = 5      # max 5

引起问题的数据源类:

代码语言:javascript
复制
import javax.sql.DataSource
import org.springframework.boot.context.properties.ConfigurationProperties
import org.springframework.boot.jdbc.DataSourceBuilder
import org.springframework.context.annotation.{Bean, Configuration, Primary, PropertySource}

@Configuration
@PropertySource(Array("classpath:spring.properties"))
class Datasource {
    @Primary
    @Bean(name = Array("gameoo"))
    @ConfigurationProperties(prefix = "spring.datasource")
    def get: DataSource = {
        DataSourceBuilder.create().build()
    }
}

我的主要应用程序类如下

代码语言:javascript
复制
import org.springframework.boot.SpringApplication
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.context.annotation.ComponentScan


@SpringBootApplication
@ComponentScan(Array[String]("com.hf"))
class AppRunner


object HexFrontier
{
    def main(args: Array[String]): Unit = {
        System.setProperty("spring.config.name", "spring")
        SpringApplication.run(classOf[AppRunner])
    }
}

最后,下面是我的gradle依赖文件

代码语言:javascript
复制
plugins {
    id 'java'
    id 'scala'
}

group 'com.hf'
version '1.0'

repositories {
    mavenCentral()
}

sourceCompatibility = 1.8

sourceSets {
        main.java.srcDirs = ['src/main/hf']
        test.java.srcDirs = ['src/test/hf']
        main.resources.srcDirs = ['src/main/resources']
}

configurations.all {
    resolutionStrategy.cacheChangingModulesFor 1, 'minutes'
}

compileJava {
    options.compilerArgs += ["-proc:none"]
}

dependencies {
    // Scala
    compile('org.scala-lang:scala-library:2.12.8')

    // Spring
    compile group: 'org.springframework.boot', name: 'spring-boot', version: '2.1.2.RELEASE'
    compile group: 'org.springframework.boot', name: 'spring-boot-autoconfigure', version: '2.1.2.RELEASE'
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-web', version: '2.1.2.RELEASE'
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-actuator', version: '2.1.2.RELEASE'
    compile group: 'org.springframework', name: 'spring-core', version: '5.1.5.RELEASE'
    compile group: 'org.springframework', name: 'spring-beans', version: '5.1.5.RELEASE'
    compile group: 'org.springframework', name: 'spring-context', version: '5.1.5.RELEASE'
    compile group: 'org.springframework', name: 'spring-web', version: '5.1.5.RELEASE'
    compile group: 'org.springframework', name: 'spring-webmvc', version: '5.1.5.RELEASE'
    compile group: 'org.springframework', name: 'spring-aop', version: '5.1.5.RELEASE'
    compile group: 'org.springframework', name: 'spring-jdbc', version: '5.1.5.RELEASE'
//    compile group: 'org.springframework.data', name: 'spring-data-commons', version: '2.1.4.RELEASE'

    // servlets
    compile group: 'javax.servlet', name: 'javax.servlet-api', version: '4.0.1'
    compile group: 'javax.persistence', name: 'javax.persistence-api', version: '2.2'
    compile group: 'javax.transaction', name: 'jta', version: '1.1'
    compile group: 'javax.xml.bind', name: 'jaxb-api', version: '2.1'
    compile group: 'javax.validation', name: 'validation-api', version: '1.0.0.GA'
    compile group: 'javax.servlet.jsp', name: 'javax.servlet.jsp-api', version: '2.3.3'
    compile group: 'javax.annotation', name: 'javax.annotation-api', version: '1.3.2'

    // mysql jdbc
    compile group: 'mysql', name: 'mysql-connector-java', version: '8.0.15'

    // Logging
    compile group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.11.0'
    compile group: 'org.apache.logging.log4j', name: 'log4j-api', version: '2.11.0'

    // JSON / Yaml / Config support
    compile group: 'com.fasterxml.jackson.core', name: 'jackson-core', version: '2.9.8'
    compile group: 'com.fasterxml.jackson.module', name: 'jackson-module-scala_2.12', version: '2.9.8'
    compile group: 'com.fasterxml.jackson.dataformat', name: 'jackson-dataformat-yaml', version: '2.9.8'
    compile group: 'com.typesafe', name: 'config', version: '1.3.3'

    // Testing
    testCompile group: 'org.hamcrest', name: 'hamcrest-all', version: '1.3'
    testCompile group: 'junit', name: 'junit', version: '4.12'
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-02-16 07:25:20

未能实例化javax.sql.DataSource:工厂方法'get‘引发的异常;嵌套异常为java.lang.IllegalStateException:未找到受支持的DataSource类型

如果您不通过在DataSource之前调用type()来设置build()类名,DataSourceBuilder将扫描类路径以按顺序查找以下DataSource类。如果找不到任何错误,则会发生此错误。

  1. com.zaxxer.hikari.HikariDataSource (HikariCP)
  2. org.apache.tomcat.jdbc.pool.DataSource (Tomcat连接池)
  3. org.apache.commons.dbcp2.BasicDataSource (公域DBCP2)

因此,您必须将HikariCP包含在build.gradle中。或者只需使用spring-boot-starter-jdbc,它将自动获得HikariCP和spring-jdbc,并为您定义DataSource bean。

因此,我建议修改build.gradle如下:

代码语言:javascript
复制
compile group: 'org.springframework.boot', name: 'spring-boot-starter-jdbc', version: '2.1.2.RELEASE'

//No need as spring-boot-starter-jdbc automatically get this
//compile group: 'org.springframework', name: 'spring-jdbc', version: '5.1.5.RELEASE'  
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54719293

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档