Untitled

mail@pastecode.io avatar
unknown
plain_text
5 months ago
2.2 kB
3
Indexable
package pl.santander.bancassurance.comparison.policy.domain;

import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.jdbc.core.JdbcTemplate;

@Configuration
@EntityScan
@EnableJpaRepositories
@EnableJpaAuditing
class DomainConfiguration {

    @Bean("hikariConfig")
    @ConfigurationProperties(prefix = "spring.datasource.hikari")
    public HikariConfig hikariConfig() {
        return new HikariConfig();
    }

    @Bean("dataSourceProperties")
    @Primary
    @SuppressWarnings("ConfigurationProperties")
    @ConfigurationProperties(prefix = "spring.datasource")
    public DataSourceProperties dataSourceProperties() {
        return new DataSourceProperties();
    }

    @Bean("dataSource")
    @Primary
    @SuppressWarnings("ConfigurationProperties")
    @ConfigurationProperties(prefix = "spring.datasource")
    public HikariDataSource dataSource() {
        var source = dataSourceProperties()
                .initializeDataSourceBuilder()
                .type(HikariDataSource.class)
                .build();
        var hikariConfig = hikariConfig();
        source.setConnectionTimeout(hikariConfig.getConnectionTimeout());
        source.setIdleTimeout(hikariConfig.getIdleTimeout());
        source.setMaxLifetime(hikariConfig.getMaxLifetime());
        source.setMaximumPoolSize(hikariConfig.getMaximumPoolSize());
        source.setMinimumIdle(hikariConfig.getMinimumIdle());
        return source;
    }

    @Bean("jdbcTemplate")
    @Primary
    public JdbcTemplate jdbcTemplate() {
        return new JdbcTemplate(dataSource());
    }
}
Leave a Comment