如何将我的 class 实例注入另一个 class?
How to inject instance of my class to another class?
我正在使用 play framework 2.8.x
,我需要将我的配置 class 注入另一个 class,并且我已经编写了用于创建配置的自定义模块。我的模块如下所示:
public class ConfigModule extends AbstractModule {
private Config config;
public ConfigModule(Environment environment, Config config) {
this.config = config;
}
@Override
protected void configure() {
bindRedisConfig();
}
void bindRedisConfig() {
System.out.println("LOAD");
RedisConfig redisConfig = new RedisConfig(
config.getString("configuration.redis.host")
, config.getInt("configuration.redis.port")
, config.getString("configuration.redis.password")
, config.getString("configuration.redis.domain")
);
bind(RedisConfig.class).annotatedWith(Names.named(Constant.CONFIG_REDIS)).toInstance(redisConfig);
}
}
我试着像这样注入它:
public class ProcessDataStore {
private final JedisPool jedisPool;
private final RedisConfig config;
@Inject
public ProcessDataStore(RedisConfig config) {
this.config = config;
}
}
我的配置class很简单:
@Singleton
public class RedisConfig {
public final String host;
public final int port;
public final String password;
public final String domain;
public RedisConfig(String host, int port, String password, String domain) {
this.host = host;
this.port = port;
this.password = password;
this.domain = domain;
}
}
但是当我 运行 我的应用程序出现异常时:
1) Could not find a suitable constructor in config.RedisConfig. Classes must have either one (and only one) constructor annotated with @Inject or a zero-argument constructor that is not private.
at config.RedisConfig.class(RedisConfig.java:17)
while locating config.RedisConfig
for the 1st parameter of repository.ProcessDataStore.<init>(ProcessDataStore.java:25)
while locating repository.ProcessDataStore
for field at controllers.DashboardController.db(DashboardController.java:19)
while locating controllers.DashboardController
for the 5th parameter of router.Routes.<init>(Routes.scala:38)
at play.api.inject.RoutesProvider$.bindingsFromConfiguration(BuiltinModule.scala:137):
Binding(class router.Routes to self) (via modules: com.google.inject.util.Modules$OverrideModule -> play.api.inject.guice.GuiceableModuleConversions$$anon)
如果我创建没有参数的默认构造函数,我的应用程序不会抛出任何异常,但注入的 RedisConfig
是空的。
我将我的模块加载到我的应用程序:
play.modules.enabled += "modules.ConfigModule"
我做错了什么?
我看到 2 个可疑的东西。
1) @Singleton
RedisConfig
class 注释。它可以使注入器创建和注入 RedisConfig
class 的新实例而不是 redisConfig
.
2) 绑定定义包含.annotatedWith(Names.named(Constant.CONFIG_REDIS))
。因此ProcessDataStore
的构造函数参数应该注解为@Named
:
@Inject
public ProcessDataStore( @Named(Constant.CONFIG_REDIS) RedisConfig config) {...}
我正在使用 play framework 2.8.x
,我需要将我的配置 class 注入另一个 class,并且我已经编写了用于创建配置的自定义模块。我的模块如下所示:
public class ConfigModule extends AbstractModule {
private Config config;
public ConfigModule(Environment environment, Config config) {
this.config = config;
}
@Override
protected void configure() {
bindRedisConfig();
}
void bindRedisConfig() {
System.out.println("LOAD");
RedisConfig redisConfig = new RedisConfig(
config.getString("configuration.redis.host")
, config.getInt("configuration.redis.port")
, config.getString("configuration.redis.password")
, config.getString("configuration.redis.domain")
);
bind(RedisConfig.class).annotatedWith(Names.named(Constant.CONFIG_REDIS)).toInstance(redisConfig);
}
}
我试着像这样注入它:
public class ProcessDataStore {
private final JedisPool jedisPool;
private final RedisConfig config;
@Inject
public ProcessDataStore(RedisConfig config) {
this.config = config;
}
}
我的配置class很简单:
@Singleton
public class RedisConfig {
public final String host;
public final int port;
public final String password;
public final String domain;
public RedisConfig(String host, int port, String password, String domain) {
this.host = host;
this.port = port;
this.password = password;
this.domain = domain;
}
}
但是当我 运行 我的应用程序出现异常时:
1) Could not find a suitable constructor in config.RedisConfig. Classes must have either one (and only one) constructor annotated with @Inject or a zero-argument constructor that is not private.
at config.RedisConfig.class(RedisConfig.java:17)
while locating config.RedisConfig
for the 1st parameter of repository.ProcessDataStore.<init>(ProcessDataStore.java:25)
while locating repository.ProcessDataStore
for field at controllers.DashboardController.db(DashboardController.java:19)
while locating controllers.DashboardController
for the 5th parameter of router.Routes.<init>(Routes.scala:38)
at play.api.inject.RoutesProvider$.bindingsFromConfiguration(BuiltinModule.scala:137):
Binding(class router.Routes to self) (via modules: com.google.inject.util.Modules$OverrideModule -> play.api.inject.guice.GuiceableModuleConversions$$anon)
如果我创建没有参数的默认构造函数,我的应用程序不会抛出任何异常,但注入的 RedisConfig
是空的。
我将我的模块加载到我的应用程序:
play.modules.enabled += "modules.ConfigModule"
我做错了什么?
我看到 2 个可疑的东西。
1) @Singleton
RedisConfig
class 注释。它可以使注入器创建和注入 RedisConfig
class 的新实例而不是 redisConfig
.
2) 绑定定义包含.annotatedWith(Names.named(Constant.CONFIG_REDIS))
。因此ProcessDataStore
的构造函数参数应该注解为@Named
:
@Inject
public ProcessDataStore( @Named(Constant.CONFIG_REDIS) RedisConfig config) {...}