Missing DatabaseClient in Postgres spring boot R2dbc application
Asked Answered
M

3

6

I have the following error coming up:

Exception: Error creating bean with name 'inventoryService' defined in URL [jar:file:/app.jar!/BOOT-INF/classes!/com/epi/services/inventory/items/InventoryService.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'itemRepository': Cannot resolve reference to bean 'databaseClient' while setting bean property 'databaseClient'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'databaseClient' available


2019-06-18 18:38:41,409 INFO  [main] org.apache.juli.logging.DirectJDKLog: Stopping service [Tomcat]


WARNING: An illegal reflective access operation has occurred


WARNING: Illegal reflective access by org.apache.catalina.loader.WebappClassLoaderBase (jar:file:/app.jar!/BOOT-INF/lib/tomcat-embed-core-8.5.29.jar!/) to field java.lang.Thread.threadLocals


WARNING: Please consider reporting this to the maintainers of org.apache.catalina.loader.WebappClassLoaderBase


WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations


WARNING: All illegal access operations will be denied in a future release


2019-06-18 18:38:45,424 INFO  [main] org.springframework.boot.autoconfigure.logging.ConditionEvaluationReportLoggingListener: 




Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.


2019-06-18 18:38:50,695 ERROR [main] org.springframework.boot.diagnostics.LoggingFailureAnalysisReporter: 




***************************


APPLICATION FAILED TO START


***************************




Description:




Parameter 0 of constructor in com.epi.services.inventory.items.InventoryService required a bean named 'databaseClient' that could not be found.






Action:




Consider defining a bean named 'databaseClient' in your configuration.

My application has the following classes and dependencies:

Inside main module:

  <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>io.projectreactor</groupId>
            <artifactId>reactor-core</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-r2dbc</artifactId>
        </dependency>
        <dependency>
            <groupId>io.r2dbc</groupId>
            <artifactId>r2dbc-postgresql</artifactId>
        </dependency>
        <dependency>
            <groupId>javax.xml.bind</groupId>
            <artifactId>jaxb-api</artifactId>
        </dependency>
        <dependency>
            <groupId>com.sun.xml.bind</groupId>
            <artifactId>jaxb-core</artifactId>
        </dependency>
        <dependency>
            <groupId>com.sun.xml.bind</groupId>
            <artifactId>jaxb-impl</artifactId>
        </dependency>
        <dependency>
            <groupId>myGroupId</groupId>
            <artifactId>myModule.dblib</artifactId>
            <version>${project.version}</version>
        </dependency>
       <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-webflux</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectreactor</groupId>
            <artifactId>reactor-spring</artifactId>
        </dependency>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.hibernate.javax.persistence</groupId>
            <artifactId>hibernate-jpa-2.1-api</artifactId>
        </dependency>
    </dependencies>

App:

@SpringBootApplication(scanBasePackages = "com.pack")
@EntityScan("com.pack")
@EnableR2dbcRepositories
@Import(DatabaseConfiguration.class)
public class InventoryApplication {

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

}

Service:

@Service
@RequiredArgsConstructor
public class InventoryService {

    private final ItemRepository itemRepository;

    public Flux<ItemPojo> getAllItems() {
        return itemRepository.findAllItems()
                             .map(Item::toPojo);
    }
}

Repo:

    @Repository
public interface ItemRepository extends ReactiveCrudRepository<Item, Long> {

    Flux<List<Item>> findByName(String name);

    @Query("select i from Item i")
    Flux<Item> findAllItems();

}

Entity:

@Data
@Table(name = "items")
public class Item implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    private String name;

    public static ItemPojo toPojo(final Item items) {
        return new ItemPojo(items.id, items.name);
    }
}

myModule.dblib:

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>io.projectreactor</groupId>
            <artifactId>reactor-core</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-r2dbc</artifactId>
        </dependency>
        <dependency>
            <groupId>io.r2dbc</groupId>
            <artifactId>r2dbc-postgresql</artifactId>
        </dependency>
        <dependency>
            <groupId>org.javassist</groupId>
            <artifactId>javassist</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-r2dbc</artifactId>
        </dependency>
        <dependency>
            <groupId>io.r2dbc</groupId>
            <artifactId>r2dbc-postgresql</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-webflux</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectreactor</groupId>
            <artifactId>reactor-spring</artifactId>
        </dependency>
    </dependencies>

Database config:

@Configuration
@EnableR2dbcRepositories
public class DatabaseConfiguration {

    @Value("${spring.data.postgres.host}") private String host;
    @Value("${spring.data.postgres.port}") private int port;
    @Value("${spring.data.postgres.database}") private String database;
    @Value("${spring.data.postgres.username}") private String username;
    @Value("${spring.data.postgres.password}") private String password;

    @Bean
    public PostgresqlConnectionFactory connectionFactory() {
            return new PostgresqlConnectionFactory(PostgresqlConnectionConfiguration.builder()
                                                    .host(host)
                                                    .port(port)
                                                    .database(database)
                                                    .username(username)
                                                    .password(password)
                                                    .build());
        }
}

What am I missing?

Metamorphic answered 18/6, 2019 at 18:49 Comment(2)
how does your inventoryservice look like?Swink
It's in the question already. Adjusting formattingMetamorphic
T
10

im going to shamelessly plug my own article about how to get started with R2DBC postgres and spring boot.

R2DBC getting started

I think your problem lies in how you initiate your database connectionFactory. According to the documentation you need to extend and override the AbstractR2dbcConfiguration#connectionFactory

