使用[name]通过[field]

时间:2017-07-17 13:16:02

标签: java spring spring-mvc spring-java-config

我在运行时使用Spring MVC Web应用程序获得以下不满意的依赖性错误:

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'taskConfig': Unsatisfied dependency expressed through field 'dataSource'; 
nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in com.myapps.todolist.main.DataSourceConfig: Bean instantiation via factory method failed;

TaskConfig是一个JavaConfig类,它依赖于dataSource(类型为DriverManagerDataSource),它在单独的JavaConfig类DataSourceConfig中配置。

TaskConfig.java

package com.myapps.todolist.main;

@Configuration
public class TaskConfig {

    @Autowired
    private DataSource dataSource;

    @Bean
    public TaskEntityDao taskEntityDao() 
    {
        TaskEntityDao taskEntityDao = new TaskEntityDaoImpl();
        taskEntityDao.setDataSource(this.dataSource);
        return taskEntityDao;
    }
}

DataSourceConfig.java

package com.myapps.todolist.main;

@Configuration
public class DataSourceConfig {

    @Bean
    public DataSource dataSource()
    {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql://localhost:3306/MyTable");
        dataSource.setUsername("username");
        dataSource.setPassword("password");

        return dataSource;
    }
}

为什么TaskConfig无法找到DataSource对象?

我的Spring配置包括以下内容,以扫描这些类所在的包中的JavaConfig:

<context:component-scan base-package="com.myapps.todolist.main" />

更新:更多的堆栈跟踪:

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in com.myapps.todolist.main.DataSourceConfig: Bean instantiation via factory method failed;
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.sql.DataSource]: Factory method 'dataSource' threw exception; nested exception is java.lang.NoClassDefFoundError: org/springframework/jdbc/datasource/DriverManagerDataSource
Caused by: java.lang.NoClassDefFoundError: org/springframework/jdbc/datasource/DriverManagerDataSource
Caused by: java.lang.ClassNotFoundException: org.springframework.jdbc.datasource.DriverManagerDataSource

0 个答案:

没有答案
相关问题