自动装配数据源db项目bean

时间:2017-07-22 15:12:52

标签: java spring tomcat javabeans autowired

我为spring启动应用程序设置了一个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);
}

2 个答案:

答案 0 :(得分:0)

您正在属性文件中启用liquibase

liquibase.enabled=true

你有意激活它吗?因为我没有看到liquibase依赖项和更改日志文件。

答案 1 :(得分:0)

尝试评论或删除(如果不使用liquibase)这些行

liquibase.change日志=类路径:/db/changelog-master.xml

liquibase.enabled =真

我猜你注意使用liquibase,当你输入liquibase属性时,spring de试图将liquibase bean设置为instanciate。因此,如果您不需要它,请不要使用它。