春季启动的最新版本中jdbctemplate的@autowired问题

时间:2018-07-29 05:52:41

标签: java spring spring-boot oracle11g jdbctemplate

下面是我的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引导。但是当尝试自动连接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

2 个答案:

答案 0 :(得分:2)

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

不幸的是,由于二进制许可证,Oracle Driver JAR没有公共存储库。尝试将其添加到this answer之后的pom中。

答案 1 :(得分:0)

我添加此代码是为了展示在jar应用中存储spring-boot的另一种方法。这样,如果新开发人员拉出该应用程序,他/她将无需遵循this的方式。

lib内创建目录srcsrc/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>