@Configuration
@EnableR2dbcRepositories
public class PostgresConfig extends AbstractR2dbcConfiguration {

    @Override
    @Bean
    public ConnectionFactory connectionFactory() {
        return new PostgresqlConnectionFactory(
                PostgresqlConnectionConfiguration.builder()
                .host("localhost")
                .port(5432)
                .username("postgres")
                .password("mysecretpassword")
                .database("myDatabase")
                .build());
    }
}

Official documentation

This approach lets you use the standard io.r2dbc.spi.ConnectionFactory instance, with the container using Spring’s AbstractR2dbcConfiguration.

As compared to registering a ConnectionFactory instance directly, the configuration support has the added advantage of also providing the container with an ExceptionTranslator implementation that translates R2DBC exceptions to exceptions in Spring’s portable DataAccessException hierarchy for data access classes annotated with the @Repository annotation.

This hierarchy and the use of @Repository is described in Spring’s DAO support features.

AbstractR2dbcConfiguration registers also DatabaseClient that is required for database interaction and for Repository implementation.

Official R2DBC documentation

Tressietressure answered 18/6, 2019 at 20:29 Comment(0)
M
1

Turns out I needed to have my Database configuration class extend AbstractR2dbcConfiguration which includes the DatabaseClient bean.

Metamorphic answered 18/6, 2019 at 20:48 Comment(0)
I
1

From a TDD approach, I worked from trying to get the following TestNG method to work:

@Test
@Slf4j
@SpringBootTest
class PostgresSanityTesting extends AbstractTestNGSpringContextTests{
    @Autowired
    DatabaseClient databaseClient

    void sanityCheck() {
        assert databaseClient

    }

But to get there, there's some work...

First, I'm not seeing any effective code unless I forced my gradle subproject to build with

ext['spring.version'] = '5.2.0.M2'

which in fact is called out in the R2Dbc documentation. And half of the other dependencies are coming in from spring experimental and milestones. So "buyer beware"... (e.g. I'm still not seeing R2dbc correctly handle repository save(), and a series of other problems.)

As to the instant coding problem:

Given the correct set up, the database client is a @Bean configured as part of the AbstractR2dbcConfiguration class, and then that TestNg test will pass.

But I found that I needed to still create a @Bean, and I inject the connection factory parameters via @ConfigurationProperties. I separate those concerns in two files, as placing the @Bean in with the subclass of AbstractR2dbcConfiguration just didn't work for me.

Here's the code I'm using, expressed in groovy.

import io.r2dbc.spi.ConnectionFactory
import org.springframework.context.annotation.Configuration
import org.springframework.context.annotation.Profile
import org.springframework.data.r2dbc.config.AbstractR2dbcConfiguration
import org.springframework.data.r2dbc.repository.config.EnableR2dbcRepositories

@Configuration
class R2DbcConfiguration extends AbstractR2dbcConfiguration {

    private final ConnectionFactory connectionFactory

    R2DbcConfiguration( ConnectionFactory connectionFactory ) {
        this.connectionFactory = connectionFactory
    }
    @Override
    ConnectionFactory connectionFactory() {
        this.connectionFactory
    }
}

and

import io.r2dbc.postgresql.PostgresqlConnectionConfiguration
import io.r2dbc.postgresql.PostgresqlConnectionFactory
import org.springframework.boot.context.properties.ConfigurationProperties
import org.springframework.boot.context.properties.EnableConfigurationProperties
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.context.annotation.Profile

@Configuration
@EnableConfigurationProperties
@ConfigurationProperties(prefix = "spring.datasource")
class PostgresConnectionConfig{
    String database
    String username
    String password
    String host
    String port

    @Bean
    PostgresqlConnectionFactory connectionFactory() {
        final def dbPort = port as Integer
        PostgresqlConnectionConfiguration config = PostgresqlConnectionConfiguration.builder() //
                .host(host)
                .port(dbPort)
                .database(database)
                .username(username)
                .password(password).build()
        PostgresqlConnectionFactory candidate = new PostgresqlConnectionFactory(config)
        candidate
    }
}

and part of my application.yml

spring:
  datasource:
    url: jdbc:postgresql://127.0.0.1:5432/postgres
    driver-class-name: org.postgresql.Driver
    driverClassName: org.postgresql.Driver
    database: postgres
    host: localhost
    port: 5432
    password: pokey0
    username: postgres

and excerpts from my subproject's build.gradle

repositories {
    maven { url "https://repo.spring.io/libs-milestone" }
}

dependencies {
    implementation group: 'org.springframework.data', name: 'spring-data-releasetrain', version: 'Lovelace-RELEASE', ext: 'pom'
    implementation group: 'org.springframework.data', name: 'spring-data-r2dbc', version: '1.0.0.M2'
    implementation group: 'io.r2dbc', name: 'r2dbc-postgresql', version: '1.0.0.M7'
    implementation('org.springframework.boot:spring-boot-starter-webflux')
    implementation('io.projectreactor:reactor-test')
    implementation group: 'org.postgresql', name: 'postgresql', version: '42.2.5'
        implementation('org.springframework.boot:spring-boot-starter-actuator')
        testImplementation('org.springframework.boot:spring-boot-starter-test')
        implementation group: 'javax.persistence', name: 'javax.persistence-api', version: '2.2'
}
Invitatory answered 4/7, 2019 at 17:53 Comment(1)
i was just missing the : implementation group: 'io.r2dbc', name: 'r2dbc-postgresql', version: '0.8.5.RELEASE' TDD :))Provincetown

© 2022 - 2024 — McMap. All rights reserved.