2

I am setting up a Springboot(version 2.0.4) project with maven build to use PostgreSQL database. I want to utilize data-source auto configuration feature of Springboot but it is giving me following errors:

Field dataSource in com.praveen.demo.MyController required a bean of type 'javax.sql.DataSource' that could not be found.
Bean method 'dataSource' not loaded because @ConditionalOnClass did not find required class 'org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType'
Bean method 'dataSource' not loaded because @ConditionalOnClass did not find required classes 'javax.transaction.TransactionManager', 'org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType'

In my pom.xml I've dependencies on postgresql and HikariCP as below:

<dependency>
    <groupId>org.postgresql</groupId>
    <artifactId>postgresql</artifactId>
</dependency>

<dependency>
    <groupId>com.zaxxer</groupId>
    <artifactId>HikariCP</artifactId>
</dependency>

Also I've in my application.properties file

spring.datasource.url=jdbc:postgresql://localhost:5432/mydb?user=myself&password=mypassword

In my Java file having @RestController annotation , I am injecting DataSource as below:

@Autowired
private DataSource dataSource;

I am following below artcile: https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-sql.html I do not want to use JPA. I believe article is not suggesting to use the JPA for auto configuration to work. I am expecting that Spring boot should auto-configure Data source for the application as I've declared the dependencies and provided the database URL. Still I get errors(as mentioned on top) on starting the application.

Edit-1: I am following below article: https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-sql.html

Edit-2: Complete POM:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.praveen</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>demo</name>
<description>Demo project for Spring Boot</description>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.5.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.postgresql</groupId>
        <artifactId>postgresql</artifactId>
    </dependency>


    <dependency>
      <groupId>com.zaxxer</groupId>
        <artifactId>HikariCP</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>
<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>
</project>

Although, I am still confused by the documentation provided at https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-sql.html and was expecting that I should not be required to use JPA for auto-configuration to work if I am explicitly declaring a dependency on HikariCP and Postgresql. Closing it for now.

6
  • did you add <artifactId>spring-boot-starter-data-jpa</artifactId> ? Commented Sep 22, 2018 at 8:32
  • No I didn't add <artifactId>spring-boot-starter-data-jpa</artifactId>. Can I not get the auto configuration without JPA? I was following below article: docs.spring.io/spring-boot/docs/current/reference/html/… Commented Sep 22, 2018 at 8:50
  • The doc says Spring Boot can auto-configure embedded H2, HSQL, and Derby databases. "External" connections can also be auto-configured by using a pooling DataSource. Commented Sep 22, 2018 at 9:03
  • Section '29.1.2 Connection to a Production Database' covers non embedded database as well. and it says Production database connections can also be auto-configured by using a pooling DataSource Commented Sep 22, 2018 at 9:13
  • Exactly. If you use the spring-boot-starter-jdbc or spring-boot-starter-data-jpa “starters”, you automatically get a dependency to HikariCP Commented Sep 22, 2018 at 9:17

1 Answer 1

4

Your pom is fine if you don't want to use JPA.

Define @Bean for Datasource to configure or add property in application.properties spring.datasource.type=com.zaxxer.hikari.HikariDataSource:

@Configuration
public class DbConfig {

    @Bean
    @ConfigurationProperties("spring.datasource")
    public HikariDataSource dataSource() {
        return DataSourceBuilder.create().type(HikariDataSource.class).build();
    }
}

also change your properties to below:

spring.datasource.url=jdbc:postgresql://localhost:5432/mydb
spring.datasource.username-myself
spring.datasource.password= mypassword

Note: JPA configure for you everything so if not using JPA then you need to configure DataSource by telling about Hikari.

As per reference document 29.1.2 Connection to a Production Database

If you use the spring-boot-starter-jdbc or spring-boot-starter-data-jpa “starters”, you automatically get a dependency to HikariCP.

You can bypass that algorithm completely and specify the connection pool to use by setting the spring.datasource.type property. This is especially important if you run your application in a Tomcat container, as tomcat-jdbc is provided by default.

[Tip] Additional connection pools can always be configured manually. If you define your own DataSource bean, auto-configuration does not occur.

Sign up to request clarification or add additional context in comments.

11 Comments

Removing above dependency did not help. I think it is indeed required. See docs.spring.io/spring-boot/docs/current/reference/html/…
and you have JPA dependency, can you please show me your complete pom??
can you please also change your properties as I updated in answer.
Update my question with complete POM
and I do not want to use JPA. I believe docs.spring.io/spring-boot/docs/current/reference/html/… is not asking that JPA is mandatory to use.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.