Skip to content

Instantly share code, notes, and snippets.

@exaucae
Created August 31, 2021 14:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save exaucae/6653638850ae80ecc9859225f2a9b502 to your computer and use it in GitHub Desktop.
Save exaucae/6653638850ae80ecc9859225f2a9b502 to your computer and use it in GitHub Desktop.
postgres springboot reactive config for r2dbc
package your.pkg;
import io.r2dbc.postgresql.PostgresqlConnectionConfiguration;
import io.r2dbc.postgresql.PostgresqlConnectionFactory;
import io.r2dbc.spi.ConnectionFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.data.r2dbc.config.AbstractR2dbcConfiguration;
import org.springframework.data.r2dbc.core.DefaultReactiveDataAccessStrategy;
import org.springframework.data.r2dbc.dialect.PostgresDialect;
import org.springframework.data.r2dbc.repository.config.EnableR2dbcRepositories;
import org.springframework.data.r2dbc.repository.support.R2dbcRepositoryFactory;
import org.springframework.r2dbc.core.DatabaseClient;
@PropertySource({"classpath:postgres-persistence.properties"})
@Configuration
@EnableR2dbcRepositories(basePackages = {"your.pkg.persistence.postgres.repository"})
public class DatabaseConfiguration extends AbstractR2dbcConfiguration {
@Bean
@Override
public PostgresqlConnectionFactory connectionFactory() {
return new PostgresqlConnectionFactory(PostgresqlConnectionConfiguration
.builder()
.host("localhost")
.database("you-db")
.username("postgres")
.password("password")
.port(5423)
.build());
}
@Bean
public DatabaseClient databaseClient(ConnectionFactory connectionFactory) {
return DatabaseClient.builder().connectionFactory(connectionFactory).build();
}
@Bean
R2dbcRepositoryFactory repositoryFactory(DatabaseClient client) {
return new R2dbcRepositoryFactory(client, new DefaultReactiveDataAccessStrategy(new PostgresDialect()));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment