如何在 Play 2.5.4 中使用 Guice 注入 Utility 类
How to inject Utility classes with Guice in Play 2.5.4
我正在尝试使用 play 2.5.4 将示例数据加载到我的数据库中
最初我正在关注 https://www.playframework.com/documentation/2.2.x/JavaGuide1
上的教程
但由于它是为 Play 2.2 编写的,所以我会尽力对其进行改编并避免弃用的内容。
现在,我对 DI 的东西还是有点迷茫。
我目前拥有的:
application.conf
play.modules.enabled += "GuiceConfiguration"
GuiceConfiguration.java
public class GuiceConfiguration extends AbstractModule{
@Override
protected void configure() {
bind(OnStartupService.class).asEagerSingleton();
}
}
OnStartupService.java
@Singleton
public class OnStartupService {
@Inject
private OnStartupService(Environment environment) {
if (environment.isDev()) {
if (Issue.find.findRowCount() == 0) {
System.out.println("Okay");
}
}
}
}
以上代码工作正常!
以下不是:
@Singleton
public class OnStartupService {
@Inject
private OnStartupService(Environment environment) {
if (environment.isDev()) {
if (Issue.find.findRowCount() == 0) {
Ebean.saveAll((Collection<?>) Yaml.load("test-data.yml"));
}
}
}
}
编辑:失败,出现以下异常
play.api.UnexpectedException: Unexpected exception[CreationException: Unable to create injector, see the following errors:
1) Error injecting constructor, java.lang.RuntimeException: There is no started application
at OnStartupService.<init>(OnStartupService.java:1)
at GuiceConfiguration.configure(GuiceConfiguration.java:9) (via modules: com.google.inject.util.Modules$OverrideModule -> GuiceConfiguration)
while locating OnStartupService
所以我想我应该以某种方式注入 Ebean 和 Yaml。但我只使用那些 类 中的静态方法。
谁能帮助我了解如何执行此操作以及原因。
如果您查看 Yaml.java
的 code,我们可以看到它在第 20 行使用了静态 Play.current()
。这就是您收到错误的原因,因为没有应用程序开始了。
此外,Yaml
将在 2.6 中弃用,因此我建议您遵循 migration guide。它向您解释了如何解决您的问题:
- 添加对
snakeyaml
的依赖
- 创建你自己的
Yaml
class
- 在里面注入
Application
编辑
然后注入你的Yaml
class,不要使用静态方式:
@Singleton
public class OnStartupService {
@Inject
private OnStartupService(Environment environment, Yaml yaml) {
if (environment.isDev()) {
if (Issue.find.findRowCount() == 0) {
Ebean.saveAll((Collection<?>) yaml.load("test-data.yml"));
}
}
}
}
我正在尝试使用 play 2.5.4 将示例数据加载到我的数据库中 最初我正在关注 https://www.playframework.com/documentation/2.2.x/JavaGuide1
上的教程但由于它是为 Play 2.2 编写的,所以我会尽力对其进行改编并避免弃用的内容。
现在,我对 DI 的东西还是有点迷茫。
我目前拥有的:
application.conf
play.modules.enabled += "GuiceConfiguration"
GuiceConfiguration.java
public class GuiceConfiguration extends AbstractModule{
@Override
protected void configure() {
bind(OnStartupService.class).asEagerSingleton();
}
}
OnStartupService.java
@Singleton
public class OnStartupService {
@Inject
private OnStartupService(Environment environment) {
if (environment.isDev()) {
if (Issue.find.findRowCount() == 0) {
System.out.println("Okay");
}
}
}
}
以上代码工作正常!
以下不是:
@Singleton
public class OnStartupService {
@Inject
private OnStartupService(Environment environment) {
if (environment.isDev()) {
if (Issue.find.findRowCount() == 0) {
Ebean.saveAll((Collection<?>) Yaml.load("test-data.yml"));
}
}
}
}
编辑:失败,出现以下异常
play.api.UnexpectedException: Unexpected exception[CreationException: Unable to create injector, see the following errors:
1) Error injecting constructor, java.lang.RuntimeException: There is no started application
at OnStartupService.<init>(OnStartupService.java:1)
at GuiceConfiguration.configure(GuiceConfiguration.java:9) (via modules: com.google.inject.util.Modules$OverrideModule -> GuiceConfiguration)
while locating OnStartupService
所以我想我应该以某种方式注入 Ebean 和 Yaml。但我只使用那些 类 中的静态方法。 谁能帮助我了解如何执行此操作以及原因。
如果您查看 Yaml.java
的 code,我们可以看到它在第 20 行使用了静态 Play.current()
。这就是您收到错误的原因,因为没有应用程序开始了。
此外,Yaml
将在 2.6 中弃用,因此我建议您遵循 migration guide。它向您解释了如何解决您的问题:
- 添加对
snakeyaml
的依赖
- 创建你自己的
Yaml
class - 在里面注入
Application
编辑
然后注入你的Yaml
class,不要使用静态方式:
@Singleton
public class OnStartupService {
@Inject
private OnStartupService(Environment environment, Yaml yaml) {
if (environment.isDev()) {
if (Issue.find.findRowCount() == 0) {
Ebean.saveAll((Collection<?>) yaml.load("test-data.yml"));
}
}
}
}