使用Guice注入JOOQ配置

时间:2016-04-28 06:17:18

标签: guice jooq

我们在Dropwizard项目中广泛使用Jooq,想知道是否有办法使用Guice将配置注入我们的服务类。

Guice设置的更新

public class SystemModule extends AbstractModule {

private static final Logger LOGGER =
        LoggerFactory.getLogger(SystemModule.class);
private static DataSource DATA_SOURCE;

static {
    try {
        HikariConfig config = new HikariConfig();
        config.setJdbcUrl("jdbc:mariadb://" + System.getenv("DB_HOST") + ":" + System.getenv("DB_PORT") + "/sminq");
        config.setUsername(System.getenv("DB_USER"));
        config.setPassword(System.getenv("DB_PASSWORD"));
        config.addDataSourceProperty("cachePrepStmts", "true");
        config.addDataSourceProperty("prepStmtCacheSize", "250");
        config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");
        config.setMaximumPoolSize(20);
        config.setAutoCommit(false);
        DATA_SOURCE = new HikariDataSource(config);
    } catch (Exception e) {
        LOGGER.info("Datasource exception"+ ExceptionUtils.getStackTrace(e));
    }
}

@Provides
@Singleton
public Configuration providesFactory() {
    Configuration configuration = new DefaultConfiguration()
            .set(new SpringConnectionProvider(DATA_SOURCE))
            .set(SQLDialect.MARIADB)
            .set(new DefaultExecuteListenerProvider(new DatabaseLoggerService(DATA_SOURCE)));
    return configuration;
}

@Provides
@Singleton
public DataSourceTransactionManager provideDataSourceTransactionManager() {
    return new DataSourceTransactionManager(new TransactionAwareDataSourceProxy(DATA_SOURCE));
}

@Provides
@Singleton
private DataSource provideDataSource() {
    return FiberDataSource.wrap(DATA_SOURCE);
}

@Override
protected void configure() {

    TransactionalMethodInterceptor interceptor = new TransactionalMethodInterceptor();
    requestInjection(interceptor);
    bindInterceptor(annotatedWith(Transactional.class), any(), interceptor);
    bindInterceptor(any(), annotatedWith(Transactional.class), interceptor);

}

这是我的Guice模块,我在其中设置了JOOQ和Spring配置。其余类与示例中的完全相同

0 个答案:

没有答案