gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:46:41 25 4
gpt4 key购买 nike

我正在尝试使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com