簡體   English   中英

Spring Boot-創建名稱為'org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration'的bean時出錯

[英]Spring Boot - Error creating bean with name 'org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration'

我已經安裝了Spring Boot應用程序。 現在,我需要向其中添加Spring JDBC Template。 在執行此操作時,我面臨以下異常。

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'XXX': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: org.springframework.jdbc.core.JdbcTemplate com..XXX.jdbcTemplate; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration$JdbcTemplateConfiguration': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private javax.sql.DataSource org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration$JdbcTemplateConfiguration.dataSource; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceAutoConfiguration$NonEmbeddedConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.sql.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 (the profiles "LOCAL" are currently active).
Caused by: 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 (the profiles "LOCAL" are currently active).

下面是代碼。

@Service
public class XXX {

    @Autowired
    JdbcTemplate jdbcTemplate;

    public void testDataSource() {
        List<Map<String, Object>> list = jdbcTemplate.queryForList("select * from C_MASTER");
        System.out.println("list : " + list);
    }

}

Java配置

@Configuration
@ComponentScan
@EnableTransactionManagement
public class DAODataServiceManagerConfiguration {

    @Bean
    public DataSource getDataSource() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName("oracle.jdbc.driver.OracleDriver");
    dataSource.setUrl("jdbc:oracle:thin:@g9u1769.houston.hpecorp.net:1525:ODSDBD");
    dataSource.setUsername("Solid_batch");
    dataSource.setPassword("solid_batch123");

    return dataSource;
    }

}

在Spring Boot查找application.properties時,我也在資源目錄中添加了該文件。 appliation.properties。

spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver
spring.datasource.url=jdbc:oracle:thin:@g9u1769.houston.hpecorp.net:1525:ODSDBD
spring.datasource.username=Solid_batch
spring.datasource.password=solid_batch123

spring.datasource.initialize=true

無法生成該應用程序 如果我做錯了什么,請糾正我。

您在項目classpath中缺少ojdbc jar,請按照以下步驟下載,安裝並將其用作依賴項:

  1. 此處下載ojdbc6.jar

  2. 安裝它,運行命令-

    mvn install:install-file -Dfile={Path/to/your/ojdbc.jar} -DgroupId=com.oracle -DartifactId=ojdbc6 -Dversion=11.2.0 -Dpackaging=jar

對於jar版本,請提取jar文件並檢查MANIFEST.MFImplementation-Version ,例如:

Manifest-Version: 1.0
Ant-Version: Apache Ant 1.6.5
Created-By: 1.5.0_51-b10 (Sun Microsystems Inc.)
Implementation-Vendor: Oracle Corporation
Implementation-Title: JDBC
Implementation-Version: 11.2.0.4.0
Repository-Id: JAVAVM_11.2.0.4.0_LINUX.X64_RELEASE
Specification-Vendor: Sun Microsystems Inc.
Specification-Title: JDBC
Specification-Version: 4.0
Main-Class: oracle.jdbc.OracleDriver
sealed: true

Name: oracle/sql/converter/
Sealed: false

Name: oracle/sql/
Sealed: false

Name: oracle/sql/converter_xcharset/
Sealed: false

Name: oracle/replay/driver/
Sealed: false
  1. 在項目中添加為依賴項,如下所示:

    <dependency> <groupId>com.oracle</groupId> <artifactId>ojdbc6</artifactId> <version>11.2.0</version> </dependency>

Use jdbctemplate by extends JdbcDaoSupport .
By it programmer not concern about the open and close the connection.
Use commons-dbcp2-2.1.1.jar and commons-pool2-2.4.2.jar for use dbcp2  because dbcp2 support Connection pooling.
It's a technique to allow multiple clinets to make use of a cached set of shared and reusable connection objects providing access to a database


public class XXX extends JdbcDaoSupport {

    public void testDataSource() {
        List<Map<String, Object>> list = jdbcTemplate.queryForList("select * from C_MASTER");
        System.out.println("list : " + list);
    }

}

In spring.xml write

    <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">
        <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
        <property name="url" value="jdbc:mysql://127.0.0.1:3306/database_name" />
        <property name="username" value="username" />
        <property name="password" value="password" />
    </bean>

    <bean id="xXX" class="your_package_name.XXX">
        <property name="dataSource" ref="dataSource" />
    </bean>

我已經閱讀了Spring Boot參考文檔。 我知道,如果我們使用的是(H2,HSQL或Derby)數據庫,則不需要application.properties。

如果項目具有Oracle數據庫,則應更新application.properties。 就我而言,我已經更新了屬性文件。

所以我只更新了以下內容,然后它可以正常工作。

pom.xml

<dependency>
            <groupId>com.oracle</groupId>
            <artifactId>ojdbc6</artifactId>
            <version>11.2.0.3.0</version>
        </dependency>

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

配置文件

@Configuration
public class DaoConfig {

    @Bean
    public DataSource getDataSource() {

        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName("oracle.jdbc.OracleDriver");
        dataSource.setUrl("xxx");
        dataSource.setUsername("xxx");
        dataSource.setPassword("xxx");
        return dataSource;
    }

    @Bean
    public JdbcTemplate getJdbcTemplate() {

        return new JdbcTemplate(getDataSource());
    }

我希望這可以幫助某人。 謝謝@ChrisThompson和@Arpit

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM