java - 错误: Field dataSource in required a bean of type 'javax.activation.DataSource' that could not be found

标签 java spring-boot jdbc spring-jdbc

我在 SPringBoot 中的数据源有问题。我想使用 JDBC 并从数据源获取数据,但出现错误: 说明:

com.example.My.MyApplication 中的字段 dataSource 需要类型为“javax.activation.DataSource”的 bean,但无法找到。

行动:

考虑在您的配置中定义“javax.activation.DataSource”类型的 bean。

这是关于MyApplication.java中的DataSourse

下面是代码

我的schema.sql:

CREATE TABLE CUSTOMER(
ID NUMBER(10) NOT NULL,
NAME VARCHAR2(100) NOT NULL,
EMAIL VARCHAR2(100) NOT NULL,
CREATED_DATE DATE NOT NULL,
CONSTRAINT CUSTOMER_PK PRIMARY KEY (ID)
);

和数据.sql

INSERT INTO "CUSTOMER" (ID, NAME, EMAIL, CREATED_DATE) VALUES(1, 
'mkyong','111@yahoo.com', TO_DATE('2017-02-11', 'yyyy-mm-dd'));
INSERT INTO "CUSTOMER" (ID, NAME, EMAIL, CREATED_DATE) VALUES(2, 
'yflow','222@yahoo.com', TO_DATE('2017-02-12', 'yyyy-mm-dd'));
INSERT INTO "CUSTOMER" (ID, NAME, EMAIL, CREATED_DATE) VALUES(3, 
'zilap','333@yahoo.com', TO_DATE('2017-02-13', 'yyyy-mm-dd'));

和我的 Customer.java 包 com.example.My.model;

import java.sql.Date;

public class Customer {

    int id;
    String name;
    String email;
    Date date;

    public Customer(int id, String name, String email, Date date) {
        this.id = id;
        this.name = name;
        this.email = email;
        this.date = date;
    }
}

CustomerRepository.java

import com.example.My.model.Customer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository

public class CustomerRepository {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    // thanks Java 8, look the custom RowMapper
    public List<Customer> findAll() {

        List<Customer> result = jdbcTemplate.query(
                "SELECT id, name, email, created_date FROM customer",
                (rs, rowNum) -> new Customer(rs.getInt("id"),
                        rs.getString("name"), rs.getString("email"), 
rs.getDate("created_date"))
        );

        return result;

    }
}

MyApplication.java

package com.example.My;

import com.example.My.dao.CustomerRepository;
import com.example.My.model.Customer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import javax.activation.DataSource;
import java.util.List;

import static java.lang.System.exit;

@SpringBootApplication

public class MyApplication implements CommandLineRunner {

/**
 *
 */
    @Autowired
    DataSource dataSource;

    @Autowired
    CustomerRepository customerRepository;


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


    @Override
    public void run(String... args) throws Exception {

        System.out.println("DATASOURCE = " + dataSource);

        /// Get dbcp2 datasource settings
        // BasicDataSource newds = (BasicDataSource) dataSource;
        // System.out.println("BasicDataSource = " + newds.getInitialSize());

        System.out.println("Display all customers...");
        List<Customer> list = customerRepository.findAll();
        list.forEach(x -> System.out.println(x));

        System.out.println("Done!");

        exit(0);
    }

}

最佳答案

您在 MyApplication 中导入了错误的 DataSource 类型。您需要导入javax.sql.DataSource,而不是javax.activation.DataSource

关于java - 错误: Field dataSource in required a bean of type 'javax.activation.DataSource' that could not be found,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52786820/

相关文章:

java - JSONObject 无法转换为 JSONArray , jsonObject.getJSONArray(KEY_DATA) 不起作用

java - 我的智能手机上只收到一个数据包

spring-mvc - Spring + Jackson + joda time : how to specify the serialization/deserialization format?

spring - 配置中的默认管理员用户和数据库中的其他用户

java - 如何从 Spring Data 存储库调用 MySQL 函数

java - 为多个源生成唯一 ID

java - 如何在ignite中检查缓存状态

java - JDBC DriverManager 无法访问不同包中的类

java - Java 中的简单 mysql 选择比 MySQL Workbench 中慢

linux - 无法从 Linux 查询 SQL Server 2008 数据库?