Spring Boot - 创建名为'org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration'的bean时出错

时间:2016-08-14 15:22:41

标签: java spring spring-boot spring-jdbc spring-java-config

我有弹簧启动应用程序设置。现在我需要添加Spring JDBC Template。在这样做时,我面临着异常。

    func uploadImages(completion: (result: [String]) -> Void){
    let storageRef = storage.referenceForURL("gs://project-xxxxxxxxx.appspot.com/Uploads/\(ref.childByAutoId())")
    var imageUrl = [String]()
    var imgNum = 0



    for i in 0...imageData.count-1 {
        let imagesRef = storageRef.child("\(FIRAuth.auth()?.currentUser?.uid) \(imgNum)")
        imgNum+=1

        let uploadTask =  imagesRef.putData(imageData[i], metadata: nil) { metadata, error in
            if (error != nil) {
                print("error")
                imageUrl = [String]()
                completion(result: imageUrl)
            } else {

                print("uploading")
                // Metadata contains file metadata such as size, content-type, and download URL.
                let downloadURL = metadata!.downloadURL()?.absoluteString
                print(downloadURL)
                imageUrl.append(downloadURL!)
                if i == imageUrl.count-1{ //end of the loop
                    print("completionUpload")
                    completion(result: imageUrl)

                }
            }
        }}

以下是代码。

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).

Java Config

@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);
    }

}

当spring boot查找application.properties时,我也在resources目录中添加了它。 appliation.properties。

@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;
    }

}

无法构建应用程序。如果我做错了,请纠正我。

3 个答案:

答案 0 :(得分:1)

您的项目ojdbc中缺少classpath jar,请按照以下步骤下载,安装并将其用作依赖项:

  1. here下载ojdbc6.jar

  2. 安装它,运行命令 -

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

  3. 对于jar版本,提取jar文件并检查Implementation-Version中的MANIFEST.MF,例如:

    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>

答案 1 :(得分:0)

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>

答案 2 :(得分:0)

我已经通过了春季启动参考文档。我开始知道如果我们使用(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