Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: Field dataSource in required a bean of type 'javax.activation.DataSource' that could not be found

I have problem with DataSource in SPringBoot. I want use JDBC and get data from datasource but I get error: Description:

Field dataSource in com.example.My.MyApplication required a bean of type 'javax.activation.DataSource' that could not be found.

Action:

Consider defining a bean of type 'javax.activation.DataSource' in your configuration.

It is about DataSourse in MyApplication.java

Below is the code

my 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)
);

and data.sql

INSERT INTO "CUSTOMER" (ID, NAME, EMAIL, CREATED_DATE) VALUES(1, 
'mkyong','[email protected]', TO_DATE('2017-02-11', 'yyyy-mm-dd'));
INSERT INTO "CUSTOMER" (ID, NAME, EMAIL, CREATED_DATE) VALUES(2, 
'yflow','[email protected]', TO_DATE('2017-02-12', 'yyyy-mm-dd'));
INSERT INTO "CUSTOMER" (ID, NAME, EMAIL, CREATED_DATE) VALUES(3, 
'zilap','[email protected]', TO_DATE('2017-02-13', 'yyyy-mm-dd'));

and my Customer.java package 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);
    }

}
like image 256
Vix Avatar asked Nov 02 '25 13:11

Vix


1 Answers

You imported the wrong DataSource type in MyApplication. You need to import javax.sql.DataSource, not javax.activation.DataSource.

like image 66
Mark Rotteveel Avatar answered Nov 04 '25 04:11

Mark Rotteveel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!