簡體   English   中英

自動裝配數據源db項目bean

[英]Autowiring data source db project beans

我正在為Spring Boot應用程序設置bean,但出現以下錯誤,請您幫我嗎? 我不確定MyDataSource類是否正確以及application.properties中是否缺少某些內容

提前謝謝了。

2017-07-22 13:50:33.752  WARN 6872 --- [ost-startStop-1] 
ationConfigEmbeddedWebApplicationContext : Exception encountered during 
context initialization - cancelling refresh attempt: 
org.springframework.beans.factory.BeanCreationException: Error creating bean 
with name 
'org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration$LiquibaseConfiguration': 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.liquibase.LiquibaseAutoConfiguration$LiquibaseConfiguration.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 (no profiles are currently active).
2017-07-22 13:50:33.780 ERROR 6872 --- [ost-startStop-1] o.s.boot.SpringApplication               : Application startup failed

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration$LiquibaseConfiguration': 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.liquibase.LiquibaseAutoConfiguration$LiquibaseConfiguration.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 (no profiles are currently active).

MyDataSource類:

public class MyDataSource {

private static final String driverClassName = "org.postgresql.Driver";
private static final String url = "jdbc:postgresql://mydbinstance3.ckvnevtnxxbs.us-west-2.rds.amazonaws.com:5432/postgres";
private static final String dbUsername = "OnFocus";
private static final String dbPassword = "G0nk0p0rr0";

private static DataSource dataSource;

public static void main(String[] args) throws Exception {

    dataSource = getDataSource();

    // JdbcTemplate template = new JdbcTemplate(dataSource); // constructor

    JdbcTemplate template = new JdbcTemplate();
    template.setDataSource(dataSource);

    System.out.println(dataSource.getClass());

}

public static DriverManagerDataSource getDataSource() {

    DriverManagerDataSource dataSource = new DriverManagerDataSource();

    dataSource.setDriverClassName(driverClassName);

    dataSource.setUrl(url);

    dataSource.setUsername(dbUsername);

    dataSource.setPassword(dbPassword);

    return dataSource;
}

}

的build.gradle:

buildscript {
repositories {
    mavenCentral()
}
dependencies {
    classpath("org.springframework.boot:spring-boot-gradle-plugin:1.3.3.RELEASE")
    classpath 'io.spring.gradle:dependency-management-plugin:0.6.1.RELEASE'
}
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'spring-boot'
apply plugin: 'war'

version = '1.0-SNAPSHOT'
buildDir = 'target'

jar {
baseName = rootProject.name
}

war {
baseName = 'myapp'
version = '1.0.0'
}
repositories {
jcenter()
maven { url "http://repo.spring.io/libs-snapshot" }
}

sourceCompatibility = 1.8
targetCompatibility = 1.8

dependencies {
compile("org.springframework.boot:spring-boot-starter-data-jpa:1.2.5.RELEASE")
compile("org.springframework.boot:spring-boot-starter-web")
compile("org.springframework.boot:spring-boot-starter-actuator")
compile("org.postgresql:postgresql:9.4-1201-jdbc41")
compile("org.springframework:spring-jdbc:4.2.4.RELEASE")
compile("com.fasterxml.jackson.core:jackson-databind")
compile("org.liquibase:liquibase-core:3.3.3")
compile("org.springframework.boot:spring-boot-starter-data-mongodb")
compile 'org.springframework.boot:spring-boot-starter-thymeleaf'
compile("org.springframework.boot:spring-boot-starter-web")

providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat'

testCompile("junit:junit")
}

bootRepackage {
mainClass = 'theproject.src.main.java.tutorialDatabaseServer.Application'
}

Application.properties:

server.port=8081
spring.datasource.database-platform=org.hibernate.dialect.PostgreSQLDialect
spring.datasource.driverClassName=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://mydbinstance3.ckvnevtnxxbs.us-west-
2.rds.amazonaws.com:5432/postgres
spring.datasource.username=OnFocus
spring.datasource.password=G0nk0p0rr0
liquibase.change-log=classpath:/db/changelog-master.xml
liquibase.enabled=true

應用類別:

@SpringBootApplication
public class Application extends SpringBootServletInitializer {

@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
    return application.sources(Application.class);
}

public static void main(String[] args) throws Exception {
    SpringApplication.run(Application.class, args);
}

您正在屬性文件中啟用liquibase的使用

liquibase.enabled=true

您是否有意激活它? 因為我沒有看到liquibase依賴性和更改日志文件。

嘗試注釋或刪除(如果未使用liquibase)這些行

liquibase.change日志=類路徑:/db/changelog-master.xml

liquibase.enabled =真

我想您注意到使用liquibase時,當您放置了liquibase屬性時,spring de試圖實例化liquibase bean。 所以如果你不需要它,不要使用它。

暫無
暫無

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

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