Java – Spring how to set datasource in Spring Boot

javajdbcpostgresqlspringspring-boot

I'm just learning Spring and I've been struggling for the past couple of days on how to configure the Spring JdbcTemplate to use my PostgreSQL database. I don't know how to do this. I keep reading the documentation, (such as http://docs.spring.io/spring/docs/current/spring-framework-reference/html/jdbc.html) but it seems like I'm going around in circles.

From the errors thrown it seems that it can't instantiate the RelationalDatabase Class that I wrote as a bean. I'm not sure what it would take to instantiate the class properly.

How do I move from something like the guides (Like https://spring.io/guides/gs/relational-data-access/) which totally work to a more complex solution?

Relational Database Class

@Repository
public class RelationalDatabase 
{

    private JdbcTemplate jdbcTemplate;

    public RelationalDatabase(){}


    public RelationalDatabase(JdbcTemplate jdbcTemplate)
    {
        this.jdbcTemplate = jdbcTemplate;
    }

    @Autowired
    public void setDataSource(javax.sql.DataSource dataSource) {
        this.jdbcTemplate = new JdbcTemplate(dataSource);
    }

    public JdbcTemplate getJdbcTemplate() {
        return jdbcTemplate;
    }


    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

}

\src\main\resources\application-dev.properties

spring.datasource.url=jdbc:postgresql://192.168.56.102:5432/scibase
spring.datasource.type=org.postgresql.ds.PGPoolingDataSource 
spring.datasource.username=lemon
spring.datasource.password=XXXXXX
spring.datasource.platform=postgres
spring.datasource.max-active=100
spring.datasource.name=lime
spring.database.driverClassName=org.postgresql.Drive

Stack Trace (Summary)

org.springframework.beans.factory.BeanCreationException: Error
creating bean with name 'relationalDatabase': Injection of autowired
dependencies failed; nested exception is
org.springframework.beans.factory.BeanCreationException: Could not
autowire method: public void
com.scientifi.papers.db.relational.RelationalDatabase.setDataSource(javax.sql.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 (the profiles "dev "
are currently active).

Thanks!

Best Answer

Did you forget the "r" in Driver? (Cannot determine embedded database driver class for database type NONE)

spring.database.driverClassName=org.postgresql.Driver

instead of

spring.database.driverClassName=org.postgresql.Drive