java - 如何在 dropwizard 上使用 guice 自动连接 HibernateBundle?

标签 java hibernate guice dropwizard

我正在尝试使用 guice/dropwizard 配置 hibernatebundle,需要帮助。 我正在使用 hubspot / dropwizard-guice / 0.7.0除了 dropwizard 库之外的第 3 方库。

下面的代码显然行不通,需要帮助才能弄明白。我该如何重写它,以便将 hibernatebundle 和最终的 session 工厂自动注入(inject)到任何需要它的 bean 中。

我的应用程序.java

public class MyApplication extends Application<MyAppConfiguration> {

    private final HibernateBundle<MyAppConfiguration> hibernateBundle = new HibernateBundle<MyAppConfiguration>(MyModel.class) {
        @Override
        public DataSourceFactory getDataSourceFactory(MyAppConfiguration configuration) {
            return configuration.getDataSourceFactory();
        }
    };

    @Override
    public void initialize(Bootstrap<MyAppConfiguration> bootstrap) {
        bootstrap.addBundle(hibernateBundle);  // ???

        bootstrap.addBundle(
            GuiceBundle.<MyAppConfiguration>newBuilder()
                    .addModule(new MyAppModule())
                    .enableAutoConfig(getClass().getPackage().getName())
                    .setConfigClass(MyAppConfiguration.class)
                    .build()
        );
    }

}   

MyAppModule.java

public class MyAppModule extends AbstractModule {

    @Provides
    public SessionFactory provideSessionFactory(MyAppConfiguration configuration) {
            // really wrong as it creates new instance everytime.
        return configuration.getHibernateBundle().getSessionFactory(); // ???
    }

}

MyAppConfiguration.java

public class MyAppConfiguration extends Configuration {
    @Valid
    @NotNull
    private DataSourceFactory database = new DataSourceFactory();

    @JsonProperty("database")
    public DataSourceFactory getDataSourceFactory() {
        return database;
    }

    @JsonProperty("database")
    public void setDataSourceFactory(DataSourceFactory dataSourceFactory) {
        this.database = dataSourceFactory;
    }

        // ???
    public HibernateBundle<MyAppConfiguration> getHibernateBundle() {
        return new HibernateBundle<MyAppConfiguration>(MyModel.class) {
            @Override
            public DataSourceFactory getDataSourceFactory(MyAppConfiguration configuration) {
                return database;
            }
        };
    }

}  

最佳答案

这就是我最终的做法。我从来没有从这里或邮件列表中得到答案,所以我认为这很老套,可能不是正确的方法,但它对我有用。

在我的模块中(扩展了抽象模块):

private final HibernateBundle<MyConfiguration> hibernateBundle =
        new HibernateBundle<MyConfiguration>(MyModel.class) {
            @Override
            public DataSourceFactory getDataSourceFactory(MyConfiguration configuration) {
                return configuration.getDataSourceFactory();
            }
        };

@Provides
public SessionFactory provideSessionFactory(MyConfiguration configuration,
                                            Environment environment) {

    SessionFactory sf = hibernateBundle.getSessionFactory();
    if (sf == null) {
        try {
            hibernateBundle.run(configuration, environment);
        } catch (Exception e) {
            logger.error("Unable to run hibernatebundle");
        }
    }

    return hibernateBundle.getSessionFactory();
}

修订:

public SessionFactory provideSessionFactory(MyConfiguration configuration,
                                            Environment environment) {

    SessionFactory sf = hibernateBundle.getSessionFactory();
    if (sf == null) {
        try {
            hibernateBundle.run(configuration, environment);
            return hibernateBundle.getSessionFactory();
        } catch (Exception e) {
            logger.error("Unable to run hibernatebundle");
        }
    } else {
        return sf;
    }
}

关于java - 如何在 dropwizard 上使用 guice 自动连接 HibernateBundle?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23320927/

相关文章:

java - 无法从其他类访问主类

java - Java 中的这个框架可能吗?

java - 将一个 Hibernate 命名查询包含在另一个 Hibernate 命名查询中

java - ConstraintViolationException : Duplicate entry

generics - Guice 类型 Kotlin 中的字面量

java - 在 Guice 中使用通配符绑定(bind)泛型

java - 关闭弹出窗口后关闭平台 | JavaFX

java - Hibernate 左连接使用 with

java - Guice 不初始化属性

简单构造函数的 Javadoc 注释。需要还是不需要?