【问题标题】:@autowired issue with jdbctemplate in spring boot latest versionSpring Boot 最新版本中 jdbctemplate 的 @autowired 问题
【发布时间】:2019-01-05 17:44:50
【问题描述】:

下面是我的 application.properties 文件

spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver
spring.datasource.url=jdbc:oracle:thin:@abc:1512:dbq
spring.datasource.username=root
spring.datasource.password=root
spring.jpa.hibernate.ddl-auto=update

下面是我的 POM.xml

      <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.3.RELEASE</version>
        <relativePath/>
      </parent>
    <dependencies>


            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency> 

            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-tomcat</artifactId>
                <scope>provided</scope>
            </dependency>

             <dependency>
                <groupId>org.springframework.boot</groupId>
                 <artifactId>spring-boot-starter-jdbc</artifactId>
            </dependency> 

            <dependency>
                <groupId>org.codehaus.jettison</groupId>
                <artifactId>jettison</artifactId>
                <version>1.4.0</version>
            </dependency>

      </dependencies>

我使用 eclipse 创建了新的 maven + Spring boot 和最新版本的项目。但是在尝试自动装配 JdbcTemplate 时出现以下错误。我已经在上面分享了我的属性文件和 pom.xml。

    Error::
    org.springframework.beans.factory.UnsatisfiedDependencyException: 
    Error creating bean with name 'asurintApp': Unsatisfied dependency 
    expressed through field 'jdbcTemplate'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.boot.autoconfigure.jdbc.JdbcTemplateAutoConfiguration$JdbcTemplateConfiguration': Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.zaxxer.hikari.HikariDataSource]: Factory method 'dataSource' threw exception; nested exception is java.lang.Ill`enter code here`egalStateException: Cannot load driver class: oracle.jdbc.driver.OracleDriver

【问题讨论】:

  • 如果你使用spring-boot那么最好使用JPA。
  • 当我添加依赖时"org.springframeworkspring-jdbc4.1.4.RELEASE dependency>" 而不是 "spring-boot-starter-jdbc" 然后考虑在你的配置中定义一个 'org.springframework.jdbc.core.JdbcTemplate' 类型的 bean。
  • Cannot load driver class: oracle.jdbc.driver.OracleDriver -> 你缺少ojdbc 依赖
  • 添加:com.oracleojdbc712.1.0 但得到同样的错误
  • oracleojdbc611.2.0.3 试试添加这个依赖项

标签: java spring spring-boot oracle11g jdbctemplate


【解决方案1】:

您缺少 Oracle 数据库的 jdbc 驱动程序。

不幸的是,由于二进制许可,没有带有 Oracle 驱动程序 JAR 的公共存储库。尝试在this answer 之后将其添加到您的 pom 中。

【讨论】:

  • 谢谢,它对我有用。但是为什么 jdbctemplate 需要外部 jar 呢?
  • jdbctemplate 实际上允许您使用相同的接口访问一系列实际数据库。但是对于每个不同的数据库,您都需要一个新的驱动程序。 oracle 驱动程序位于外部 jar 中,将由 jdbc 使用。不幸的是,这些驱动程序不是开源的,因此由于许可证的原因,有必要跳过一些环节。
【解决方案2】:

我添加这个是为了展示另一种将jar 存储在spring-boot 应用程序中的方式。这样,如果新开发者拉取这个应用程序,他/她将不需要遵循this 的方式。

src 中创建一个目录libsrc/lib/

然后将您的ojdbc-{versio}.jar 放入lib 文件夹,然后将此依赖项添加到gradle

compile files('src/lib/ojdbc8-{version}.jar')

为 maven 添加此依赖项:

<dependency>
groupId ...
artifactId ...
version ...
<scope>system</scope
<systemPath>${project.basedir}/src/main/resources/yourJar.jar</systemPath>
</dependency>

【讨论】:

  • Gradle 相关答案如何帮助使用 Maven 的 OP?
  • 我为使用 gradle 的人添加了这个
  • 添加了对 maven 的依赖
  • 这对 Maven 不起作用,因为 system 范围的依赖项不会包含在创建的工件中。所以它可以在 IDE 中工作(取决于 IDE),但不适用于运行时工件。
猜你喜欢
  • 1970-01-01
  • 2016-02-23
  • 2021-10-14
  • 1970-01-01
  • 1970-01-01
  • 2019-05-19
  • 1970-01-01
  • 2021-11-01
  • 2020-01-05
相关资源
最近更新 更多